forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commentModule.js
328 lines (304 loc) · 9.54 KB
/
commentModule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
function commentModule(){
var that = this;
var database = require('./database');
/**
* Retrieves a comment via its id from the database and executes the callback function on it
*/
that.findComment = function(id, callback){
database.connection.query("SELECT *,FROM_UNIXTIME(UNIX_TIMESTAMP(comment.timestamp),'%d %b %Y') AS time FROM comment WHERE id=?", [id], function(err, rows, fields){
if(!err){
for(var i=0; i<rows.length; i++){
var comment = {
id: rows[i].id,
user: rows[i].userID,
venue: rows[i].venueID,
photo: rows[i].photoID,
text: rows[i].text,
time: rows[i].time
}
callback(comment);
return;
}
} else {
console.log("Error querying DB for comment");
}
callback(null);
});
};
/**
* Retrieves all comments that belong to a venue from the database and executes the callback function on them
*/
that.findCommentsByVenue = function(venueId, callback){
database.connection.query("SELECT comment.*,users.username,FROM_UNIXTIME(UNIX_TIMESTAMP(comment.timestamp),'%d %b %Y') AS time FROM comment LEFT JOIN users ON (comment.userID = users.id) WHERE venueID=? ORDER BY score DESC", [venueId], function(err, rows, fields){
if(!err){
var comments = [];
for(var i=0; i<rows.length; i++){
var comment = {
id: rows[i].id,
userID: rows[i].userID,
author: rows[i].username,
//venue: rows[i].venueID,
//photo: rows[i].photoID,
text: rows[i].text,
score: rows[i].score,
time: rows[i].time
}
comments.push(comment);
}
callback(comments);
return;
} else {
console.log("Error querying DB for venue comments");
}
callback(null);
});
};
/**
* Retrieves all comments that belong to a user from the database and executes the callback function on them
*/
that.findCommentsByUser = function(userId, callback){
database.connection.query("SELECT * FROM comment WHERE userID=?", [userId], function(err, rows, fields){
if(!err){
var comments = [];
for(var i=0; i<rows.length; i++){
var comment = {
id: rows[i].id,
user: rows[i].userID,
venue: rows[i].venueID,
photo: rows[i].photoID,
text: rows[i].text,
timestamp: rows[i].timestamp
}
comments.push(comment);
}
callback(comments);
return;
} else {
console.log("Error querying DB for user comments");
}
callback(null);
});
};
/**
* Inserts a comment into the database and executes the callback function on the comment and its new id
*/
that.saveComment = function(comment, callback){
database.connection.query("INSERT INTO comment SET userID=?, venueID=?, photoID=?, text=?",
[comment.user, comment.venue, comment.photo, comment.text], function(err, result){
if(!err){
comment.id = result.insertId;
console.log("Inserted comment into DB: " + result.insertId);
callback(comment);
} else {
console.log("Error trying to insert comment into database");
callback(null);
}
});
};
/**
* Updates the rating of a comment defined by its id and executes the callback function on the id
*/
that.updateCommentRating = function(id, rating, callback){
database.connection.query("UPDATE comment SET score=? WHERE id=?", [rating, id], function(err, result){
if(!err){
console.log("Updated comment: " + id);
callback(id);
} else {
console.log("Error updating comment score");
callback(null);
}
});
};
/**
* Removes the comment defined by its id from the database and executes the callback function with status "OK" if successful
*/
that.removeComment = function(id, callback){
database.connection.query("DELETE FROM comment WHERE id=?", [id], function(err, rows, fields){
if(!err){
console.log("Deleted comment " + id);
callback("OK");
return;
} else {
console.log("Error deleting comment from DB");
}
callback(null);
});
};
/**
* Retrieves the sum of all user ratings for a comment defined by its id from the database and executes the callback function on it
*/
that.getScoreForComment = function(id, callback){
database.connection.query("SELECT SUM(rating) AS score FROM comment_rating WHERE comment=?", [id], function(err, rows, fields){
if(!err){
for(var i=0; i<rows.length; i++){
console.log("Score for comment " + id + " is " + rows[i].score);
callback(rows[i].score);
return;
}
} else {
console.log("Error querying DB for comment score");
}
callback(null);
});
};
/**
* Retrieves a user rating for a comment from the database and executes the callback function on it
*/
that.findRating = function(comment, user, callback){
database.connection.query("SELECT * FROM comment_rating WHERE comment=? AND user=?", [comment, user], function(err, rows, fields){
if(!err){
for(var i=0; i<rows.length; i++){
var rating = {
user: rows[i].user,
comment: rows[i].comment,
rating: rows[i].rating,
timestamp: rows[i].timestamp
}
callback(rating);
return;
}
} else {
console.log("Error querying DB for comment rating");
}
callback(null);
});
};
/**
* Inserts a user rating for a comment into the database and executes the callback function on it
*/
that.insertRating = function(rating, callback){
database.connection.query("INSERT INTO comment_rating SET comment=?, rating=?, user=?",
[rating.comment, rating.rating, rating.user], function(err, result){
if(!err){
console.log("Inserted comment rating into DB");
callback(rating);
} else {
console.log("Error trying to insert comment rating into database");
callback(null);
}
});
};
/**
* Sets the user rating for a venue to the new value and executes the callback function on the rating
*/
that.updateRating = function(rating, callback){
database.connection.query("UPDATE comment_rating SET rating=? WHERE comment=? AND user=?",
[rating.rating, rating.comment, rating.user], function(err, result){
if(!err){
console.log("Updated comment rating");
callback(rating);
} else {
console.log("Error updating comment rating");
callback(null);
}
});
};
/**
* Removes a user rating for a comment from the database and executes the callback function with status "OK" if successful
*/
that.removeRating = function(comment, user, callback){
database.connection.query("DELETE FROM comment_rating WHERE comment=? AND user=?", [comment, user], function(err, rows, fields){
if(!err){
console.log("Deleted comment rating");
callback("OK");
return;
} else {
console.log("Error deleting comment rating from DB");
}
callback(null);
});
};
/**
* Handles a POST comment request, i.e. inserts a comment into the database and sends a status response to the mobile app
* req must contain comment and venueid
*/
that.postComment = function(req, res, next){
if(req.user && req.user.id && req.user.username){
if(!req.body.hasOwnProperty('comment') || !req.body.hasOwnProperty('venueid')){
res.send(400, {error: "No comment text or venue specified"});
} else {
var comment = {
user: req.user.id,
venue: req.body.venueid,
text: req.body.comment,
photo: null
};
that.saveComment(comment, function(comment){
console.log("Comment added: id "+comment.id);
res.send(201, {error: "false"});
});
}
} else {
res.send(403, {error: "You are not signed in"});
}
return next();
};
/**
* Handles a DELETE comment request and removes a comment from the database if the user has written that comment
* req.params must contain id
*/
that.delComment = function(req, res, next){
if(req.user && req.user.id){
that.findComment(req.params.id, function(comment){
if(!comment){
res.send(404, {error: "This comment does not exist"});
} else {
if(comment.user == req.user.id){
that.removeComment(req.params.id, function(result){
if(!result){
res.send(404, {error: "This comment does not exist"});
} else {
res.send(200, {error: "false"});
}
});
} else {
res.send(403, {error: "This is not your comment"});
}
}
});
} else {
res.send(403, {error: "You are not signed in"});
}
return next();
};
/**
* Handles a PUT comment rating request and inserts/updates the user's rating for the comment and updates the rating score for the comment
* req.params must contain id (the comment id)
* req.body must contain rating
*/
that.rateComment = function(req, res, next){
if(req.user && req.user.id){
that.findRating(req.params.id, req.user.id, function(rating){
if(!rating){
that.insertRating({comment: req.params.id, rating: req.body.rating, user: req.user.id}, function(rating){
that.getScoreForComment(req.params.id, function(score){
that.updateCommentRating(req.params.id, score, function(comment){
if(!comment){
res.send(404, {error: "This comment does not exist"});
} else {
res.send(200, {error: "false"});
}
});
});
});
} else {
that.updateRating({comment: req.params.id, rating: req.body.rating, user: req.user.id}, function(rating){
that.getScoreForComment(req.params.id, function(score){
that.updateCommentRating(req.params.id, score, function(comment){
if(!comment){
res.send(404, {error: "This comment does not exist"});
} else {
res.send(200, {error: "false"});
}
});
});
});
}
});
} else {
res.send(403, {error: "You are not signed in"});
}
return next();
};
}
module.exports = new commentModule();