From 576852cad54867b0a1dc6bac335fd9828edaff98 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 7 Aug 2017 00:01:02 +0200 Subject: [PATCH] Added some error handling --- public/calendarwebapp.css | 3 ++ source/app.d | 1 + source/calendarwebapp.d | 71 ++++++++++++++++++++++----------------- views/create.dt | 15 +++++++-- views/layout.dt | 1 + views/login.dt | 2 ++ views/navigation.dt | 4 ++- 7 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 public/calendarwebapp.css diff --git a/public/calendarwebapp.css b/public/calendarwebapp.css new file mode 100644 index 0000000..9fc7b1e --- /dev/null +++ b/public/calendarwebapp.css @@ -0,0 +1,3 @@ +.error { + color: red; +} \ No newline at end of file diff --git a/source/app.d b/source/app.d index 018aa57..9fb58aa 100644 --- a/source/app.d +++ b/source/app.d @@ -5,6 +5,7 @@ shared static this() { auto router = new URLRouter; router.registerWebInterface(new CalendarWebapp); + router.get("*", serveStaticFiles("public")); auto settings = new HTTPServerSettings; settings.port = 8080; diff --git a/source/calendarwebapp.d b/source/calendarwebapp.d index b773041..d31fa81 100644 --- a/source/calendarwebapp.d +++ b/source/calendarwebapp.d @@ -43,15 +43,49 @@ private: mixin PrivateAccessProxy; public: + @auth void index(string userName) + { + auto entries = getEntriesFromFile(fileName); + render!("showevents.dt", entries); + } - @auth @method(HTTPMethod.POST) @path("/event/create") - void createEvent(Date begin, Nullable!Date end, string description, - string name, EventType type, bool shout, string userName) + void getLogin(string _error = null) + { + render!("login.dt", _error); + } + + @errorDisplay!getLogin void postLogin(string username, string password) + { + import std.uuid : randomUUID; + + enforce(username == "foo" && password == "bar", "Benutzername oder Passwort ungültig"); + UserData d; + d.loggedIn = true; + d.name = username; + d.uuid = randomUUID.toString; + user = d; + redirect("/"); + } + + void getLogout() + { + terminateSession(); + redirect("/"); + } + + @auth void getCreate(string userName, ValidationErrorData _error = ValidationErrorData.init) + { + render!("create.dt", _error); + } + + @auth @errorDisplay!getCreate void postCreate(Date begin, Nullable!Date end, + string description, string name, EventType type, bool shout, string userName) { import std.array : split, replace; if (!end.isNull) - enforce(end - begin >= 1.days); + enforce(end - begin >= 1.days, + "Mehrtägige Ereignisse müssen mindestens einen Tag dauern"); auto entry = Entry(begin, end, Event("", name, description.replace("\r", "").split('\n'), type, shout)); @@ -61,33 +95,10 @@ public: render!("showevents.dt", entries); } - @auth @method(HTTPMethod.GET) @path("create") - void newEvent(string userName) + struct ValidationErrorData { - render!("create.dt"); + string msg; + string field; } - @auth void index(string userName) - { - auto entries = getEntriesFromFile(fileName); - render!("showevents.dt", entries); - } - - void getLogin() - { - render!("login.dt"); - } - - void postLogin(string username, string password) - { - import std.uuid : randomUUID; - - enforce(username == "foo" && password == "bar", "Invalid username / password"); - UserData d; - d.loggedIn = true; - d.name = username; - d.uuid = randomUUID.toString; - user = d; - redirect("/"); - } } diff --git a/views/create.dt b/views/create.dt index fa6ea05..313fac6 100644 --- a/views/create.dt +++ b/views/create.dt @@ -1,7 +1,10 @@ extends layout block content - form(action="/event/create", method="post") - fieldset(name="orderFields") + - void showerror(string field = null) + - if (_error.msg && _error.field == field) + td.error= _error.msg + form(action="/create", method="post") + fieldset(name="eventFields") table tbody#fieldTable tr @@ -9,21 +12,25 @@ block content label(for="name") Ereignisname td input#name(value="", name="name", type="text") + - showerror("name"); tr td label(for="description") Beschreibung td textarea#description(value="", name="description", cols="40", rows="5") + - showerror("description"); tr td label(for="begin") Von td input#begin(value="", name="begin", type="date") + - showerror("begin"); tr td label(for="end") Bis td input#end(value="", name="end", type="date") + - showerror("end"); tr td label(for="type") Typ @@ -34,13 +41,17 @@ block content option(value="FSI_Event") FSI Ereignis option(value="General_University_Event") Allgemeines Universitäts Ereignis option(value="Any") Sonstiges + - showerror("type"); tr td label(for="shout") Shout td input#shout(name="shout", type="checkbox") + - showerror("shout"); tfoot tr td(colspan="2") input#submitButton(type="submit", value="Ereignis erstellen") + - if (_error.msg && _error.field == "") + p.error= _error.msg \ No newline at end of file diff --git a/views/layout.dt b/views/layout.dt index 567b1ee..5cabeff 100644 --- a/views/layout.dt +++ b/views/layout.dt @@ -3,6 +3,7 @@ html head meta(charset='utf-8') title Calendar Webapp + link(rel='stylesheet', type='text/css', href='/calendarwebapp.css') body block navigation include navigation diff --git a/views/login.dt b/views/login.dt index cad8666..21799ed 100644 --- a/views/login.dt +++ b/views/login.dt @@ -19,3 +19,5 @@ block content tr td(colspan="2") input#submitButton(type="submit", value="Anmelden") + - if (_error) + p.error= _error diff --git a/views/navigation.dt b/views/navigation.dt index 6e1f87e..e7719aa 100644 --- a/views/navigation.dt +++ b/views/navigation.dt @@ -3,4 +3,6 @@ nav li a(href='/') Home li - a(href='/create') Create Event \ No newline at end of file + a(href='/create') Ereignis erstellen + li + a(href='/logout') Ausloggen \ No newline at end of file