From 6a26370b0c67ab7312ed39352047f16e7f3b166c Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sun, 3 Dec 2017 01:27:00 +0100 Subject: [PATCH] wip --- source/calendarwebapp/event.d | 18 ++-- source/calendarwebapp/jsonexport.d | 130 +++++++++++++++++++++++++++ test/calendarwebapp/testjsonexport.d | 13 +++ 3 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 source/calendarwebapp/jsonexport.d create mode 100644 test/calendarwebapp/testjsonexport.d diff --git a/source/calendarwebapp/event.d b/source/calendarwebapp/event.d index bde695d..e373a5e 100644 --- a/source/calendarwebapp/event.d +++ b/source/calendarwebapp/event.d @@ -17,7 +17,7 @@ interface EventStore Event getEvent(string id); InputRange!Event getAllEvents(); void addEvent(Event); - /* InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe; */ + InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe; void removeEvent(string id); } @@ -54,7 +54,7 @@ public: InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe { return events.find(["$and" : [["date" : ["$gte" : begin.serializeToBson]], - ["date" : ["$lte" : end.serializeToBson]]]]).map!(deserializeBson!Event) + ["date" : ["$lt" : end.serializeToBson]]]]).map!(deserializeBson!Event) .inputRangeObject; } @@ -107,12 +107,16 @@ public: prepared.exec(); } - /* InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe + InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe { - return events.find(["$and" : [["date" : ["$gte" : begin.serializeToBson]], - ["date" : ["$lte" : end.serializeToBson]]]]).map!(deserializeBson!Event) - .inputRangeObject; - } */ + auto cn = pool.lockConnection(); + scope (exit) + cn.close; + auto prepared = cn.prepare( + "SELECT id, begin, end, name, description, type, shout FROM events WHERE begin >= ? and end < ?"); + prepared.setArgs(begin, end); + return prepared.querySet.map!(r => toEvent(r)).inputRangeObject; + } void removeEvent(string id) { diff --git a/source/calendarwebapp/jsonexport.d b/source/calendarwebapp/jsonexport.d new file mode 100644 index 0000000..e1e74c7 --- /dev/null +++ b/source/calendarwebapp/jsonexport.d @@ -0,0 +1,130 @@ +module calendarwebapp.jsonexport; + +import calendarwebapp.event : Event, EventStore; + +import core.time; +import std.datetime.date; +import std.datetime.interval; +import std.datetime.systime; + +import poodinis : Autowire; + +struct DayJSONManager +{ +private: + Date begin, end; + Event[][Date] events; + +public: + this(in Date begin, in Date end) + { + this.begin = begin; + this.end = end; + } + + void addEvent(Event event) + { + if (Interval(begin, end).contains(event.begin)) + { + if (event.end.isNull) + { + event.end = event.begin; + } + events[event.begin] ~= event; + } + } +} + +class JSONExporter +{ +private: + @Autowire EventStore eventStore; + +public: + auto write() @system + { + import std.format : format; + + immutable today = cast(Date) Clock.currTime; + immutable todayName = "%s, %s. %s. %s".format(today.dayOfWeek.toGerString, + today.day, today.month.toGerString, today.year); + auto startDate = Date(today.year, today.month, 1); + auto endDate = startDate; + endDate.add!"months"(3); + return endDate; + } + +} + +private: + +string toGerString(Month m) +{ + final switch (m) with (Month) + { + case jan: + return "Januar"; + break; + case feb: + return "Februar"; + break; + case mar: + return "März"; + break; + case apr: + return "April"; + break; + case may: + return "Mai"; + break; + case jun: + return "Juni"; + break; + case jul: + return "Juli"; + break; + case aug: + return "August"; + break; + case sep: + return "September"; + break; + case oct: + return "Oktober"; + break; + case nov: + return "November"; + break; + case dec: + return "Dezember"; + break; + } +} + +string toGerString(DayOfWeek d) +{ + final switch (d) with (DayOfWeek) + { + case mon: + return "Montag"; + break; + case tue: + return "Dienstag"; + break; + case wed: + return "Mittwoch"; + break; + case thu: + return "Donnerstag"; + break; + case fri: + return "Freitag"; + break; + case sat: + return "Samstag"; + break; + case sun: + return "Sonntag"; + break; + } +} diff --git a/test/calendarwebapp/testjsonexport.d b/test/calendarwebapp/testjsonexport.d new file mode 100644 index 0000000..5abcf5b --- /dev/null +++ b/test/calendarwebapp/testjsonexport.d @@ -0,0 +1,13 @@ +module test.calendarwebapp.testjsonexport; + +import calendarwebapp.jsonexport; + +import unit_threaded; +import std.datetime.date : Date; + +@("calendarwebapp.JSONExporter") +@system unittest +{ + auto exporter = new JSONExporter; + exporter.write.shouldEqual(Date(2018, 2, 1)); +}