Moved functionality to write entries to a file to event.d

This commit is contained in:
Johannes Loher 2017-08-07 01:09:45 +02:00
parent 0da71540bc
commit f521d39772
2 changed files with 22 additions and 16 deletions

View file

@ -2,7 +2,7 @@ module calendarwebapp;
import event;
import std.datetime.date;
import std.datetime.date : Date;
import std.typecons : Nullable;
import vibe.vibe;
@ -57,8 +57,8 @@ public:
render!("create.dt", _error);
}
@anyAuth @errorDisplay!getCreate void postCreate(Date begin, Nullable!Date end,
string description, string name, EventType type, bool shout)
@anyAuth @errorDisplay!getCreate void postCreate(Date begin,
Nullable!Date end, string description, string name, EventType type, bool shout)
{
import std.array : split, replace;
@ -70,7 +70,7 @@ public:
description.replace("\r", "").split('\n'), type, shout));
auto entries = getEntriesFromFile(fileName) ~ entry;
fileName.writeFileUTF8(entries.serializeToPrettyJson);
entries.writeEntriesToFile(fileName);
render!("showevents.dt", entries);
}
@ -81,19 +81,7 @@ public:
}
private:
immutable fileName = Path("events.json");
SessionVar!(AuthInfo, "auth") auth;
Entry[] getEntriesFromFile(in Path fileName)
{
Entry[] entries;
if (fileName.existsFile)
{
deserializeJson(entries, fileName.readFileUTF8.parseJsonString);
}
return entries;
}
}

View file

@ -3,6 +3,9 @@ module event;
import std.datetime.date;
import std.typecons : Nullable;
import vibe.core.file: existsFile, readFileUTF8, writeFileUTF8;
import vibe.core.path;
import vibe.data.json : deserializeJson, parseJsonString, serializeToPrettyJson;
import vibe.data.serialization;
enum EventType
@ -29,3 +32,18 @@ struct Event
@(vibe.data.serialization.name("etype")) EventType type;
bool shout;
}
Entry[] getEntriesFromFile(in Path fileName)
{
Entry[] entries;
if (fileName.existsFile)
{
deserializeJson(entries, fileName.readFileUTF8.parseJsonString);
}
return entries;
}
void writeEntriesToFile(in Entry[] entries, in Path fileName)
{
fileName.writeFileUTF8(entries.serializeToPrettyJson);
}