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 redef class NitwebConfig
23 # MongoDB uri used for data persistence.
26 # * default: `mongodb://localhost:27017/`
27 var mongo_uri
: String is lazy
do
28 return value_or_default
("mongo.uri", "mongodb://localhost:27017/")
31 # MongoDB DB used for data persistence.
35 var mongo_db
: String is lazy
do return value_or_default
("mongo.db", "nitweb")
38 var mongo
: MongoClient is lazy
do return new MongoClient(mongo_uri
)
41 var db
: MongoDb is lazy
do return mongo
.database
(mongo_db
)
43 # MongoDB collection used to store stars.
44 var stars
: MongoCollection is lazy
do return db
.collection
("stars")
47 # Group all api handlers in one router
48 class APIFeedbackRouter
52 use
("/stars/:id", new APIStars(config
))
56 # Stars attributed to mentities
60 redef fun get
(req
, res
) do
61 var mentity
= mentity_from_uri
(req
, res
)
62 if mentity
== null then
67 res
.json mentity_ratings
(mentity
)
70 redef fun post
(req
, res
) do
71 var mentity
= mentity_from_uri
(req
, res
)
72 if mentity
== null then
76 var obj
= req
.body
.parse_json
77 if not obj
isa JsonObject then
81 var rating
= obj
["rating"]
82 if not rating
isa Int then
87 var val
= new MEntityRating(mentity
.full_name
, rating
, get_time
)
88 config
.stars
.insert
(val
.json
)
90 res
.json mentity_ratings
(mentity
)
93 # Get the ratings of a `mentity`
94 fun mentity_ratings
(mentity
: MEntity): MEntityRatings do
95 var ratings
= new MEntityRatings(mentity
)
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
)
105 # Ratings representation for a mentity
113 var ratings
= new Array[MEntityRating]
115 # Mean of all ratings or 0
117 if ratings
.is_empty
then return 0.0
119 for r
in ratings
do sum
+= r
.rating
.to_f
120 var res
= sum
/ ratings
.length
.to_f
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
)
133 redef fun to_json
do return json
.to_json
136 # Rating value of a MEntity
140 # MEntity this rating is about
143 # Rating value (between 1 and 5)
146 # Timestamp of this rating
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))
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
163 redef fun to_json
do return json
.to_json