-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.js
More file actions
309 lines (278 loc) · 8.7 KB
/
thread.js
File metadata and controls
309 lines (278 loc) · 8.7 KB
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
var user = require('./user.js');
var forum = require('./forum.js');
var moment = require('moment');
function MyAsync(finalCallback) {
this.finalCallback = finalCallback;
this.counter = 0;
this.data = {};
this.add = function (callback) {
this.counter++;
callback();
};
this.check = function () {
var that = this;
this.counter--;
if (this.counter == 0)
process.nextTick(function () {
that.finalCallback(that.data);
});
};
}
function getThreadDetails (callback, id, connection) {
connection.query('SELECT * FROM `threads` WHERE `id` = ?;', id, function (error, results, fields) {
if(error){
console.log(error);
callback(undefined, 'unknown error');
}
else if(results.length === 0) {
callback(undefined,'error object not found');
}
else {
var res = results[0];
res.date = moment(res.date).format("YYYY-MM-DD HH:mm:ss");
callback(res);
}
})
}
var create = function (data, connection, callback) {
if(!data.hasOwnProperty('isDeleted')) data.isDeleted = false;
connection.query('INSERT INTO `threads`(`isDeleted`, `forum`, `title`, `isClosed`, `user`, `date`, `message`, `slug`)'+
'VALUE (?, ?, ?, ?, ?, ?, ?, ?);', [data.isDeleted, data.forum, data.title, data.isClosed, data.user, data.date,
data.message, data.slug],
function (error, results, fields) {
console.log(results);
if(error) {
console.log(error);
callback(4,'unknow error');
}
else {
data.id = results.insertId;
callback(0, data);
}
})
}
var close = function (data, connection, callback) {
connection.query('UPDATE `threads` SET `isClosed` = TRUE WHERE `id` = ?;', data.thread,
function (error, results, fields) {
if(error) console.log(error);
callback(0, data);
})
}
var open = function (data, connection, callback) {
connection.query('UPDATE `threads` SET `isClosed` = FALSE WHERE `id` = ?;', data.thread,
function (error, results, fields) {
if(error) console.log(error);
callback(0, data);
})
}
var details = function (data, connection, callback) {
getThreadDetails(function(results){
if(results === undefined){
callback(1, 'error');
}
else {
var res = results;
if (data.related) {
if(data.related.indexOf('thread') !== -1) {
callback(3, 'error')
}
else {
var asyn = new MyAsync(function() {
callback(0, res);
});
asyn.add(function(){
if (data.related.indexOf('user') !== -1) {
asyn.add(function(){
user.getUserDetails( function (userInfo) {
if (userInfo !== undefined) {
res.user = userInfo;
}
else {
asyn.finalCallback = function(){
callback(4,'unknown error');
}
}
asyn.check();
}, res.user, connection)
});
}
if(data.related.indexOf('forum') !== -1) {
asyn.add(function(){
forum.getForumDetails( function (forumInfo) {
if (forumInfo !== undefined) {
res.forum = forumInfo;
}
else {
asyn.finalCallback = function(){
callback(4,'unknown error');
}
}
asyn.check();
}, res.forum, connection)
});
}
});
asyn.check();
}
}
else callback(0, res);
}
}, data.thread, connection)
}
// getThreadDetails(function (data) {
// callback(0, data);
// }, data.thread, connection);
// if(data.hasOwnProperty('related')) {
// if (data.related.indexOf('user')) {
// // get user details
// }
// if (data.related.indexOf('forum')) {
// // get forum details
// }
// }
var list = function (data, connection, callback) {
var limit = '';
if(!data.hasOwnProperty('order')) data.order = 'desc';
if(!data.hasOwnProperty('since')) data.since = '0000-00-00 00:00:00';
if(data.hasOwnProperty('limit')) {
limit = ' LIMIT ' + data.limit;
}
if(data.hasOwnProperty('user')) {
connection.query('SELECT * FROM `threads` WHERE `user` = ? AND `date` >= ? ORDER BY `date` ' + data.order + limit + ' ;',
[data.user, data.since], function (error, results, fields) {
var res = results;
res.forEach( function (item){
item.date = moment(item.date).format("YYYY-MM-DD HH:mm:ss");
})
if(error) console.log(error);
else callback(0, res);
});
}
else if(data.hasOwnProperty('forum')) {
connection.query('SELECT * FROM `threads` WHERE `forum` = ? AND `date` >= ? ORDER BY `date` ' + data.order + limit + ' ;',
[data.forum, data.since], function (error, results, fields) {
var res = results;
res.forEach( function (item){
item.date = moment(item.date).format("YYYY-MM-DD HH:mm:ss");
})
if(error) console.log(error);
else callback(0, res);
});
}
else callback(5, 'error');
}
//сделать сортировку!!!!
var listPosts = function (data, connection, callback) {
var limit = '';
if(!data.hasOwnProperty('order')) data.order = 'desc';
if(!data.hasOwnProperty('since')) data.since = '0000-00-00 00:00:00';
if(data.hasOwnProperty('limit')) {
limit = ' LIMIT ' + data.limit;
}
connection.query('SELECT * FROM `posts` WHERE `thread` = ? AND `date` >= ? ORDER BY `date` ' + data.order + limit + ' ;',
[data.thread, data.since], function (error, results, fields) {
var res = results;
res.forEach( function (item){
item.date = moment(item.date).format("YYYY-MM-DD HH:mm:ss");
})
if(error) console.log(error);
else callback(0, res);
});
}
var remove = function (data, connection, callback) {
var thread = data.thread;
//maybe not delete posts
connection.query('UPDATE `threads` SET `isDeleted` = TRUE, `posts` = 0 WHERE `id` = ?;',
data.thread, function (error, results, fields) {
if(error) console.log(error);
else {
connection.query('UPDATE `posts` SET `isDeleted` = TRUE WHERE `thread` = ?;',
data.thread, function (error, results, fields) {
if(error) console.log(error);
else callback(0, data);
});
}
});
}
var restore = function (data, connection, callback) {
var thread = data.thread;
connection.query('UPDATE `posts` SET `isDeleted` = FALSE WHERE `thread` = ?;',
data.thread, function (error, results, fields) {
var rows = results.affectedRows;
if(error) console.log(error);
else {
connection.query('UPDATE `threads` SET `isDeleted` = FALSE, `posts` = ? WHERE `id` = ?;',
[rows ,data.thread], function (error, results, fields) {
if(error) console.log(error);
else callback(0, data);
});
}
});
}
var subscribe = function (data, connection, callback) {
connection.query('INSERT INTO `users_threads` (`user`, `thread`) VALUE (?, ?);',
[data.user, data.thread], function (error, results, fields) {
if(error) {
console.log('in subscribe');
console.log(error);
callback(0, data);
}
else callback(0, data);
});
}
var unsubscribe = function (data, connection, callback) {
connection.query('DELETE FROM `users_threads` WHERE `user` = ? AND `thread` = ?;',
[data.user, data.thread], function (error, results, fields) {
if(error) console.log(error);
else callback(0, data);
});
}
var update = function (data, connection, callback) {
connection.query('UPDATE `threads` SET `message` = ?, `slug` = ? WHERE `id` = ?',
[data.message, data.slug, data.thread], function (error, results, fields) {
if(error) console.log(error);
else {
getThreadDetails(function (results) {
callback(0, results)
}, data.thread, connection);
}
});
}
var vote = function (data, connection, callback) {
if (data.vote === 1) {
connection.query('UPDATE `threads` SET `likes` = `likes` + 1, `points` = `points` + 1 WHERE `id` = ?;',
data.thread, function (error, results, fields) {
if(error) console.log(error);
else {
getThreadDetails(function (results) {
callback(0, results)
}, data.thread, connection);
}
});
}
else if (data.vote === -1) {
connection.query('UPDATE `threads` SET `dislikes` = `dislikes` + 1, `points` = `points` - 1 WHERE `id` = ?;',
data.thread, function (error, results, fields) {
if(error) console.log(error);
else {
getThreadDetails(function (results) {
callback(0, results)
}, data.thread, connection);
}
});
}
else console.log('error data.vote');
}
module.exports.listPosts = listPosts;
module.exports.remove = remove;
module.exports.vote = vote;
module.exports.update = update;
module.exports.subscribe = subscribe;
module.exports.unsubscribe = unsubscribe;
module.exports.restore = restore;
module.exports.list = list;
module.exports.close = close;
module.exports.details = details;
module.exports.open = open;
module.exports.create = create;
module.exports.getThreadDetails = getThreadDetails;