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;
router.registerWebInterface(new CalendarWebapp);
router.get("*", serveStaticFiles("public"));
auto settings = new HTTPServerSettings;
settings.port = 8080;

View file

@ -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("/");
}
}

View file

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

View file

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

View file

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

View file

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