1 module vibelog.config;
2 
3 import vibe.data.bson;
4 
5 final class Config {
6 	BsonObjectID id;
7 	string name;
8 	string[] categories;
9 	string language = "en-us";
10 	string copyrightString;
11 	string mailServer;
12 	string feedTitle;
13 	string feedLink;
14 	string feedDescription;
15 	string feedImageTitle;
16 	string feedImageUrl;
17 
18 	this()
19 	{
20 		id  = BsonObjectID.generate();
21 	}
22 
23 	@property string[] groups() const { return ["admin"]; }
24 
25 	bool hasCategory(string cat) const {
26 		foreach( c; categories )
27 			if( c == cat )
28 				return true;
29 		return false;
30 	}
31 
32 	static Config fromBson(Bson bson)
33 	{
34 		auto ret = new Config;
35 		ret.id = bson["_id"].opt!BsonObjectID();
36 		ret.name = bson["name"].opt!string();
37 		foreach( grp; cast(Bson[])bson["categories"] )
38 			ret.categories ~= grp.opt!string();
39 		ret.language = bson["language"].opt!string(language.init);
40 		ret.copyrightString = bson["copyrightString"].opt!string();
41 		ret.mailServer = bson["mailServer"].opt!string();
42 		ret.feedTitle = bson["feedTitle"].opt!string();
43 		ret.feedLink = bson["feedLink"].opt!string();
44 		ret.feedDescription = bson["feedDescription"].opt!string();
45 		ret.feedImageTitle = bson["feedImageTitle"].opt!string();
46 		ret.feedImageUrl = bson["feedImageUrl"].opt!string();
47 		return ret;
48 	}
49 
50 	Bson toBson()
51 	const {
52 		Bson[] bcategories;
53 		foreach( grp; categories )
54 			bcategories ~= Bson(grp);
55 
56 		// Create a default category if none is specified
57 		if(bcategories.length < 1)
58 		{
59 			bcategories ~= Bson("general");
60 		}
61 
62 		// Could use a switch here
63 		Bson[string] ret;
64 		ret["_id"] = Bson(id);
65 		ret["name"] = Bson(name);
66 		ret["categories"] = Bson(bcategories);
67 		ret["language"] = Bson(language);
68 		ret["copyrightString"] = Bson(copyrightString);
69 		ret["mailServer"] = Bson(mailServer);
70 		ret["feedTitle"] = Bson(feedTitle);
71 		ret["feedLink"] = Bson(feedLink);
72 		ret["feedDescription"] = Bson(feedDescription);
73 		ret["feedImageTitle"] = Bson(feedImageTitle);
74 		ret["feedImageUrl"] = Bson(feedImageUrl);
75 
76 		return Bson(ret);
77 	}
78 }