-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (134 loc) · 4.19 KB
/
app.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
const express = require('express');
const { MongoClient, ObjectId } = require('mongodb');
const app = express();
const port = 3000;
const methodOverride = require('method-override');
const bodyParser = require('body-parser');
// MongoDB setup
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
let db;
async function connectDB() {
try {
await client.connect();
console.log("Connected to MongoDB");
db = client.db('Arcade');
} catch (e) {
console.error(e);
}
}
connectDB();
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(methodOverride('_method'));
app.use(bodyParser.urlencoded({ extended: true }));
// Redirect
app.get('/', (req, res) => {
res.redirect('/games');
});
// Route for listing games
app.get('/games', async (req, res) => {
let query = {};
if (req.query.title) {
query.name = { $regex: new RegExp(req.query.title, 'i') }; // Case-insensitive partial search
}
if (req.query.type && req.query.type !== 'No filter') {
query.type = req.query.type;
}
try {
const games = await db.collection('ArcadeGames').find(query).toArray();
const types = await db.collection('ArcadeGames').distinct('type'); // Get all distinct types for the dropdown
res.render('games', { games, types });
} catch (e) {
res.status(500).send("Error fetching games: " + e.message);
}
});
// Add new games
app.get('/games/new', (req, res) => {
res.render('newGame'); // This should point to a 'newGame.ejs' form without needing an ID
});
// Route for add new games
app.post('/games', async (req, res) => {
try {
await db.collection('ArcadeGames').insertOne({
name: req.body.title,
description: req.body.description,
type: req.body.type,
minimumAge: req.body.minimumAge,
pricing: {
hourly: req.body.pricingHourly,
perGame: req.body.pricingPerGame
},
image: {
path: req.body.imagePath,
description: req.body.imageAlt
}
});
res.redirect('/games');
} catch (e) {
res.status(500).send("Error creating new game: " + e.message);
}
});
// Route for game detail
app.get('/games/:id', async (req, res) => {
try {
const gameId = new ObjectId(req.params.id);
const game = await db.collection('ArcadeGames').findOne({ _id: gameId });
if (!game) {
return res.status(404).send("Game not found");
}
res.render('gameDetails', { game });
} catch (e) {
res.status(500).send("Error fetching game details: " + e.message);
}
});
// Route to show the edit form
app.get('/games/:id/edit', async (req, res) => {
try {
const game = await db.collection('ArcadeGames').findOne({ _id: new ObjectId(req.params.id) });
if (!game) {
return res.status(404).send("Game not found");
}
res.render('editGame', { game });
} catch (e) {
res.status(500).send("Error showing edit form: " + e.message);
}
});
// Route to update the game details
app.put('/games/:id', async (req, res) => {
try {
const gameId = new ObjectId(req.params.id);
await db.collection('ArcadeGames').updateOne(
{ _id: gameId },
{ $set: {
name: req.body.title,
description: req.body.description,
type: req.body.type,
minimumAge: req.body.minimumAge,
pricing: {
hourly: req.body.pricingHourly,
perGame: req.body.pricingPerGame
},
image: {
path: req.body.imagePath,
description: req.body.imageAlt
}
}}
);
res.redirect('/games/' + req.params.id); // Redirect to the updated game details page
} catch (e) {
res.status(500).send("Error updating game: " + e.message);
}
});
// Route to handle the DELETE request
app.delete('/games/:id', async (req, res) => {
try {
await db.collection('ArcadeGames').deleteOne({ _id: new ObjectId(req.params.id) });
res.redirect('/games');
} catch (e) {
res.status(500).send("Error deleting game: " + e.message);
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});