-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrendManager.js
104 lines (75 loc) · 1.86 KB
/
trendManager.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
// Linking to other files
db = require('./dbManager.js');
module.exports.trendBlog = trendBlog;
module.exports.trendAll = trendAll;
var allPosts = JSON.parse('{"trending":[]}');
var countPost = 0;
function trendBlog (req, res){
// Get the params
var limit = req.params.limit;
var order = req.params.order;
var hostname = req.params.hostname;
// Check if limit not set
if(limit == undefined || limit > 20){
limit = 20;
}
if(order.toUpperCase() == 'TRENDING'){
db.getBlogPostbyPopularity(hostname, limit, chunkPost);
}
else if (order.toUpperCase() == 'RECENT'){
db.getBlogPostRecent(hostname, limit, chunkPost);
}
function chunkPost(posts, end){
if(posts == 404){
res.send(404);
return;
}
allPosts.trending[countPost] = posts;
countPost += 1;
// Last post
if(countPost == end){
// Order and limit just need to be set once
allPosts.order = order;
allPosts.limit = limit;
// Reset count
countPost = 0;
// Send the result to user
res.send(allPosts);
allPosts = JSON.parse('{"trending":[]}');
}
}
}
function trendAll (req, res){
// Get the params
var limit = req.params.limit;
var order = req.params.order;
// Check if limit not set
if(limit == undefined || limit > 20){
limit = 20;
}
if(order.toUpperCase() == 'TRENDING'){
db.getPostbyPopularity(limit, chunkPost);
}
else if (order.toUpperCase() == 'RECENT'){
db.getPostRecent(limit, chunkPost);
}
function chunkPost(posts, end){
if(posts == 404){
res.send(404);
return;
}
allPosts.trending[countPost] = posts;
countPost += 1;
// Last post
if(countPost == end){
// Order and limit just need to be set once
allPosts.order = order;
allPosts.limit = limit;
// Reset count
countPost = 0;
// Send the result to user
res.send(allPosts);
allPosts = JSON.parse('{"trending":[]}');
}
}
}