nitweb: split APIRouter in modules
[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
20 redef class NitwebConfig
21
22 # MongoDB collection used to store stars.
23 var stars: MongoCollection is lazy do return db.collection("stars")
24 end
25
26 redef class APIRouter
27 redef init do
28 super
29 use("/feedback/stars/:id", new APIStars(config))
30 end
31 end
32
33 # Stars attributed to mentities
34 class APIStars
35 super APIHandler
36
37 redef fun get(req, res) do
38 var mentity = mentity_from_uri(req, res)
39 if mentity == null then
40 res.error 404
41 return
42 end
43
44 res.json mentity_ratings(mentity)
45 end
46
47 redef fun post(req, res) do
48 var mentity = mentity_from_uri(req, res)
49 if mentity == null then
50 res.error 404
51 return
52 end
53 var obj = req.body.parse_json
54 if not obj isa JsonObject then
55 res.error 400
56 return
57 end
58 var rating = obj["rating"]
59 if not rating isa Int then
60 res.error 400
61 return
62 end
63
64 var val = new MEntityRating(mentity.full_name, rating, get_time)
65 config.stars.insert(val.json)
66
67 res.json mentity_ratings(mentity)
68 end
69
70 # Get the ratings of a `mentity`
71 fun mentity_ratings(mentity: MEntity): MEntityRatings do
72 var ratings = new MEntityRatings(mentity)
73
74 var req = new JsonObject
75 req["mentity"] = mentity.full_name
76 var rs = config.stars.find_all(req)
77 for r in rs do ratings.ratings.add new MEntityRating.from_json(r)
78 return ratings
79 end
80 end
81
82 # Ratings representation for a mentity
83 class MEntityRatings
84 super Jsonable
85
86 # MEntity rated
87 var mentity: MEntity
88
89 # List of ratings
90 var ratings = new Array[MEntityRating]
91
92 # Mean of all ratings or 0
93 fun mean: Float do
94 if ratings.is_empty then return 0.0
95 var sum = 0.0
96 for r in ratings do sum += r.rating.to_f
97 var res = sum / ratings.length.to_f
98 return res
99 end
100
101 # Json representation of `self`
102 fun json: JsonObject do
103 var obj = new JsonObject
104 obj["mentity"] = mentity.full_name
105 obj["ratings"] = new JsonArray.from(ratings)
106 obj["mean"] = mean
107 return obj
108 end
109
110 redef fun to_json do return json.to_json
111 end
112
113 # Rating value of a MEntity
114 class MEntityRating
115 super Jsonable
116
117 # MEntity this rating is about
118 var mentity: String
119
120 # Rating value (between 1 and 5)
121 var rating: Int
122
123 # Timestamp of this rating
124 var timestamp: Int
125
126 # Init this rating value from a JsonObject
127 init from_json(obj: JsonObject) do
128 init(obj["mentity"].as(String), obj["rating"].as(Int), obj["timestamp"].as(Int))
129 end
130
131 # Translate this rating value to a JsonObject
132 fun json: JsonObject do
133 var obj = new JsonObject
134 obj["mentity"] = mentity
135 obj["rating"] = rating
136 obj["timestamp"] = timestamp
137 return obj
138 end
139
140 redef fun to_json do return json.to_json
141 end