61ba7564d2babe255f096048bfd6ce8a06e1ec1c
1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Feedback related features
21 # Group all api handlers in one router
22 class APIFeedbackRouter
25 # Model to pass to handlers
28 # Mainmodule to pass to handlers
29 var mainmodule
: MModule
31 # Mongo collection used to store ratings
32 var collection
: MongoCollection
35 use
("/stars/:id", new APIStars(model
, mainmodule
, collection
))
39 # Stars attributed to mentities
43 # Collection used to store ratings
44 var collection
: MongoCollection
46 redef fun get
(req
, res
) do
47 var mentity
= mentity_from_uri
(req
, res
)
48 if mentity
== null then
53 res
.json mentity_ratings
(mentity
)
56 redef fun post
(req
, res
) do
57 var mentity
= mentity_from_uri
(req
, res
)
58 if mentity
== null then
62 var obj
= req
.body
.parse_json
63 if not obj
isa JsonObject then
67 var rating
= obj
["rating"]
68 if not rating
isa Int then
73 var val
= new MEntityRating(mentity
.full_name
, rating
, get_time
)
74 collection
.insert
(val
.json
)
76 res
.json mentity_ratings
(mentity
)
79 # Get the ratings of a `mentity`
80 fun mentity_ratings
(mentity
: MEntity): MEntityRatings do
81 var ratings
= new MEntityRatings(mentity
)
83 var req
= new JsonObject
84 req
["mentity"] = mentity
.full_name
85 var rs
= collection
.find_all
(req
)
86 for r
in rs
do ratings
.ratings
.add
new MEntityRating.from_json
(r
)
91 # Ratings representation for a mentity
99 var ratings
= new Array[MEntityRating]
101 # Mean of all ratings or 0
103 if ratings
.is_empty
then return 0.0
105 for r
in ratings
do sum
+= r
.rating
.to_f
106 var res
= sum
/ ratings
.length
.to_f
110 # Json representation of `self`
111 fun json
: JsonObject do
112 var obj
= new JsonObject
113 obj
["mentity"] = mentity
.full_name
114 obj
["ratings"] = new JsonArray.from
(ratings
)
119 redef fun to_json
do return json
.to_json
122 # Rating value of a MEntity
126 # MEntity this rating is about
129 # Rating value (between 1 and 5)
132 # Timestamp of this rating
135 # Init this rating value from a JsonObject
136 init from_json
(obj
: JsonObject) do
137 init(obj
["mentity"].as(String), obj
["rating"].as(Int), obj
["timestamp"].as(Int))
140 # Translate this rating value to a JsonObject
141 fun json
: JsonObject do
142 var obj
= new JsonObject
143 obj
["mentity"] = mentity
144 obj
["rating"] = rating
145 obj
["timestamp"] = timestamp
149 redef fun to_json
do return json
.to_json