This commit is contained in:
Johannes Loher 2017-12-10 00:44:05 +01:00
parent 29a20fa4fb
commit eb1a1fd1cc
2 changed files with 75 additions and 23 deletions

View file

@ -17,7 +17,7 @@ interface EventStore
Event getEvent(string id); Event getEvent(string id);
InputRange!Event getAllEvents(); InputRange!Event getAllEvents();
void addEvent(Event); void addEvent(Event);
InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe; InputRange!Event getEventsBeginningBetween(Date begin, Date end);
void removeEvent(string id); void removeEvent(string id);
} }
@ -109,7 +109,7 @@ public:
prepared.exec(); prepared.exec();
} }
InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe InputRange!Event getEventsBeginningBetween(Date begin, Date end)
{ {
auto cn = pool.lockConnection(); auto cn = pool.lockConnection();
scope (exit) scope (exit)

View file

@ -9,6 +9,8 @@ import std.datetime.systime;
import poodinis : Autowire; import poodinis : Autowire;
import vibe.data.serialization : name;
struct DayJSONManager struct DayJSONManager
{ {
private: private:
@ -24,7 +26,7 @@ public:
void addEvent(Event event) void addEvent(Event event)
{ {
if (Interval(begin, end).contains(event.begin)) if (Interval!Date(begin, end).contains(event.begin))
{ {
if (event.end.isNull) if (event.end.isNull)
{ {
@ -33,6 +35,15 @@ public:
events[event.begin] ~= event; events[event.begin] ~= event;
} }
} }
auto getDayData(Date date)
{
import std.exception : enforce;
enforce(Interval!Date(begin, end).contains(date));
return DayData(date.year, date.month, date.month.toGerString, date.day,
date.dayOfWeek.dayType, events[date], date.dayOfWeek.toGerString, []);
}
} }
class JSONExporter class JSONExporter
@ -43,15 +54,24 @@ private:
public: public:
auto write() @system auto write() @system
{ {
import std.algorithm : each, map;
import std.range : array;
import std.format : format; import std.format : format;
immutable today = cast(Date) Clock.currTime; immutable today = cast(Date) Clock.currTime;
immutable todayName = "%s, %s. %s. %s".format(today.dayOfWeek.toGerString, immutable todayName = "%s, %s. %s. %s".format(today.dayOfWeek.toGerString,
today.day, today.month.toGerString, today.year); today.day, today.month.toGerString, today.year);
immutable todays = Todays(today.year, today.month, today.day, today.dayOfWeek, todayName);
auto startDate = Date(today.year, today.month, 1); auto startDate = Date(today.year, today.month, 1);
auto endDate = startDate; auto endDate = startDate;
endDate.add!"months"(3); endDate.add!"months"(3);
return endDate; auto dayJSONManager = new DayJSONManager(startDate, endDate);
foreach (event; eventStore.getEventsBeginningBetween(startDate, endDate))
{
dayJSONManager.addEvent(event);
}
auto trackedDays = Interval!Date(startDate, endDate).fwdRange(date => date + 1.dur!"days")
.map!(day => dayJSONManager.getDayData(day)).array;
} }
} }
@ -64,40 +84,28 @@ string toGerString(Month m)
{ {
case jan: case jan:
return "Januar"; return "Januar";
break;
case feb: case feb:
return "Februar"; return "Februar";
break;
case mar: case mar:
return "März"; return "März";
break;
case apr: case apr:
return "April"; return "April";
break;
case may: case may:
return "Mai"; return "Mai";
break;
case jun: case jun:
return "Juni"; return "Juni";
break;
case jul: case jul:
return "Juli"; return "Juli";
break;
case aug: case aug:
return "August"; return "August";
break;
case sep: case sep:
return "September"; return "September";
break;
case oct: case oct:
return "Oktober"; return "Oktober";
break;
case nov: case nov:
return "November"; return "November";
break;
case dec: case dec:
return "Dezember"; return "Dezember";
break;
} }
} }
@ -107,24 +115,68 @@ string toGerString(DayOfWeek d)
{ {
case mon: case mon:
return "Montag"; return "Montag";
break;
case tue: case tue:
return "Dienstag"; return "Dienstag";
break;
case wed: case wed:
return "Mittwoch"; return "Mittwoch";
break;
case thu: case thu:
return "Donnerstag"; return "Donnerstag";
break;
case fri: case fri:
return "Freitag"; return "Freitag";
break;
case sat: case sat:
return "Samstag"; return "Samstag";
break;
case sun: case sun:
return "Sonntag"; return "Sonntag";
break;
} }
} }
enum DayType
{
Workday,
Holiday,
Weekend
}
DayType dayType(DayOfWeek dayOfWeek)
{
switch (dayOfWeek) with (DayOfWeek)
{
case sat:
return DayType.Weekend;
case sun:
return DayType.Holiday;
default:
return DayType.Workday;
}
}
struct DayData
{
short year;
Month month;
string monthName;
ubyte day;
@name("daytype") DayType dayType;
Event[] eventList;
@name("wday") string weekDayName;
Line[] lines;
}
struct Line
{
}
struct Todays
{
short year;
Month month;
ubyte day;
DayOfWeek weekDay;
string todayName;
}
struct OutputFormat
{
Todays today;
@name("tracked_days") DayData trackedDays;
}