This commit is contained in:
Johannes Loher 2017-12-03 01:27:00 +01:00
parent c918e8e574
commit 6a26370b0c
3 changed files with 154 additions and 7 deletions

View file

@ -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)
{

View file

@ -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;
}
}

View file

@ -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));
}