-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
477 lines (392 loc) · 15.5 KB
/
server.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
const express = require('express');
const path = require('path');
const { Pool } = require('pg');
const bodyParser = require('body-parser');
const axios = require('axios');
const session = require('express-session');
const bcrypt = require('bcrypt');
const RAWG_API_KEY = '0d58929b4d22498181f00eda4eb343c0';
const RAWG_API_URL = 'https://api.rawg.io/api';
const app = express();
const port = 3000;
const pool = new Pool({
host: 'localhost',
user: "postgres",
database: 'postgres',
password: 'admin123',
port: 5432,
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
secret: 'My_key',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(express.static(path.join(__dirname, 'public')));
async function fetchGameDetails(games) {
const promises = games.map(async (game) => {
const response = await axios.get(`${RAWG_API_URL}/games/${game.gameid}?key=${RAWG_API_KEY}`);
return { ...game, ...response.data };
});
return await Promise.all(promises);
}
app.post('/signup', async (req, res) => {
const { user_name, email, password, confirm_password } = req.body;
if (password !== confirm_password) {
return res.status(400).json({ error: 'Passwords do not match' });
}
try {
const hashedPassword = await bcrypt.hash(password, 10);
await pool.query(
'INSERT INTO users (user_name, email, password) VALUES ($1, $2, $3)',
[user_name, email, hashedPassword]
);
res.status(201).json({ message: 'User registered successfully' });
} catch (error) {
console.error('Error registering user:', error);
if (error.code === '23505') {
res.status(400).json({ error: 'Username or email already in use' });
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});
app.post('/login', async (req, res) => {
const { userId, user_name, email, password } = req.body;
try {
const result = await pool.query(
'SELECT * FROM users WHERE user_name = $1 OR email = $2',
[user_name, email]
);
if (result.rows.length > 0) {
const user = result.rows[0];
const match = await bcrypt.compare(password, user.password);
if (match) {
req.session.user = { userId: user.user_no, user_name: user.user_name, email: user.email };
res.json({ message: 'Login successful' });
} else {
res.status(400).json({ error: 'Invalid credentials' });
}
} else {
res.status(400).json({ error: 'User not found' });
}
} catch (error) {
console.error('Error logging in user:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/logout', (req, res) => {
req.session.destroy();
res.json({ message: 'Logout successful' });
});
app.get('/topics', async (req, res) => {
try {
const result = await pool.query('SELECT topic_no, topic_name, description, date_created FROM forum');
res.json(result.rows);
} catch (error) {
console.error('Error fetching topics:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/api/topic/:id', async (req, res) => {
const topicNo = req.params.id;
try {
const topicResult = await pool.query('SELECT topic_name, description, date_created, details FROM forum WHERE topic_no = $1', [topicNo]);
const commentsResult = await pool.query('SELECT user_name, comment, date_created FROM forumComments WHERE topic_no = $1', [topicNo]);
res.json({
topic: topicResult.rows[0],
comments: commentsResult.rows
});
} catch (error) {
console.error('Error fetching topic:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/api/topic/:id/comment', async (req, res) => {
const topicNo = req.params.id;
const { comment } = req.body;
if (req.session.user){
var user_name = req.session.user.user_name;
}
else{
return res.status(401).json({ error: 'You have to be logged in to make a comment' });
}
try {
await pool.query(
`INSERT INTO forumComments (topic_no, user_name, comment, date_created) VALUES ($1, $2, $3, CURRENT_TIMESTAMP)`,
[topicNo, user_name, comment]
);
res.status(201).json({ message: 'Comment added successfully' });
} catch (error) {
console.error('Error adding comment:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/api/games', async (req, res) => {
const { genre, year, searchTerm, sort , page = 1} = req.query;
let params = {
key: RAWG_API_KEY,
page_size: 20,
page: page
};
if (genre) params.genres = genre;
if (year) params.dates = `${year}-01-01,${year}-12-31`;
if (searchTerm) params.search = searchTerm;
switch (sort) {
case 'name-asc':
params.ordering = 'name';
break;
case 'name-desc':
params.ordering = '-name';
break;
case 'rating-asc':
params.ordering = 'metacritic';
params.metacritic = '1,100';
break;
case 'rating-desc':
params.ordering = '-metacritic';
break;
case 'released-asc':
params.ordering = 'released';
break;
case 'released-desc':
params.ordering = '-released';
break;
default:
params.ordering = '-metacritic -released';
}
try {
const response = await axios.get(`${RAWG_API_URL}/games`, { params });
res.json(response.data);
} catch (error) {
console.error('Error fetching games from RAWG API:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/api/top-games', async (req, res) => {
try {
const response = await axios.get(`${RAWG_API_URL}/games`, {
params: {
key: RAWG_API_KEY,
ordering: '-released',
page_size: 4,
metacritic: '90, 100'
}
});
res.json(response.data);
} catch (error) {
console.error('Error fetching top games from RAWG API:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/api/short-library', async (req, res) =>{
if (req.session.user){
var userId = req.session.user.userId;
}
else{
return res.status(401).json({ error: 'You have to be logged in to view your library' });
}
try{
const likedGamesResult = await pool.query('SELECT gameid FROM LikedGames WHERE userid = $1', [userId]);
const likedGames = await axios.get(`${RAWG_API_URL}/games/${likedGamesResult.rows[0].gameid}?key=${RAWG_API_KEY}`);
const playedGamesResult = await pool.query('SELECT gameid FROM PlayedGames WHERE userid = $1', [userId]);
const playedGames = await axios.get(`${RAWG_API_URL}/games/${playedGamesResult.rows[0].gameid}?key=${RAWG_API_KEY}`);
const toPlayGamesResult = await pool.query('SELECT gameid FROM ToPlayGames WHERE userid = $1', [userId]);
const toPlayGames = await axios.get(`${RAWG_API_URL}/games/${toPlayGamesResult.rows[0].gameid}?key=${RAWG_API_KEY}`);
res.json({
likedGame: likedGames.data,
playedGame: playedGames.data,
toPlayGame: toPlayGames.data
})
} catch (error) {
console.error('Error fetching games from RAWG API:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
})
app.get('/api/game/:id', async (req, res) => {
const gameId = req.params.id;
try {
const response = await axios.get(`${RAWG_API_URL}/games/${gameId}`, {
params: { key: RAWG_API_KEY }
});
const commentsResult = await pool.query('SELECT user_name, comment, date_created FROM gameComment WHERE gameid = $1', [gameId]);
const ratingResult = await pool.query('SELECT ROUND(AVG(rating)) AS average_rating FROM GameRating WHERE gameid = $1', [gameId]);
const averageRating = ratingResult.rows[0].average_rating || 0;
res.json({...response.data, commentsResult, averageRating: averageRating});
} catch (error) {
console.error('Error fetching game details from Servers:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/api/game/:id/comment', async (req, res) => {
const gameId = req.params.id;
const { comment } = req.body;
if (req.session.user){
var user_name = req.session.user.user_name;
}
else{
return res.status(401).json({ error: 'You have to be logged in to make a comment' });
}
try {
await pool.query(
`INSERT INTO gameComment (gameid, user_name, comment, date_created) VALUES ($1, $2, $3, CURRENT_TIMESTAMP)`,
[gameId, user_name, comment]
);
res.status(201).json({ message: 'Comment added successfully' });
} catch (error) {
console.error('Error adding comment:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/api/like-game/:gameId', async (req, res) => {
const { gameId } = req.params;
if(req.session.user){
var userId = req.session.user.userId;
}
else{
return res.status(401).json({ message: 'You must be logged in to like the game' });
}
try {
const checkResult = await pool.query(
`SELECT * FROM likedgames WHERE userID = ${userId} AND gameID = ${gameId}`
);
if (checkResult.rows.length > 0) {
await pool.query(
`DELETE FROM likedgames WHERE userID = ${userId} AND gameID = ${gameId}`
);
return res.status(200).json({ message: 'Game unliked successfully' });
} else {
await pool.query(
`INSERT INTO likedgames (userID, gameID, dateLiked) VALUES (${userId}, ${gameId}, CURRENT_TIMESTAMP)`
);
return res.status(201).json({ message: 'Game liked successfully' });
}
} catch (error) {
console.error('Error toggling like status:', error);
return res.status(500).json({ message: 'Internal Server Error' });
}
});
app.get('/api/check-liked-game/:gameId', async (req, res) => {
const { gameId } = req.params;
if (req.session.user){
var userId = req.session.user.userId;
}
else{
return res.status(401).json({ message: 'User not logged in' });
}
try {
const result = await pool.query(
`SELECT * FROM likedgames WHERE userID = ${userId} AND gameID = ${gameId}`
);
res.json({ liked: result.rows.length > 0 });
} catch (error) {
console.error('Error checking liked game:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
});
app.post('/api/game/:id/rate', async (req, res) => {
const { rating } = req.body;
const gameId = req.params.id;
if (req.session.user){
var userId = req.session.user.userId;
}
else{
return res.status(401).json({ error: 'User not logged in' });
}
try {
await pool.query(
'INSERT INTO GameRating (gameid, userid, rating) VALUES ($1, $2, $3) ON CONFLICT (gameid, userid) DO UPDATE SET rating = EXCLUDED.rating',
[gameId, userId, rating]
);
res.status(200).json({ message: 'Rating submitted successfully' });
} catch (error) {
console.error('Error saving game rating:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/api/check-session', (req, res) => {
if (req.session.user) {
res.json({ loggedIn: true, user: req.session.user });
} else {
res.json({ loggedIn: false });
}
});
app.get('/user-games', async (req, res) => {
if (req.session.user){
var userId = req.session.user.userId;
}
else{
return res.status(401).json({ error: 'User not logged in' });
}
try {
const likedGamesResult = await pool.query('SELECT gameId, dateLiked FROM LikedGames WHERE userId = $1', [userId]);
const playedGamesResult = await pool.query('SELECT gameId, dateCompleted, userplaytime FROM PlayedGames WHERE userId = $1', [userId]);
const toPlayGamesResult = await pool.query('SELECT gameId, dateAdded FROM ToPlayGames WHERE userId = $1', [userId]);
const likedGames = await fetchGameDetails(likedGamesResult.rows);
const playedGames = await fetchGameDetails(playedGamesResult.rows);
const toPlayGames = await fetchGameDetails(toPlayGamesResult.rows);
res.json({
likedGames,
playedGames,
toPlayGames,
});
} catch (error) {
console.error('Error fetching user games:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
});
app.post('/save-game-status', async (req, res) => {
const { gameid, status, playtime, completedDate } = req.body;
if (req.session.user){
var userId = req.session.user.userId;
}
else{
return res.status(401).json({ error: 'User not logged in' });
}
try {
if (status === 'played') {
await pool.query(
'INSERT INTO PlayedGames (gameid, userid, userplaytime, datecompleted) VALUES ($1, $2, $3, $4) ON CONFLICT (gameid, userid) DO UPDATE SET userplaytime = $3, datecompleted = $4',
[gameid, userId, playtime, completedDate]
);
} else if (status === 'to-play') {
await pool.query(
'INSERT INTO ToPlayGames (gameid, userid, dateadded) VALUES ($1, $2, CURRENT_TIMESTAMP) ON CONFLICT (gameid, userid) DO NOTHING',
[gameid, userId]
);
}
res.status(200).json({ message: 'Game status saved successfully' });
} catch (error) {
console.error('Error saving game status:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'home.html'));
});
app.get('/forum', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'forum.html'));
});
app.get('/games', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'games.html'));
});
app.get('/signup', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'signup.html'));
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
app.get('/game/:id', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'game.html'));
});
app.get('/topic/:id', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'topic.html'));
});
app.get('/library', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'usergames.html'));
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});