-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
480 lines (419 loc) · 12.2 KB
/
controller.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// get the list of all events in BDECalendar with all posts associated to each one
/*exports.getAllEvents = function (req, res) {
console.log("getAllEvents");
//get all events
req.database.query('SELECT e.id as id, e.description as content, e.start as date, e.organisation as author, e.title as title FROM BDECalendar AS e ORDER BY e.start DESC;', (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
events: []
};
if (result.length > 0) {
for (let i in result) {
console.log(result);
console.log(result[i]);
var event={
id: result[i]['id'],
date: result[i]['date'],
title: result[i]['title'],
content: result[i]['content'],
author: result[i]['author'],
posts: []
};
data.events.push(event);
}
// get all posts
req.database.query('SELECT p.id as id, p.content as content, p.date as date, p.author as author, p.eventid as eventid FROM AQHposts AS p ORDER BY p.date DESC;', (errorQuery, resultQuery) => {
if (errorQuery) {
req.logger.error(errorQuery);
res.sendStatus(500);
} else {
//console.log(resultQuery.length)
if (resultQuery.length > 0) {
var posts = [];
for (r of resultQuery) {
var post={
id: r['id'],
content: r['content'],
date: r['date'],
author: r['author'],
eventid: r['eventid']
};
console.log(post);
// associate the post to the event
if (data.events[data.events.length - r['eventid']])
data.events[data.events.length - r['eventid']].posts.push(post);
}
}
}
console.log("data1:");
console.log(data);
console.log("end");
// render the html page sent to the client from ejs file
req.engines['ejs'].renderFile('modules/aqh/body/home.ejs', data, (errorEjs, resultEjs) => {
if (errorEjs) {
req.logger.error(errorEjs);
res.sendStatus(500);
} else {
console.log(resultEjs);
res.send(resultEjs);
}
});
});
}
}
});
};
*/
exports.getAllEvents = function (req, res) {
console.log("getAdminPage");
//get all events
// req.database.query('SELECT e.id as id, e.description as content, e.start as date, e.organisationid as author, e.title as title FROM BDECalendar AS e ORDER BY e.start DESC;', (error, result) => {
req.database.query('SELECT e.id as id, e.description as content, e.start as date, e.organisationid as author, e.title as title, m.id_user as miduser FROM BDECalendar AS e JOIN core_membership AS m ON e.organisationid=m.id_group ORDER BY e.start DESC;', (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
events: []
};
if (result.length > 0) {
for (let i in result) {
// console.log(result);
//console.log(result[i]);
var admin = (result[i]['miduser'] == req.user.id);
var event={
id: result[i]['id'],
date: result[i]['date'],
title: result[i]['title'],
content: result[i]['content'],
author: result[i]['author'],
admin: admin,
posts: []
};
var add=true;
for (var j in data.events) {
if (data.events[j].id==event.id) {
add=false;
if (!data.events[j].admin && admin) {
data.events[j].admin=true;
}
}
}
if (add) {
data.events.push(event);
}
}
/*req.database.query('SELECT * FROM core_membership AS m WHERE id_user=?;', [req.user.id], (errorm, resultm) => {
if (errorm) {
req.logger.error(errorm);
res.sendStatus(500);
} else {
if (resultm.length > 0) {
for (let i in resultm) {
if (resultm[i]['id_group']==event.author) {
event.
}
}
}
*/
// get all posts
req.database.query('SELECT p.id as id, p.content as content, p.date as date, p.author as author, p.eventid as eventid FROM AQHposts AS p ORDER BY p.date DESC;', (errorQuery, resultQuery) => {
if (errorQuery) {
req.logger.error(errorQuery);
res.sendStatus(500);
} else {
//console.log(resultQuery.length)
if (resultQuery.length > 0) {
var posts = [];
for (r of resultQuery) {
var post={
id: r['id'],
content: r['content'],
date: r['date'],
author: r['author'],
eventid: r['eventid']
};
// console.log(post);
// associate the post to the event
for (var i in data.events) {
if (data.events[i].id == r['eventid']) {
data.events[i].posts.push(post);
break;
}
}
// if (data.events[data.events.length - r['eventid']])
// data.events[data.events.length - r['eventid']].posts.push(post);
}
}
}
// console.log("data1:");
// console.log(data);
//console.log("end");
// render the html page sent to the client from ejs file
req.engines['ejs'].renderFile('modules/aqh/body/home.ejs', data, (errorEjs, resultEjs) => {
if (errorEjs) {
req.logger.error(errorEjs);
res.sendStatus(500);
} else {
//console.log(resultEjs);
res.send(resultEjs);
}
});
});
// }
//});
}
}
});
};
exports.getOne = function (req, res) {
req.database.query('SELECT p.id as id, p.content as content, p.date as date, u.nick as author FROM AQHposts AS p JOIN core_user AS u ON u.id = p.author WHERE p.id = ?;', [req.query.id], (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
if (result.length > 0) {
console.log(result);
res.json({
id: result[0]['id'],
content: result[0]['content'],
date: result[0]['date'],
author: result[0]['author']
});
// console.log(res);
// console.log(result[0]['date']);
} else {
res.json({
id: "-1",
content: "Le post n'a pas pu être trouvée",
date: "21/04/1998",
author: ""
});
}
}
});
};
exports.getAll = function (req, res) {
req.database.query('SELECT p.id as id, p.content as content, p.date as date, u.nick as author FROM AQHposts AS p JOIN core_user AS u ON u.id = p.author ORDER BY p.date DESC;', (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
posts: []
};
if (result.length > 0) {
for (let i in result) {
data.posts.push({
id: result[i]['id'],
date: result[i]['date'],
content: result[i]['content'],
author: result[i]['author']
});
}
}
res.json(data);
}
});
};
exports.getAllFrom = function (req, res) {
req.database.query('SELECT p.id as id, p.content as content, p.date as date, u.nick as author FROM AQHposts AS p JOIN core_user AS u ON u.id = p.author WHERE p.author=? ORDER BY p.date DESC;', [req.user.id], (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
posts: []
};
if (result.length > 0) {
for (let i in result) {
data.posts.push({
id: result[i]['id'],
date: result[i]['date'],
content: result[i]['content'],
author: result[i]['author']
});
}
}
res.json(data);
}
});
};
exports.add = function (req, res) {
console.log("add");
req.database.query('INSERT INTO AQHposts (content, date, author, eventid) VALUES (?, NOW(), ?, ?);', [req.body.content, req.user.id, req.body.eventid], (error, result) => {
if (error) {
req.logger.error(error);
} else {
res.sendStatus(200);
}
});
};
exports.update = function (req, res) {
console.log("update");
req.database.query('UPDATE AQHposts SET content = ? WHERE id = ?;', [req.body.content, req.body.id], (error, result) => {
if (error) {
req.logger.error(error);
} else {
res.sendStatus(200);
}
});
};
exports.delete = function (req, res) {
req.database.query('DELETE FROM AQHposts WHERE id = ?;', [req.body.id], (error, result) => {
if (error) {
req.logger.error(error);
} else {
res.sendStatus(200);
}
});
};
exports.getALLData = function (req, res) {
req.database.query('\
SELECT p.id, p.content, c.author, c.content FROM core_users AS u \
JOIN (\
SELECT c.id AS id, c.content AS content, c.author AS author, p.date AS date FROM AQHcomments AS c \
JOIN AQHposts AS p \
ON c.postid = p.id) AS req\
ON req.author=u.id \
ORDER BY req.date DESC ;', (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
posts: []
};
if (result.length > 0) {
for (let i in result) {
data.posts.push({
id: result[i]['id'],
date: result[i]['date'],
content: result[i]['content'],
author: result[i]['author']
});
}
}
res.json(data);
}
});
};
exports.getComments = function (req, res) {
req.database.query('SELECT c.id as id, c.content as content, c.date as date, c.author as author FROM AQHcomments AS c JOIN AQHposts AS p ON c.postid = p.id ORDER BY p.date DESC;', (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
comments: []
};
if (result.length > 0) {
for (let i in result) {
data.comments.push({
id: result[i]['id'],
date: result[i]['date'],
content: result[i]['content'],
author: result[i]['author']
});
}
}
res.json(data);
}
});
};
exports.getPostComments = function (req, res) {
console.log("getPostComments");
req.database.query('SELECT c.id as id, c.content as content, c.date as date, c.author as author FROM AQHcomments AS c JOIN AQHposts AS p ON c.postid = p.id WHERE p.id=? ORDER BY p.date DESC;', [req.body.postid], (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
comments: []
};
if (result.length > 0) {
for (let i in result) {
data.comments.push({
id: result[i]['id'],
date: result[i]['date'],
content: result[i]['content'],
author: result[i]['author'],
});
}
}
res.json(data);
}
});
};
a=function(b){
console.log(b);
}
/*getPosts = function (i) {
console.log("pregetPOsts");
req.database.query("SELECT p.id as id, p.content as content, p.date as date, p.author as author FROM AQHposts AS p JOIN AQHevents AS e ON p.eventid = e.id WHERE e.id=? ORDER BY p.date DESC;",[i] , function (errorQuery, result) {
console.log("postquery");
if (errorQuery) {
console.log("errorgetPosts");
req.logger.warning(errorQuery);
res.sendStatus(500);
} else {
var data = {
posts: []
};
console.log("result");
if (result.length > 0) {
for (let i in result) {
data.posts.push({
id: result[i]['id'],
date: result[i]['date'],
content: result[i]['content'],
author: result[i]['author']
});
}
}
console.log(data);
return data;*/
/* req.engines['ejs'].renderFile('modules/aqh/body/post.ejs', data, (errorEjs, resultEjs) => {
if (errorEjs) {
req.logger.error(errorEjs);
res.sendStatus(500);
} else {
console.log(resultEjs);
res.send(resultEjs);
}
});*/
// }
// });
//};
// req.database.query('SELECT * FROM AQHevents;', (error2, result2) => {
//};
// req.database.query('SELECT e.id as id, e.content as content, e.date as date, u.nick as author, e.title as title FROM (AQHevents AS e JOIN core_user AS u ON u.id = e.author) ORDER BY e.date DESC;', (error2, result2) => {
//};
//a("testfction");
exports.getPostsForEvent = function (req, res) {
console.log("getPostsForEvent");
req.database.query('SELECT p.id as id, p.content as content, p.date as date, p.author as author FROM AQHposts AS p JOIN AQHevents AS e ON p.eventid = e.id WHERE e.id=? ORDER BY p.date DESC;', [req.body.eventid], (error, result) => {
if (error) {
req.logger.error(error);
res.sendStatus(500);
} else {
var data = {
posts: []
};
if (result.length > 0) {
for (let post of result) {
data.posts.push({
id: post['id'],
date: post['date'],
content: post['content'],
author: post['author']
});
}
}
res.json(data);
}
});
};