calendar-webapp/source/app.d

79 lines
1.7 KiB
D
Raw Normal View History

2017-08-02 01:56:36 +02:00
import std.datetime.date;
2017-08-05 17:31:35 +02:00
import vibe.vibe;
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
class CalendarWebapp
{
private:
immutable fileName = Path("events.json");
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
struct Event
{
string name, place;
DateTime begin, end;
}
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
Event[] getEventsFromFile(in Path fileName)
{
Event[] events;
auto eventsString = readFileUTF8(fileName);
try
{
deserializeJson(events, eventsString.parseJsonString);
}
catch (std.json.JSONException)
{
}
return events;
}
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
public:
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
@method(HTTPMethod.POST) @path("/event/create")
void createEvent(string Ereignisname, string Ereignisort, string Von, string Bis)
{
Event event;
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
event.name = Ereignisname;
event.place = Ereignisort;
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
event.begin = DateTime.fromISOExtString(Von ~ ":00");
event.end = DateTime.fromISOExtString(Bis ~ ":00");
enforce(event.end - event.begin > 0.seconds);
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
auto events = getEventsFromFile(fileName);
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
events ~= event;
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
writeFileUTF8(fileName, events.serializeToJsonString());
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
render!("listevents.dt", events);
}
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
@method(HTTPMethod.GET) @path("create")
void newEvent()
{
render!("create.dt");
}
2017-08-02 01:56:36 +02:00
2017-08-05 17:31:35 +02:00
void index()
2017-08-02 01:56:36 +02:00
{
2017-08-05 17:31:35 +02:00
auto events = getEventsFromFile(fileName);
render!("listevents.dt", events);
2017-08-02 01:56:36 +02:00
}
2017-08-05 17:31:35 +02:00
2017-08-02 01:56:36 +02:00
}
2017-08-05 17:31:35 +02:00
shared static this()
2017-08-02 01:56:36 +02:00
{
2017-08-05 17:31:35 +02:00
auto router = new URLRouter;
router.registerWebInterface(new CalendarWebapp);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, router);
logInfo("Please open http://127.0.0.1:8080/ in your browser.");
2017-08-02 01:56:36 +02:00
}