From 3af4f2fde9c4c1cdf746652d88665e8df34b6842 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sun, 6 Aug 2017 21:52:27 +0200 Subject: [PATCH] changed the date format, it's now almost the same as used by calutil --- source/app.d | 77 +++++++++++++++++++++++++++++---------------- views/create.dt | 31 +++++++++++++----- views/listevents.dt | 18 ----------- views/showevents.dt | 27 ++++++++++++++++ 4 files changed, 100 insertions(+), 53 deletions(-) delete mode 100644 views/listevents.dt create mode 100644 views/showevents.dt diff --git a/source/app.d b/source/app.d index 302107b..5d616b6 100644 --- a/source/app.d +++ b/source/app.d @@ -1,4 +1,5 @@ import std.datetime.date; +import std.typecons : Nullable; import vibe.vibe; @@ -7,47 +8,69 @@ class CalendarWebapp private: immutable fileName = Path("events.json"); - struct Event + enum EventType { - string name, place; - DateTime begin, end; + Holiday, + Birthday, + FSI_Event, + General_University_Event, + Any } - Event[] getEventsFromFile(in Path fileName) + struct Entry { - Event[] events; - auto eventsString = readFileUTF8(fileName); + @name("date") Date begin; + @name("end_date") Nullable!Date end; + Event event; + } + + struct Event + { + @(vibe.data.serialization.name("eid")) string id; + string name; + @(vibe.data.serialization.name("desc")) string[] description; + @(vibe.data.serialization.name("etype")) EventType type; + bool shout; + } + + Entry[] getEventsFromFile(in Path fileName) + { + Entry[] entries; try { - deserializeJson(events, eventsString.parseJsonString); + auto entriesString = readFileUTF8(fileName); + + try + { + deserializeJson(entries, entriesString.parseJsonString); + } + catch (std.json.JSONException) + { + } } - catch (std.json.JSONException) - { - } - return events; + catch(Exception) + {} + return entries; } public: @method(HTTPMethod.POST) @path("/event/create") - void createEvent(string Ereignisname, string Ereignisort, string Von, string Bis) + void createEvent(Date begin, Nullable!Date end, string description, + string name, EventType type, bool shout) { - Event event; + import std.array : split, replace; - event.name = Ereignisname; - event.place = Ereignisort; + if (!end.isNull) + enforce(end - begin >= 1.days); - event.begin = DateTime.fromISOExtString(Von ~ ":00"); - event.end = DateTime.fromISOExtString(Bis ~ ":00"); - enforce(event.end - event.begin > 0.seconds); + auto entry = Entry(begin, end, Event("", name, + description.replace("\r", "").split('\n'), type, shout)); - auto events = getEventsFromFile(fileName); - - events ~= event; - - writeFileUTF8(fileName, events.serializeToJsonString()); - - render!("listevents.dt", events); + auto entries = getEventsFromFile(fileName); + entries ~= entry; + writeFileUTF8(fileName, serializeToPrettyJson(entries)); + render!("showevents.dt", entries); } @method(HTTPMethod.GET) @path("create") @@ -58,8 +81,8 @@ public: void index() { - auto events = getEventsFromFile(fileName); - render!("listevents.dt", events); + auto entries = getEventsFromFile(fileName); + render!("showevents.dt", entries); } } diff --git a/views/create.dt b/views/create.dt index bd50cb2..fa6ea05 100644 --- a/views/create.dt +++ b/views/create.dt @@ -6,24 +6,39 @@ block content tbody#fieldTable tr td - label(for="Ereignisname") Ereignisname + label(for="name") Ereignisname td - input#Ereignisname(value="", name="Ereignisname", type="text") + input#name(value="", name="name", type="text") tr td - label(for="Ereignisort") Ereignisort + label(for="description") Beschreibung td - input#Ereignisort(value="", name="Ereignisort", type="text") + textarea#description(value="", name="description", cols="40", rows="5") tr td - label(for="Von") Von + label(for="begin") Von td - input#Von(value="", name="Von", type="datetime-local") + input#begin(value="", name="begin", type="date") tr td - label(for="Bis") Bis + label(for="end") Bis td - input#Bis(value="", name="Bis", type="datetime-local") + input#end(value="", name="end", type="date") + tr + td + label(for="type") Typ + td + select#type(name="type") + option(value="Holiday") Feiertag + option(value="Birthday") Geburtstag + option(value="FSI_Event") FSI Ereignis + option(value="General_University_Event") Allgemeines Universitäts Ereignis + option(value="Any") Sonstiges + tr + td + label(for="shout") Shout + td + input#shout(name="shout", type="checkbox") tfoot tr diff --git a/views/listevents.dt b/views/listevents.dt deleted file mode 100644 index 759c9d5..0000000 --- a/views/listevents.dt +++ /dev/null @@ -1,18 +0,0 @@ -extends layout.dt -block content - h1 Events - - foreach(event; events) - table - tr - td Ereignisname - td #{event.name} - tr - td Ereignisort - td #{event.place} - tr - td Von - td #{event.begin} - tr - td Bis - td #{event.end} - hr diff --git a/views/showevents.dt b/views/showevents.dt new file mode 100644 index 0000000..ff46ad3 --- /dev/null +++ b/views/showevents.dt @@ -0,0 +1,27 @@ +extends layout.dt +block content + h1 Events + - foreach (entry; entries) + table + tr + td begin + td #{entry.begin} + tr + td end + td #{entry.end} + tr + td eid + td #{entry.event.id} + tr + td name + td #{entry.event.name} + tr + td desc + td #{entry.event.description} + tr + td etype + td #{entry.event.type} + tr + td shout + td #{entry.event.shout} + hr