From f521d397729cc4fe1556a3fa1b71481b9e27fa0c Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 7 Aug 2017 01:09:45 +0200 Subject: [PATCH] Moved functionality to write entries to a file to event.d --- source/calendarwebapp.d | 20 ++++---------------- source/event.d | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/source/calendarwebapp.d b/source/calendarwebapp.d index 3e596d6..26310f0 100644 --- a/source/calendarwebapp.d +++ b/source/calendarwebapp.d @@ -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; - } - } diff --git a/source/event.d b/source/event.d index 0960686..80597cf 100644 --- a/source/event.d +++ b/source/event.d @@ -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); +}