2017-12-03 01:27:00 +01:00
|
|
|
module calendarwebapp.jsonexport;
|
|
|
|
|
|
|
|
import calendarwebapp.event : Event, EventStore;
|
2018-01-14 17:59:16 +01:00
|
|
|
import calendarwebapp.configuration : Arguments;
|
2017-12-03 01:27:00 +01:00
|
|
|
|
|
|
|
import core.time;
|
2018-01-14 13:51:52 +01:00
|
|
|
|
|
|
|
import std.algorithm.iteration : each;
|
|
|
|
|
2017-12-03 01:27:00 +01:00
|
|
|
import std.datetime.date;
|
|
|
|
import std.datetime.interval;
|
|
|
|
import std.datetime.systime;
|
|
|
|
|
2018-01-14 13:51:52 +01:00
|
|
|
import std.format : format;
|
2018-01-14 17:59:16 +01:00
|
|
|
import poodinis : Autowire, Value;
|
2017-12-03 01:27:00 +01:00
|
|
|
|
2018-01-14 17:59:16 +01:00
|
|
|
import vibe.data.serialization : serializationName = name;
|
2017-12-10 00:44:05 +01:00
|
|
|
|
2018-01-14 17:59:16 +01:00
|
|
|
struct DayDataManager
|
2017-12-03 01:27:00 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
Date begin, end;
|
|
|
|
Event[][Date] events;
|
|
|
|
|
|
|
|
public:
|
|
|
|
this(in Date begin, in Date end)
|
2018-01-14 13:51:52 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
assert(begin < end,
|
2018-01-14 17:59:16 +01:00
|
|
|
"DayDataManager: begin (%s) needs to be earlier than end (%s)".format(begin, end));
|
2018-01-14 13:51:52 +01:00
|
|
|
}
|
|
|
|
do
|
2017-12-03 01:27:00 +01:00
|
|
|
{
|
|
|
|
this.begin = begin;
|
|
|
|
this.end = end;
|
2018-01-14 13:51:52 +01:00
|
|
|
Interval!Date(this.begin, this.end).fwdRange(date => date + 1.dur!"days")
|
|
|
|
.each!(date => events[date] = []);
|
2017-12-03 01:27:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void addEvent(Event event)
|
|
|
|
{
|
2017-12-10 00:44:05 +01:00
|
|
|
if (Interval!Date(begin, end).contains(event.begin))
|
2017-12-03 01:27:00 +01:00
|
|
|
{
|
|
|
|
events[event.begin] ~= event;
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 00:44:05 +01:00
|
|
|
|
|
|
|
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,
|
2018-01-14 17:59:16 +01:00
|
|
|
date.dayOfWeek.dayType, events[date], date.dayOfWeek.toShortGerString, []);
|
2017-12-10 00:44:05 +01:00
|
|
|
}
|
2017-12-03 01:27:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class JSONExporter
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
@Autowire EventStore eventStore;
|
2018-01-14 17:59:16 +01:00
|
|
|
@Value() Arguments arguments;
|
2017-12-03 01:27:00 +01:00
|
|
|
|
|
|
|
public:
|
2018-01-14 15:04:49 +01:00
|
|
|
auto write(in Date today = cast(Date) Clock.currTime) @system
|
2017-12-03 01:27:00 +01:00
|
|
|
{
|
2017-12-10 00:44:05 +01:00
|
|
|
import std.algorithm : each, map;
|
|
|
|
import std.range : array;
|
2017-12-03 01:27:00 +01:00
|
|
|
import std.format : format;
|
|
|
|
|
2018-01-14 17:59:16 +01:00
|
|
|
immutable todayName = dateFormatString.format(today.dayOfWeek.toGerString,
|
2017-12-03 01:27:00 +01:00
|
|
|
today.day, today.month.toGerString, today.year);
|
2018-01-14 17:59:16 +01:00
|
|
|
immutable todays = Today(today.year, today.month, today.day, today.dayOfWeek, todayName);
|
2017-12-03 01:27:00 +01:00
|
|
|
auto startDate = Date(today.year, today.month, 1);
|
|
|
|
auto endDate = startDate;
|
|
|
|
endDate.add!"months"(3);
|
2018-01-14 17:59:16 +01:00
|
|
|
auto dayDataManager = new DayDataManager(startDate, endDate);
|
2017-12-10 00:44:05 +01:00
|
|
|
foreach (event; eventStore.getEventsBeginningBetween(startDate, endDate))
|
|
|
|
{
|
2018-01-14 17:59:16 +01:00
|
|
|
dayDataManager.addEvent(event);
|
2017-12-10 00:44:05 +01:00
|
|
|
}
|
2017-12-10 17:04:23 +01:00
|
|
|
return Interval!Date(startDate, endDate).fwdRange(date => date + 1.dur!"days")
|
2018-01-14 17:59:16 +01:00
|
|
|
.map!(day => dayDataManager.getDayData(day)).array;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exportJSON() @system
|
|
|
|
{
|
|
|
|
import vibe.core.file : writeFile;
|
|
|
|
import vibe.core.path : Path;
|
|
|
|
import vibe.data.json : serializeToPrettyJson;
|
|
|
|
import std.datetime.systime : Clock;
|
|
|
|
import std.datetime.date : Date;
|
|
|
|
|
|
|
|
struct OutputFormat
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
alias TrackedDays = typeof(write());
|
|
|
|
public:
|
|
|
|
Today today;
|
|
|
|
@serializationName("tracked_days") TrackedDays trackedDays;
|
|
|
|
}
|
|
|
|
|
|
|
|
immutable today = cast(Date) Clock.currTime;
|
|
|
|
auto output = OutputFormat(Today(today.year, today.month, today.day,
|
|
|
|
today.dayOfWeek, dateFormatString.format(today.dayOfWeek.toGerString,
|
|
|
|
today.day, today.month.toGerString, today.year)), this.write());
|
|
|
|
Path(arguments.output).writeFile(cast(ubyte[]) output.serializeToPrettyJson);
|
2017-12-03 01:27:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 13:51:52 +01:00
|
|
|
struct DayData
|
|
|
|
{
|
|
|
|
short year;
|
|
|
|
Month month;
|
|
|
|
string monthName;
|
|
|
|
ubyte day;
|
2018-01-14 17:59:16 +01:00
|
|
|
@serializationName("daytype") DayType dayType;
|
|
|
|
Event[] events;
|
|
|
|
@serializationName("wday") string weekDayName;
|
2018-01-14 13:51:52 +01:00
|
|
|
Line[] lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DayType
|
|
|
|
{
|
|
|
|
Workday,
|
|
|
|
Holiday,
|
|
|
|
Weekend
|
|
|
|
}
|
|
|
|
|
2017-12-03 01:27:00 +01:00
|
|
|
private:
|
|
|
|
|
2018-01-14 17:59:16 +01:00
|
|
|
enum dateFormatString = "%s, %s. %s, %s";
|
|
|
|
|
2017-12-03 01:27:00 +01:00
|
|
|
string toGerString(Month m)
|
|
|
|
{
|
|
|
|
final switch (m) with (Month)
|
|
|
|
{
|
|
|
|
case jan:
|
|
|
|
return "Januar";
|
|
|
|
case feb:
|
|
|
|
return "Februar";
|
|
|
|
case mar:
|
|
|
|
return "März";
|
|
|
|
case apr:
|
|
|
|
return "April";
|
|
|
|
case may:
|
|
|
|
return "Mai";
|
|
|
|
case jun:
|
|
|
|
return "Juni";
|
|
|
|
case jul:
|
|
|
|
return "Juli";
|
|
|
|
case aug:
|
|
|
|
return "August";
|
|
|
|
case sep:
|
|
|
|
return "September";
|
|
|
|
case oct:
|
|
|
|
return "Oktober";
|
|
|
|
case nov:
|
|
|
|
return "November";
|
|
|
|
case dec:
|
|
|
|
return "Dezember";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string toGerString(DayOfWeek d)
|
|
|
|
{
|
|
|
|
final switch (d) with (DayOfWeek)
|
|
|
|
{
|
|
|
|
case mon:
|
|
|
|
return "Montag";
|
|
|
|
case tue:
|
|
|
|
return "Dienstag";
|
|
|
|
case wed:
|
|
|
|
return "Mittwoch";
|
|
|
|
case thu:
|
|
|
|
return "Donnerstag";
|
|
|
|
case fri:
|
|
|
|
return "Freitag";
|
|
|
|
case sat:
|
|
|
|
return "Samstag";
|
|
|
|
case sun:
|
|
|
|
return "Sonntag";
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 00:44:05 +01:00
|
|
|
|
2018-01-14 17:59:16 +01:00
|
|
|
string toShortGerString(DayOfWeek d)
|
|
|
|
{
|
|
|
|
final switch (d) with (DayOfWeek)
|
|
|
|
{
|
|
|
|
case mon:
|
|
|
|
return "Mo";
|
|
|
|
case tue:
|
|
|
|
return "Di";
|
|
|
|
case wed:
|
|
|
|
return "Mi";
|
|
|
|
case thu:
|
|
|
|
return "Do";
|
|
|
|
case fri:
|
|
|
|
return "Fr";
|
|
|
|
case sat:
|
|
|
|
return "Sa";
|
|
|
|
case sun:
|
|
|
|
return "So";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 00:44:05 +01:00
|
|
|
DayType dayType(DayOfWeek dayOfWeek)
|
|
|
|
{
|
|
|
|
switch (dayOfWeek) with (DayOfWeek)
|
|
|
|
{
|
|
|
|
case sat:
|
|
|
|
return DayType.Weekend;
|
|
|
|
case sun:
|
|
|
|
return DayType.Holiday;
|
|
|
|
default:
|
|
|
|
return DayType.Workday;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Line
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-14 17:59:16 +01:00
|
|
|
struct Today
|
2017-12-10 00:44:05 +01:00
|
|
|
{
|
|
|
|
short year;
|
|
|
|
Month month;
|
|
|
|
ubyte day;
|
2018-01-14 17:59:16 +01:00
|
|
|
@serializationName("weekday") DayOfWeek weekDay;
|
|
|
|
string name;
|
2017-12-10 00:44:05 +01:00
|
|
|
}
|