diff --git a/.gitignore b/.gitignore index 7bf6fdf..95e4ce0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,6 @@ __dummy.html *.o *.obj __test__*__ -calendar-webapp +calendar-webapp* events.json dub.selections.json \ No newline at end of file diff --git a/dub.json b/dub.json index 025223d..effc53a 100644 --- a/dub.json +++ b/dub.json @@ -5,7 +5,7 @@ ], "dependencies": { "vibe-d": "0.8.1-rc.2", - "poodinis": "~>8.0.0" + "poodinis": "~>8.0.1" }, "description": "A simple webapplication to edit and view calendar entries", "copyright": "Copyright © 2017, Johannes Loher", diff --git a/public/calendarwebapp.css b/public/calendarwebapp.css index 9fc7b1e..785d74b 100644 --- a/public/calendarwebapp.css +++ b/public/calendarwebapp.css @@ -1,3 +1,7 @@ .error { color: red; +} + +td.pre { + white-space: pre; } \ No newline at end of file diff --git a/source/app.d b/source/app.d index 07de544..ca618d1 100644 --- a/source/app.d +++ b/source/app.d @@ -1,15 +1,12 @@ module app; -import authenticator : Authenticator, MongoDBAuthenticator; import calendarwebapp : CalendarWebapp; -import configuration : StringInjector; -import event : EventStore, MongoDBEventStore; +import configuration : Context; import poodinis; import vibe.core.log : logInfo; -import vibe.db.mongo.client : MongoClient; -import vibe.db.mongo.mongo : connectMongoDB; + import vibe.http.fileserver : serveStaticFiles; import vibe.http.router : URLRouter; import vibe.http.server : HTTPServerSettings, listenHTTP, MemorySessionStore; @@ -17,16 +14,11 @@ import vibe.web.web : registerWebInterface; shared static this() { - auto dependencies = new shared DependencyContainer(); - auto db = connectMongoDB("localhost"); - dependencies.register!MongoClient.existingInstance(db); - dependencies.register!(EventStore, MongoDBEventStore); - dependencies.register!(Authenticator, MongoDBAuthenticator); - dependencies.register!CalendarWebapp; - dependencies.register!(ValueInjector!string, StringInjector); + auto container = new shared DependencyContainer(); + container.registerContext!Context; auto router = new URLRouter; - router.registerWebInterface(dependencies.resolve!CalendarWebapp); + router.registerWebInterface(container.resolve!CalendarWebapp); router.get("*", serveStaticFiles("public")); auto settings = new HTTPServerSettings; diff --git a/source/authenticator.d b/source/authenticator.d index 94d48f1..2e53f91 100644 --- a/source/authenticator.d +++ b/source/authenticator.d @@ -3,7 +3,7 @@ module authenticator; import poodinis; import vibe.data.bson : Bson; -import vibe.db.mongo.client : MongoClient; +import vibe.db.mongo.collection : MongoCollection; interface Authenticator { @@ -13,18 +13,12 @@ interface Authenticator class MongoDBAuthenticator : Authenticator { private: - @Autowire MongoClient mongoClient; - - @Value("Database name") - string databaseName; - - @Value("Users collection name") - string usersCollectionName; + @Value("users") + MongoCollection users; public: bool checkUser(string username, string password) @safe { - auto users = mongoClient.getCollection(databaseName ~ "." ~ usersCollectionName); auto result = users.findOne(["username" : username, "password" : password]); return result != Bson(null); } diff --git a/source/calendarwebapp.d b/source/calendarwebapp.d index e140e6f..651f5b6 100644 --- a/source/calendarwebapp.d +++ b/source/calendarwebapp.d @@ -72,7 +72,7 @@ public: enforce(end - begin >= 1.days, "Mehrtägige Ereignisse müssen mindestens einen Tag dauern"); auto event = Event(BsonObjectID.generate, begin, end, name, - description.replace("\r", "").split('\n'), type, shout); + description.replace("\r", ""), type, shout); eventStore.addEvent(event); diff --git a/source/configuration.d b/source/configuration.d index b35a584..fce925d 100644 --- a/source/configuration.d +++ b/source/configuration.d @@ -1,6 +1,29 @@ module configuration; -import poodinis : ValueInjector; +import authenticator : Authenticator, MongoDBAuthenticator; +import calendarwebapp : CalendarWebapp; +import event : EventStore, MongoDBEventStore; + +import poodinis; + +import vibe.db.mongo.client : MongoClient; +import vibe.db.mongo.collection : MongoCollection; +import vibe.db.mongo.mongo : connectMongoDB; + +class Context : ApplicationContext +{ +public: + override void registerDependencies(shared(DependencyContainer) container) + { + auto mongoClient = connectMongoDB("localhost"); + container.register!MongoClient.existingInstance(mongoClient); + container.register!(EventStore, MongoDBEventStore); + container.register!(Authenticator, MongoDBAuthenticator); + container.register!CalendarWebapp; + container.register!(ValueInjector!string, StringInjector); + container.register!(ValueInjector!MongoCollection, MongoCollectionInjector); + } +} class StringInjector : ValueInjector!string { @@ -13,12 +36,26 @@ public: // dfmt off config = ["Database name" : "CalendarWebapp", "Users collection name": "users", - "Entries collection name" : "entries"]; + "Events collection name" : "events"]; // dfmt on } - string get(string key) const @safe pure nothrow + override string get(string key) const @safe pure nothrow { return config[key]; } } + +class MongoCollectionInjector : ValueInjector!MongoCollection +{ +private: + @Autowire MongoClient mongoClient; + @Value("Database name") + string databaseName; + +public: + override MongoCollection get(string key) @safe + { + return mongoClient.getCollection(databaseName ~ "." ~ key); + } +} diff --git a/source/event.d b/source/event.d index 7678f1c..5655cb7 100644 --- a/source/event.d +++ b/source/event.d @@ -9,7 +9,7 @@ import std.typecons : Nullable; import vibe.data.bson : Bson, BsonObjectID, deserializeBson, serializeToBson; import vibe.data.serialization : serializationName = name; -import vibe.db.mongo.client : MongoClient; +import vibe.db.mongo.collection : MongoCollection; interface EventStore { @@ -25,14 +25,12 @@ class MongoDBEventStore : EventStore public: Event getEvent(BsonObjectID id) @safe { - return mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName) - .findOne(["_id" : id]).deserializeBson!Event; + return events.findOne(["_id" : id]).deserializeBson!Event; } InputRange!Event getAllEvents() @safe { - return mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName) - .find().map!(deserializeBson!Event).inputRangeObject; + return events.find().map!(deserializeBson!Event).inputRangeObject; } void addEvent(Event event) @safe @@ -40,31 +38,24 @@ public: if (!event.id.valid) event.id = BsonObjectID.generate; - mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName) - .insert(event.serializeToBson); + events.insert(event.serializeToBson); } InputRange!Event getEventsBeginningBetween(Date begin, Date end) @safe { - return mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName) - .find(["$and" : [["date" : ["$gte" : begin.serializeToBson]], ["date" + return events.find(["$and" : [["date" : ["$gte" : begin.serializeToBson]], ["date" : ["$lte" : end.serializeToBson]]]]).map!(deserializeBson!Event) .inputRangeObject; } void removeEvent(BsonObjectID id) @safe { - mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName).remove(["_id" : id]); + events.remove(["_id" : id]); } private: - @Autowire MongoClient mongoClient; - - @Value("Database name") - string databaseName; - - @Value("Entries collection name") - string entriesCollectionName; + @Value("events") + MongoCollection events; } enum EventType @@ -82,7 +73,7 @@ struct Event @serializationName("date") Date begin; @serializationName("end_date") Nullable!Date end; string name; - @serializationName("desc") string[] description; + @serializationName("desc") string description; @serializationName("etype") EventType type; bool shout; } diff --git a/views/create.dt b/views/create.dt index 313fac6..19f8e53 100644 --- a/views/create.dt +++ b/views/create.dt @@ -21,15 +21,15 @@ block content - showerror("description"); tr td - label(for="begin") Von + label(for="multiday") Mehrtägig + td + input#multiday(name="multiday", type="checkbox") + tr + td + label#beginLabel(for="begin") Datum 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 @@ -54,4 +54,20 @@ block content 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 + p.error= _error.msg + script(type="text/javascript"). + document.getElementById('multiday').onclick = function() { + var table = document.getElementById("fieldTable"); + var beginLabel = document.getElementById("beginLabel"); + if ( this.checked ) { + var row = table.insertRow(4); + var cell1 = row.insertCell(0); + var cell2 = row.insertCell(1); + cell1.innerHTML = ""; + cell2.innerHTML = ""; + beginLabel.innerHTML = "Von"; + } else { + table.deleteRow(4); + beginLabel.innerHTML = "Datum"; + } + }; \ No newline at end of file diff --git a/views/showevents.dt b/views/showevents.dt index e3a7877..8592840 100644 --- a/views/showevents.dt +++ b/views/showevents.dt @@ -17,7 +17,7 @@ block content td #{event.name} tr td desc - td #{event.description} + td.pre #{event.description} tr td etype td #{event.type}