Added some error handling

This commit is contained in:
Johannes Loher 2017-08-07 00:01:02 +02:00
parent c509a166d8
commit 576852cad5
7 changed files with 64 additions and 33 deletions

View file

@ -0,0 +1,3 @@
.error {
color: red;
}

View file

@ -5,6 +5,7 @@ shared static this()
{ {
auto router = new URLRouter; auto router = new URLRouter;
router.registerWebInterface(new CalendarWebapp); router.registerWebInterface(new CalendarWebapp);
router.get("*", serveStaticFiles("public"));
auto settings = new HTTPServerSettings; auto settings = new HTTPServerSettings;
settings.port = 8080; settings.port = 8080;

View file

@ -43,15 +43,49 @@ private:
mixin PrivateAccessProxy; mixin PrivateAccessProxy;
public: public:
@auth void index(string userName)
{
auto entries = getEntriesFromFile(fileName);
render!("showevents.dt", entries);
}
@auth @method(HTTPMethod.POST) @path("/event/create") void getLogin(string _error = null)
void createEvent(Date begin, Nullable!Date end, string description, {
string name, EventType type, bool shout, string userName) 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; import std.array : split, replace;
if (!end.isNull) 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, auto entry = Entry(begin, end, Event("", name,
description.replace("\r", "").split('\n'), type, shout)); description.replace("\r", "").split('\n'), type, shout));
@ -61,33 +95,10 @@ public:
render!("showevents.dt", entries); render!("showevents.dt", entries);
} }
@auth @method(HTTPMethod.GET) @path("create") struct ValidationErrorData
void newEvent(string userName)
{ {
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("/");
}
} }

View file

@ -1,7 +1,10 @@
extends layout extends layout
block content block content
form(action="/event/create", method="post") - void showerror(string field = null)
fieldset(name="orderFields") - if (_error.msg && _error.field == field)
td.error= _error.msg
form(action="/create", method="post")
fieldset(name="eventFields")
table table
tbody#fieldTable tbody#fieldTable
tr tr
@ -9,21 +12,25 @@ block content
label(for="name") Ereignisname label(for="name") Ereignisname
td td
input#name(value="", name="name", type="text") input#name(value="", name="name", type="text")
- showerror("name");
tr tr
td td
label(for="description") Beschreibung label(for="description") Beschreibung
td td
textarea#description(value="", name="description", cols="40", rows="5") textarea#description(value="", name="description", cols="40", rows="5")
- showerror("description");
tr tr
td td
label(for="begin") Von label(for="begin") Von
td td
input#begin(value="", name="begin", type="date") input#begin(value="", name="begin", type="date")
- showerror("begin");
tr tr
td td
label(for="end") Bis label(for="end") Bis
td td
input#end(value="", name="end", type="date") input#end(value="", name="end", type="date")
- showerror("end");
tr tr
td td
label(for="type") Typ label(for="type") Typ
@ -34,13 +41,17 @@ block content
option(value="FSI_Event") FSI Ereignis option(value="FSI_Event") FSI Ereignis
option(value="General_University_Event") Allgemeines Universitäts Ereignis option(value="General_University_Event") Allgemeines Universitäts Ereignis
option(value="Any") Sonstiges option(value="Any") Sonstiges
- showerror("type");
tr tr
td td
label(for="shout") Shout label(for="shout") Shout
td td
input#shout(name="shout", type="checkbox") input#shout(name="shout", type="checkbox")
- showerror("shout");
tfoot tfoot
tr tr
td(colspan="2") td(colspan="2")
input#submitButton(type="submit", value="Ereignis erstellen") input#submitButton(type="submit", value="Ereignis erstellen")
- if (_error.msg && _error.field == "")
p.error= _error.msg

View file

@ -3,6 +3,7 @@ html
head head
meta(charset='utf-8') meta(charset='utf-8')
title Calendar Webapp title Calendar Webapp
link(rel='stylesheet', type='text/css', href='/calendarwebapp.css')
body body
block navigation block navigation
include navigation include navigation

View file

@ -19,3 +19,5 @@ block content
tr tr
td(colspan="2") td(colspan="2")
input#submitButton(type="submit", value="Anmelden") input#submitButton(type="submit", value="Anmelden")
- if (_error)
p.error= _error

View file

@ -3,4 +3,6 @@ nav
li li
a(href='/') Home a(href='/') Home
li li
a(href='/create') Create Event a(href='/create') Ereignis erstellen
li
a(href='/logout') Ausloggen