From 005ad3bf2ad4f4127a79fe5db94e72a76d7cce03 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Wed, 2 Aug 2017 01:56:36 +0200 Subject: [PATCH] initial commit --- dub.json | 15 ++++++++++ source/app.d | 71 +++++++++++++++++++++++++++++++++++++++++++++ views/create.dt | 31 ++++++++++++++++++++ views/layout.dt | 11 +++++++ views/listevents.dt | 18 ++++++++++++ views/navigation.dt | 6 ++++ 6 files changed, 152 insertions(+) create mode 100644 dub.json create mode 100644 source/app.d create mode 100644 views/create.dt create mode 100644 views/layout.dt create mode 100644 views/listevents.dt create mode 100644 views/navigation.dt diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..2e592e2 --- /dev/null +++ b/dub.json @@ -0,0 +1,15 @@ +{ + "name": "calendar-webapp", + "authors": [ + "Johannes Loher" + ], + "dependencies": { + "vibe-d": "~>0.7.30" + }, + "description": "A simple webapplication to edit and view calendar entries", + "copyright": "Copyright © 2017, Johannes Loher", + "license": "MIT", + "versions": [ + "VibeDefaultMain" + ] +} \ No newline at end of file diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..01c7404 --- /dev/null +++ b/source/app.d @@ -0,0 +1,71 @@ +import vibe.vibe; + +import std.datetime.date; + +shared static this() +{ + auto router = new URLRouter; + router.get("/", &listEvents); + router.get("/create", staticTemplate!"create.dt"); + router.get("/event/create", &createEvent); + router.post("/event/create", &createEvent); + router.get("*", serveStaticFiles("/public")); + + 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."); +} + +void listEvents(HTTPServerRequest req, HTTPServerResponse res) +{ + auto fileName = Path("events.json"); + + auto events = fileName.getEventsFromFile; + render!("listevents.dt", events)(res); +} + +void createEvent(HTTPServerRequest req, HTTPServerResponse res) +{ + if (req.method != HTTPMethod.POST && req.method != HTTPMethod.GET) + return; + auto formdata = (req.method == HTTPMethod.POST) ? &req.form : &req.query; + + auto fileName = Path("events.json"); + Event event; + + event.name = formdata.get("Ereignisname"); + event.place = formdata.get("Ereignisort"); + + event.begin = DateTime.fromISOExtString(formdata.get("Von") ~ ":00"); + event.end = DateTime.fromISOExtString(formdata.get("Bis") ~ ":00"); + + auto events = fileName.getEventsFromFile; + + events ~= event; + + writeFileUTF8(fileName, events.serializeToJsonString()); + + render!("listevents.dt", events)(res); +} + +Event[] getEventsFromFile(in Path fileName) +{ + Event[] events; + auto eventsString = readFileUTF8(fileName); + try + { + deserializeJson(events, eventsString.parseJsonString); + } + catch(std.json.JSONException) + {} + return events; +} + +struct Event +{ + string name, place; + DateTime begin, end; +} diff --git a/views/create.dt b/views/create.dt new file mode 100644 index 0000000..bd50cb2 --- /dev/null +++ b/views/create.dt @@ -0,0 +1,31 @@ +extends layout +block content + form(action="/event/create", method="post") + fieldset(name="orderFields") + table + tbody#fieldTable + tr + td + label(for="Ereignisname") Ereignisname + td + input#Ereignisname(value="", name="Ereignisname", type="text") + tr + td + label(for="Ereignisort") Ereignisort + td + input#Ereignisort(value="", name="Ereignisort", type="text") + tr + td + label(for="Von") Von + td + input#Von(value="", name="Von", type="datetime-local") + tr + td + label(for="Bis") Bis + td + input#Bis(value="", name="Bis", type="datetime-local") + + tfoot + tr + td(colspan="2") + input#submitButton(type="submit", value="Ereignis erstellen") diff --git a/views/layout.dt b/views/layout.dt new file mode 100644 index 0000000..567b1ee --- /dev/null +++ b/views/layout.dt @@ -0,0 +1,11 @@ +doctype html +html + head + meta(charset='utf-8') + title Calendar Webapp + body + block navigation + include navigation + block content + + diff --git a/views/listevents.dt b/views/listevents.dt new file mode 100644 index 0000000..759c9d5 --- /dev/null +++ b/views/listevents.dt @@ -0,0 +1,18 @@ +extends layout.dt +block content + h1 Events + - foreach(event; events) + table + tr + td Ereignisname + td #{event.name} + tr + td Ereignisort + td #{event.place} + tr + td Von + td #{event.begin} + tr + td Bis + td #{event.end} + hr diff --git a/views/navigation.dt b/views/navigation.dt new file mode 100644 index 0000000..6e1f87e --- /dev/null +++ b/views/navigation.dt @@ -0,0 +1,6 @@ +nav + ul + li + a(href='/') Home + li + a(href='/create') Create Event \ No newline at end of file