nitweb: use config file
[nit.git] / src / web / api_feedback.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Feedback related features
16 module api_feedback
17
18 import web_base
19 import mongodb
20
21 redef class NitwebConfig
22
23 # MongoDB uri used for data persistence.
24 #
25 # * key: `mongo.uri`
26 # * default: `mongodb://localhost:27017/`
27 var mongo_uri: String is lazy do
28 return value_or_default("mongo.uri", "mongodb://localhost:27017/")
29 end
30
31 # MongoDB DB used for data persistence.
32 #
33 # * key: `mongo.db`
34 # * default: `nitweb`
35 var mongo_db: String is lazy do return value_or_default("mongo.db", "nitweb")
36
37 # Mongo instance
38 var mongo: MongoClient is lazy do return new MongoClient(mongo_uri)
39
40 # Database instance
41 var db: MongoDb is lazy do return mongo.database(mongo_db)
42
43 # MongoDB collection used to store stars.
44 var stars: MongoCollection is lazy do return db.collection("stars")
45 end
46
47 # Group all api handlers in one router
48 class APIFeedbackRouter
49 super APIRouter
50
51 init do
52 use("/stars/:id", new APIStars(config))
53 end
54 end
55
56 # Stars attributed to mentities
57 class APIStars
58 super APIHandler
59
60 redef fun get(req, res) do
61 var mentity = mentity_from_uri(req, res)
62 if mentity == null then
63 res.error 404
64 return
65 end
66
67 res.json mentity_ratings(mentity)
68 end
69
70 redef fun post(req, res) do
71 var mentity = mentity_from_uri(req, res)
72 if mentity == null then
73 res.error 404
74 return
75 end
76 var obj = req.body.parse_json
77 if not obj isa JsonObject then
78 res.error 400
79 return
80 end
81 var rating = obj["rating"]
82 if not rating isa Int then
83 res.error 400
84 return
85 end
86
87 var val = new MEntityRating(mentity.full_name, rating, get_time)
88 config.stars.insert(val.json)
89
90 res.json mentity_ratings(mentity)
91 end
92
93 # Get the ratings of a `mentity`
94 fun mentity_ratings(mentity: MEntity): MEntityRatings do
95 var ratings = new MEntityRatings(mentity)
96
97 var req = new JsonObject
98 req["mentity"] = mentity.full_name
99 var rs = config.stars.find_all(req)
100 for r in rs do ratings.ratings.add new MEntityRating.from_json(r)
101 return ratings
102 end
103 end
104
105 # Ratings representation for a mentity
106 class MEntityRatings
107 super Jsonable
108
109 # MEntity rated
110 var mentity: MEntity
111
112 # List of ratings
113 var ratings = new Array[MEntityRating]
114
115 # Mean of all ratings or 0
116 fun mean: Float do
117 if ratings.is_empty then return 0.0
118 var sum = 0.0
119 for r in ratings do sum += r.rating.to_f
120 var res = sum / ratings.length.to_f
121 return res
122 end
123
124 # Json representation of `self`
125 fun json: JsonObject do
126 var obj = new JsonObject
127 obj["mentity"] = mentity.full_name
128 obj["ratings"] = new JsonArray.from(ratings)
129 obj["mean"] = mean
130 return obj
131 end
132
133 redef fun to_json do return json.to_json
134 end
135
136 # Rating value of a MEntity
137 class MEntityRating
138 super Jsonable
139
140 # MEntity this rating is about
141 var mentity: String
142
143 # Rating value (between 1 and 5)
144 var rating: Int
145
146 # Timestamp of this rating
147 var timestamp: Int
148
149 # Init this rating value from a JsonObject
150 init from_json(obj: JsonObject) do
151 init(obj["mentity"].as(String), obj["rating"].as(Int), obj["timestamp"].as(Int))
152 end
153
154 # Translate this rating value to a JsonObject
155 fun json: JsonObject do
156 var obj = new JsonObject
157 obj["mentity"] = mentity
158 obj["rating"] = rating
159 obj["timestamp"] = timestamp
160 return obj
161 end
162
163 redef fun to_json do return json.to_json
164 end