forked from Polatouche0201/MidPro_WikiMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
327 lines (262 loc) · 9.52 KB
/
server.js
File metadata and controls
327 lines (262 loc) · 9.52 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
320
321
322
323
324
325
326
327
"use strict";
require('dotenv').config();
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || "development";
const express = require("express");
const bodyParser = require("body-parser");
const sass = require("node-sass-middleware");
const app = express();
const knexConfig = require("./knexfile");
const knex = require("knex")(knexConfig[ENV]);
const morgan = require('morgan');
const knexLogger = require('knex-logger');
// Seperated Routes for each Resource
const mapsRoutes = require("./routes/maps");
app.use(morgan('dev'));
// Log knex SQL queries to STDOUT as well
app.use(knexLogger(knex));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/styles", sass({
src: __dirname + "/styles",
dest: __dirname + "/public/styles",
debug: true,
outputStyle: 'expanded'
}));
app.use(express.static("public"));
// Mount all resource routes
app.use("/maps/add", mapsRoutes(knex));
/******** Public Varaibles ********/
let p_currentUserID = 0;
let p_mapListType = 1; // 1-All Maps, 2-Favourite Maps, 3-Contributed Maps
// Maps page
app.get("/maps", (req, res) => {
p_mapListType = 1;
if (knex.select("*").from("maps").length == 0){}
else {
knex.select("*").from("maps").then((results) => {
let mapsObj = JSON.parse(JSON.stringify(results));
let lastestMap = mapsObj[mapsObj.length - 1].id;
res.redirect("/maps/" + lastestMap.toString()); //doesn't show icon of created if not logged in as that user and no icons if not logged in
});
}
});
function getSpecificMapInfo(mapID, req, res) {
let resultObj = {};
knex.select("*").from("points").where("map_id", mapID)
.then((results) => {
resultObj.results = JSON.parse(JSON.stringify(results));
knex.select("icon").from("maps").where("id", mapID)
.then((results) => {
let markIcon = JSON.parse(JSON.stringify(results));
resultObj.mark_icon = markIcon[0].icon;
res.json(resultObj);
});
});
}
app.get('/gm/maps/:mapID', (req, res) => {
let mapID = req.params.mapID;
getSpecificMapInfo(mapID, req, res);
});
app.get('/gm/favourite/:mapID', (req, res) => {
if (!req.params.mapID){
console.log("No favourites")
}
else{
let mapID = req.params.mapID;
getSpecificMapInfo(mapID, req, res);
}
});
app.get('/gm/contribute/:mapID', (req, res) => {
let mapID = req.params.mapID;
getSpecificMapInfo(mapID, req, res);
});
app.post('/like', (req, res) => {
knex('user_favourite').insert(req.body)
.then((result) => {
res.json({ result: true});
});
});
app.get('/unlike', (req, res) => {
// console.log(">>> ", req.query);
knex('user_favourite').where("user_id",req.query.user_id).andWhere("map_id",req.query.map_id)
.del().then((result) => {
// console.log(">>> Delete complete!");
res.json({ result: true});
// res.redirect("/maps/" + req.params.mapID);
});
});
app.post('/maps',(req,res)=>{
//the promise, means the data entry was success
knex('points').insert(req.body).returning('id')
.then((id) => {
res.json({ result: true, id: id });
});
});
// Selected A Map Page from All Map List
app.get("/maps/:mapID", (req, res) => {
p_mapListType = 1;
let templateVars = {
curMapID: req.params.mapID,
mapListType: p_mapListType
};
// 1. Find All Maps Info.
knex.select("*").from("maps").then((results) => {
let mapsObj = JSON.parse(JSON.stringify(results));
templateVars.maps = mapsObj;
// 2. Find All Points Info. for current Map
knex.select("*").from("points").where("map_id", templateVars.curMapID).then((results) => {
let pointsObj = JSON.parse(JSON.stringify(results));
templateVars.points = pointsObj;
// 3. Find User-Favourite Info.
knex.select("*").from("user_favourite").then((results) => {
let ufObj = JSON.parse(JSON.stringify(results));
templateVars.user_favourite = ufObj;
// 4. Find All Users Info.
knex.select("*").from("users").then((results) => {
let usersObj = JSON.parse(JSON.stringify(results));
templateVars.users = usersObj;
if(p_currentUserID <= 0) {
templateVars.curUser = false;
res.render("maps", templateVars);
} else {
knex.select("*").from("users").where('id', p_currentUserID).then((results) => {
let userObj = JSON.parse(JSON.stringify(results));
templateVars.curUser = userObj[0];
res.render("maps", templateVars);
});
}
});
});
});
});
});
// Contributed Maps
app.get("/contribute", (req, res) => {
p_mapListType = 3;
knex.select("*").from("maps").where("user_id", p_currentUserID)
.then((results) => {
let mapsObj = JSON.parse(JSON.stringify(results));
let lastestMap = mapsObj[mapsObj.length - 1].id;
res.redirect("/contribute/" + lastestMap.toString());
});
});
// Selected A Map Page from Contributed Map List
app.get("/contribute/:mapID", (req, res) => {
p_mapListType = 3;
let templateVars = {
curMapID: req.params.mapID,
mapListType: p_mapListType
};
// 1. Find Current User Info.
knex.select("*").from("users").where('id', p_currentUserID).then((results) => {
let userObj = JSON.parse(JSON.stringify(results));
templateVars.curUser = userObj[0];
// 2. Find All Maps Info.
knex.select("*").from("maps").where("user_id", p_currentUserID).then((results) => {
let mapsObj = JSON.parse(JSON.stringify(results));
templateVars.maps = mapsObj;
// 3. Find All Points Info. for current Map
knex.select("*").from("points").where("map_id", templateVars.curMapID).then((results) => {
let pointsObj = JSON.parse(JSON.stringify(results));
templateVars.points = pointsObj;
//4. Find All Users Info. for current Map
knex.select("*").from("users").then((results) => {
let usersObj = JSON.parse(JSON.stringify(results));
templateVars.users = usersObj;
// 5. Find User-Favourite Info.
knex.select("*").from("user_favourite").then((results) => {
let ufObj = JSON.parse(JSON.stringify(results));
templateVars.user_favourite = ufObj;
res.render("maps", templateVars);
});
});
});
});
});
});
// Favourite Maps
app.get("/favourite", (req, res) => {
p_mapListType = 2;
// if (knex.select("*").from("user_favourite").where("user_id", p_currentUserID).length==undefined){
// res.send("No favourites") // want a better thing instead of just the plain text
// }
// else {
knex.select("*").from("user_favourite").where("user_id", p_currentUserID)
.then((results) => {
let ufObj = JSON.parse(JSON.stringify(results));
let lastestMap = ufObj[ufObj.length - 1].map_id;
res.redirect("/favourite/" + lastestMap.toString()); //for some reason when showing favourites, changes the created by to the favourited user instad of keeping as the original creator
});
// }
});
// Selected A Map Page from Favourite Map List
app.get("/favourite/:mapID", (req, res) => {
p_mapListType = 2;
let templateVars = {
curMapID: req.params.mapID,
mapListType: p_mapListType
};
// 1. Find Current User Info.
knex.select("*").from("users").where('id', p_currentUserID).then((results) => {
let userObj = JSON.parse(JSON.stringify(results));
templateVars.curUser = userObj[0];
// 2. Find All Maps Info.
knex.select("*").from("maps AS m").join("user_favourite AS uf", "m.id", "uf.map_id")
.where("uf.user_id", p_currentUserID).then((results) => {
let mapsObj = JSON.parse(JSON.stringify(results));
templateVars.maps = mapsObj;
// 3. Find All Points Info. for current Map
knex.select("*").from("points").where("map_id", templateVars.curMapID).then((results) => {
let pointsObj = JSON.parse(JSON.stringify(results));
templateVars.points = pointsObj;
//4. Find All Users Info. for current Map
knex.select("*").from("users").then((results) => {
let usersObj = JSON.parse(JSON.stringify(results));
templateVars.users = usersObj;
// 5. Find User-Favourite Info.
knex.select("*").from("user_favourite").then((results) => {
let ufObj = JSON.parse(JSON.stringify(results));
templateVars.user_favourite = ufObj;
res.render("maps", templateVars);
});
});
});
});
});
});
app.get('/deletePoint/:mapID', (req, res) =>{
let point_id = Number(req._parsedOriginalUrl.query);
knex('points').where("id",point_id).del().then((result) => {
res.redirect("/maps/" + req.params.mapID);
});
})
app.post('/maps/editPoint', (req, res) =>{
let point = req.body;
knex('points').where("id",point.id).update(point)
.then((result) => {
res.redirect('/maps');
});
})
app.get("/login/:userID", (req, res) => {
p_currentUserID = req.params.userID;
p_mapListType = 1;
knex.select("*").from("maps").then((results) => {
let mapsObj = JSON.parse(JSON.stringify(results));
let lastestMap = mapsObj[mapsObj.length - 1].id;
res.redirect("/maps/" + lastestMap.toString());
});
});
app.get("/logout", (req, res) => {
//req.session.user_id = "";
p_currentUserID = -1;
p_mapListType = 1;
knex.select("*").from("maps").then((results) => {
let mapsObj = JSON.parse(JSON.stringify(results));
let lastestMap = mapsObj[mapsObj.length - 1].id;
res.redirect("/maps/" + lastestMap.toString());
});
});
app.listen(PORT, () => {
console.log("Example app listening on port " + PORT);
});