initial commit

This commit is contained in:
Johannes Loher 2017-08-02 01:56:36 +02:00
commit 005ad3bf2a
6 changed files with 152 additions and 0 deletions

15
dub.json Normal file
View file

@ -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"
]
}

71
source/app.d Normal file
View file

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

31
views/create.dt Normal file
View file

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

11
views/layout.dt Normal file
View file

@ -0,0 +1,11 @@
doctype html
html
head
meta(charset='utf-8')
title Calendar Webapp
body
block navigation
include navigation
block content

18
views/listevents.dt Normal file
View file

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

6
views/navigation.dt Normal file
View file

@ -0,0 +1,6 @@
nav
ul
li
a(href='/') Home
li
a(href='/create') Create Event