use the webframework
This commit is contained in:
parent
88cf802e4b
commit
6cf7d27115
1 changed files with 64 additions and 57 deletions
121
source/app.d
121
source/app.d
|
@ -1,15 +1,73 @@
|
|||
import std.datetime.date;
|
||||
|
||||
import vibe.vibe;
|
||||
|
||||
import std.datetime.date;
|
||||
class CalendarWebapp
|
||||
{
|
||||
private:
|
||||
immutable fileName = Path("events.json");
|
||||
|
||||
struct Event
|
||||
{
|
||||
string name, place;
|
||||
DateTime begin, end;
|
||||
}
|
||||
|
||||
Event[] getEventsFromFile(in Path fileName)
|
||||
{
|
||||
Event[] events;
|
||||
auto eventsString = readFileUTF8(fileName);
|
||||
try
|
||||
{
|
||||
deserializeJson(events, eventsString.parseJsonString);
|
||||
}
|
||||
catch (std.json.JSONException)
|
||||
{
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@method(HTTPMethod.POST) @path("/event/create")
|
||||
void createEvent(string Ereignisname, string Ereignisort, string Von, string Bis)
|
||||
{
|
||||
Event event;
|
||||
|
||||
event.name = Ereignisname;
|
||||
event.place = Ereignisort;
|
||||
|
||||
event.begin = DateTime.fromISOExtString(Von ~ ":00");
|
||||
event.end = DateTime.fromISOExtString(Bis ~ ":00");
|
||||
enforce(event.end - event.begin > 0.seconds);
|
||||
|
||||
auto events = getEventsFromFile(fileName);
|
||||
|
||||
events ~= event;
|
||||
|
||||
writeFileUTF8(fileName, events.serializeToJsonString());
|
||||
|
||||
render!("listevents.dt", events);
|
||||
}
|
||||
|
||||
@method(HTTPMethod.GET) @path("create")
|
||||
void newEvent()
|
||||
{
|
||||
render!("create.dt");
|
||||
}
|
||||
|
||||
void index()
|
||||
{
|
||||
auto events = getEventsFromFile(fileName);
|
||||
render!("listevents.dt", events);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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"));
|
||||
router.registerWebInterface(new CalendarWebapp);
|
||||
|
||||
auto settings = new HTTPServerSettings;
|
||||
settings.port = 8080;
|
||||
|
@ -18,54 +76,3 @@ shared static this()
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue