-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
320 lines (260 loc) · 10 KB
/
Copy pathserver.js
File metadata and controls
320 lines (260 loc) · 10 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
310
311
312
313
314
315
316
317
318
319
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var pg = require('pg');
var app = express();
var exphbs = require('express-handlebars');
var staticPath = path.join(__dirname, 'static');
app.use(express.static(staticPath));
// allows us to parse the incoming request body
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// Connects to postgres once, on server start
var conString = process.env['DATABASE_URL'];
var db;
pg.connect(conString, function(err, client) {
if (err) {
console.log(err);
} else {
console.log("Connected to database");
db = client;
}
});
//---------- GET REQUESTS/ENDPOINTS -----------
//GET /collections, returns all collections
app.get("/collections", function(req, res) {
//checks if client is asking server for endpoint
if (req.header('Content-Type') != "application/json"){
//renders collections.html
res.sendFile(__dirname + "/views/collections.html");
return;
}
db.query("SELECT title, collection_id FROM collections", function (err, results) {
if (err){
//TODO: error handling % and special characters
res.status(500);
console.log(err);
} else if (results.rows.length == 0) {
console.log('empty rows')
res.status(404).send('Collection Not Found');
} else {
// res.send(results.rows);
console.log(results)
res.send(results.rows);
};
});
});
//GET /collections/:collection_id
//returns all articles within a collection, given the collection_id
//TODO: Solve no-cache problem; when hitting back or forward on the browser, data will be displayed in JSON format
app.get("/collections/:collection_id", function(req, res) {
console.log(req.params.collection_id);
var collection_id = req.params.collection_id;
//check to see who's calling the server (client or server?)
if (req.header('Content-Type') != "application/json"){
// my attempt at debugging the cache problem
console.log('before set');
console.log(res.getHeader('Cache-Control'));
res.setHeader('cache-control', 'no-cache');
console.log('after set');
console.log(res.getHeader('cache-control'));
res.sendFile(__dirname + "/views/collection.html");
return;
}
db.query("SELECT title, subject, user_name, article_id FROM collectionsView WHERE collection_id = $1;", [req.params.collection_id], function (err, results) {
if (err) {
//TODO: error handling % and special characters
res.status(500);
console.log(err);
} else if (results.rows.length == 0) {
console.log('empty rows')
res.status(404).send('Collection Not Found');
} else {
// res.send(results.rows);
// console.log(results);
// my attempt at debugging the cache problem
console.log('before set');
console.log(res.getHeader('cache-control'));
res.setHeader('cache-control', 'no-cache');
console.log('after set');
console.log(res.getHeader('cache-control'));
res.send(results.rows);
};
});
});
app.get('/article/new', function (req, res) {
res.sendFile(__dirname + "/views/create.html");
});
app.post('/article/new', function (req, res) {
// db.query("INSERT INTO messages (type_token, channel_token, user_name, message_text) VALUES ($1, $2, $3, $4)", [req.params.type_token, req.params.channel_token, req.body.user_name, req.body.message_text], function(err, result) {
// if (err) {
// if (err.code == "23502") {
// err.explanation = "Didn't get all of the parameters in the request body. Send user_name and message_text in the request body (remember this is a POST request)."
// }
// res.status(500).send(err);
// } else {
// res.send(result);
// }
// });
});
//GET request: returns article
app.get('/article/:article_id', function (req, res) {
var article_id = req.params.article_id;
if (req.header('Content-Type') != "application/json"){
res.sendFile(__dirname + "/views/article.html");
return;
}
console.log("boop");
db.query("SELECT * from article_view WHERE article_id = $1", [req.params.article_id],
function(err, result){callback('article_view', err, result)});
db.query("SELECT * from resources_view WHERE article_id = $1", [req.params.article_id],
function(err, result){callback('resources_view', err, result)});
db.query("SELECT * from sections_view WHERE article_id = $1", [req.params.article_id],
function(err, result){callback('sections_view', err, result)});
var completion = 0;
var articleJSON = {};
var sectionWithResourcesArray = [];
//var categoriesArray = [];
var sectionsArray = [];
var callback = function (querytype, err, result) {
completion++;
if (querytype == "resources_view"){
var results = result.rows;
var i = 0;
var sectionIDIndex = -1;
var resourceJSON = [];
//iterate through result set
while (i < results.length){
//moving on to a new section id
if (sectionIDIndex != results[i]["section_id"]){
//check if finished with resource json collection
if (resourceJSON.length > 0){
//file the jsons away for the section
var sectionJSON = {"section_id" : sectionIDIndex} ;
sectionJSON["resources"] = resourceJSON;
//put all the resource info for the section into the array
sectionWithResourcesArray.push(sectionJSON);
resourceJSON = [];
}
sectionIDIndex = results[i]["section_id"];
}
//create a json for the resource
var resource = {};
resource["owner_id"] = results[i]["owner_id"];
resource["user_name"] = results[i]["user_name"];
resource["created"] = results[i]["created"];
resource["resource_title"] = results[i]["resource_title"];
resource["resource_body"] = results[i]["body"];
resourceJSON.push(resource);
i++;
}
//account for the last group of resources
if (resourceJSON.length > 0){
var sectionJSON = {"section_id" : sectionIDIndex} ;
sectionJSON["resources"] = resourceJSON;
//put all the resource info for the section into the array
sectionWithResourcesArray.push(sectionJSON);
console.log("//////////////");
}
}
else if (querytype == "sections_view"){
var results = result.rows;
for (var i = 0; i < results.length; i++) {
var section = {};
for (var k = 0; k < sectionWithResourcesArray.length; k++){
if (sectionWithResourcesArray[k]["section_id"] == results[i]["section_id"]){
section["resources"] = sectionWithResourcesArray[k]["resources"];
break;
}
}
section["section_id"] = results[i]["section_id"];
section["section_title"] = results[i]["section_title"];
section["section_body"] = results[i]["section_body"];
section["created"] = results[i]["created"];
section["owner_id"] = results[i]["owner_id"];
sectionsArray.push(section);
}
}
else if (querytype == "article_view"){
//populate article information from article_view
if (result.rows.length > 0){
articleJSON["article_id"] = result.rows[0]["article_id"];
articleJSON["created"] = result.rows[0]["created"];
articleJSON["subject"] = result.rows[0]["subject"];
articleJSON["owner_id"] = result.rows[0]["owner_id"];
articleJSON["user_name"] = result.rows[0]["user_name"];
articleJSON["sections"] = sectionsArray;
}
}
if (completion == 3){
res.send(articleJSON);
}
}
});
//GET request: returns user's profile
//#TODO : Refactor all these callbacks
app.get('/user/:user_name', function (req, res) {
var user;
var userArticles;
var completion = 0;
var profileJSON = {};
console.log(req.header('Content-Type'));
if (req.header('Content-Type') != "application/json"){
res.sendFile(__dirname + "/views/user.html");
console.log('serving html for user/ ' + req.params.user_name)
return;
}
console.log('serving JSON', req.params);
db.query("SELECT * FROM user_view WHERE user_name = $1", [req.params.user_name], function(err, result) {
if (err) {
console.log('error');
res.status(500).send(err);
console.log(err);
} else {
if (result.rows.length > 0) {
console.log(result.rows);
user = result.rows[0];
console.log(user);
profileJSON = user;
db.query("SELECT subject, created FROM articles WHERE owner_id = $1", [user.user_id], function(err, result) {
if (err) {
res.status(500).send(err);
console.log(err);
} else {
userArticles = result.rows;
profileJSON['articleInfo'] = userArticles;
db.query("SELECT title, created, status FROM section_view WHERE owner_id = $1", [user.user_id], function(err, result) {
if (err) {
res.status(500).send(err);
console.log(err);
} else {
userSections = result.rows;
profileJSON['sectionInfo'] = userSections;
db.query("SELECT * FROM proposed_edits", function(err, result) {
if (err) {
res.status(500).send(err);
console.log(err);
} else {
if (user.role == 'Mod' || user.role == 'Admin') {
pendingReviewsForUser = result.rows;
profileJSON['pendingInfo'] = pendingReviewsForUser;
}
res.send(profileJSON);
}
});
}
});
}
});
} else {
console.log('user not found' + ' ' + req.params.user_name);
res.status(404).send('username not found :(');
}
}
});
});
app.listen(process.env['PORT'], function() {
console.log('listening');
});