2017-08-06 22:46:42 +02:00
|
|
|
module event;
|
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
import poodinis;
|
|
|
|
|
|
|
|
import std.algorithm : map;
|
|
|
|
import std.datetime.date : Date;
|
|
|
|
import std.range.interfaces : InputRange, inputRangeObject;
|
2017-08-06 22:46:42 +02:00
|
|
|
import std.typecons : Nullable;
|
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
import vibe.core.file : existsFile, readFileUTF8, writeFileUTF8;
|
2017-08-07 01:33:17 +02:00
|
|
|
import vibe.core.path : Path;
|
2017-08-08 01:05:11 +02:00
|
|
|
import vibe.data.bson : Bson, BsonObjectID, deserializeBson, serializeToBson;
|
2017-08-07 01:33:17 +02:00
|
|
|
import vibe.data.serialization : serializationName = name;
|
2017-08-08 01:05:11 +02:00
|
|
|
import vibe.db.mongo.client : MongoClient;
|
|
|
|
import vibe.db.mongo.collection : MongoCollection;
|
|
|
|
|
|
|
|
interface EventStore
|
|
|
|
{
|
|
|
|
Event getEvent(string id);
|
|
|
|
InputRange!Event getAllEvents();
|
|
|
|
void addEvent(Event);
|
|
|
|
InputRange!Event getEventsBeginningBetween(Date begin, Date end);
|
|
|
|
}
|
|
|
|
|
|
|
|
class MongoDBEventStore : EventStore
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Event getEvent(string id)
|
|
|
|
{
|
|
|
|
return mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName)
|
|
|
|
.findOne(["_id" : id]).deserializeBson!Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputRange!Event getAllEvents()
|
|
|
|
{
|
|
|
|
return mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName)
|
|
|
|
.find().map!(deserializeBson!Event).inputRangeObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEvent(Event event)
|
|
|
|
{
|
|
|
|
if (!event.id.valid)
|
|
|
|
event.id = BsonObjectID.generate;
|
|
|
|
|
|
|
|
mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName)
|
|
|
|
.insert(event.serializeToBson);
|
|
|
|
}
|
|
|
|
|
|
|
|
InputRange!Event getEventsBeginningBetween(Date begin, Date end)
|
|
|
|
{
|
|
|
|
return mongoClient.getCollection(databaseName ~ "." ~ entriesCollectionName)
|
|
|
|
.find(["$and" : [["date" : ["$gte" : begin.serializeToBson]], ["date"
|
|
|
|
: ["$lte" : end.serializeToBson]]]]).map!(deserializeBson!Event)
|
|
|
|
.inputRangeObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
@Autowire MongoClient mongoClient;
|
|
|
|
|
|
|
|
@Value("Database name")
|
|
|
|
string databaseName;
|
|
|
|
|
|
|
|
@Value("Entries collection name")
|
|
|
|
string entriesCollectionName;
|
|
|
|
}
|
2017-08-06 22:46:42 +02:00
|
|
|
|
|
|
|
enum EventType
|
|
|
|
{
|
|
|
|
Holiday,
|
|
|
|
Birthday,
|
|
|
|
FSI_Event,
|
|
|
|
General_University_Event,
|
|
|
|
Any
|
|
|
|
}
|
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
struct Event
|
2017-08-06 22:46:42 +02:00
|
|
|
{
|
2017-08-08 01:05:11 +02:00
|
|
|
@serializationName("_id") BsonObjectID id;
|
2017-08-07 01:33:17 +02:00
|
|
|
@serializationName("date") Date begin;
|
|
|
|
@serializationName("end_date") Nullable!Date end;
|
2017-08-06 22:46:42 +02:00
|
|
|
string name;
|
2017-08-07 01:33:17 +02:00
|
|
|
@serializationName("desc") string[] description;
|
|
|
|
@serializationName("etype") EventType type;
|
2017-08-06 22:46:42 +02:00
|
|
|
bool shout;
|
|
|
|
}
|