-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
230 lines (163 loc) · 6.15 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const port = 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
// Serve static files from the 'public' folder
app.use(express.static(path.join(__dirname, 'public')));
// Functions for server
function filterItemsByUser(inputUser, jsonArray) {
return jsonArray.filter(item => item.user === inputUser || item.opponent === inputUser);
}
function hasDuplicateEvent(eventDataArray, eventData) {
// Use the Array.prototype.some() method to check if there is any element in the array
// that matches the given conditions.
return eventDataArray.some(event => {
const isDuplicate =
(event.user === eventData.user && event.opponent === eventData.opponent) ||
(event.user === eventData.opponent && event.opponent === eventData.user);
return isDuplicate && event.time === eventData.time;
});
}
// Define a route to handle the POST request for booking events
app.post('/bookVev', (req, res) => {
const eventData = req.body;
// Load existing data from the events.json file (if any)
let eventDataArray = [];
try {
const data = fs.readFileSync('data/events.json', 'utf8');
eventDataArray = JSON.parse(data);
} catch (err) {
console.error('Error reapng events.json:', err);
}
if (!hasDuplicateEvent(eventDataArray, eventData)) {
// Add the new event data to the array
eventDataArray.push(eventData);
// Write the updated data back to the events.json file
fs.writeFileSync('data/events.json', JSON.stringify(eventDataArray, null, 4), 'utf8');
// Respond with a JSON object indicating success
res.status(200).json({ message: 'Event booked successfully' });
} else {
res.status(409).json({ message: 'Event with same people and time is already booked' });
}
});
app.post('/updateWinner', (req, res) => {
const { user, opponent, time } = req.body.vev; // Assuming req.body contains vev with user, opponent, and time properties
const newWinner = req.body.winner;
let eventDataArray = [];
try {
const data = fs.readFileSync('data/events.json', 'utf8');
eventDataArray = JSON.parse(data);
} catch (err) {
console.error('Error reading events.json:', err);
}
let found = false;
// Search for an event that matches the user, opponent, and time
for (const event of eventDataArray) {
if (event.user === user && event.opponent === opponent && event.time === time) {
found = true;
event.winner = newWinner; // Update the "winner" attribute
break;
}
}
if (found) {
console.log('Event found and winner updated');
// Save the updated eventDataArray back to the events.json file
fs.writeFileSync('data/events.json', JSON.stringify(eventDataArray, null, 2));
// Respond with a JSON object indicating success
res.status(200).json({ message: 'Winner updated successfully' });
} else {
console.log('Event not found');
// Event not found
res.status(404).json({ message: 'Event not found' });
}
});
// Define a route to handle the POST request for adding users
app.post('/addUser', (req, res) => {
const newUser = req.body;
// Load existing data from the users.json file (if any)
let usersDataArray = [];
try {
const data = fs.readFileSync('data/users.json', 'utf8');
usersDataArray = JSON.parse(data);
} catch (err) {
console.error('Error reading users.json:', err);
}
// Add the new event data to the array
usersDataArray.push(newUser);
// Write the updated data back to the events.json file
fs.writeFileSync('data/users.json', JSON.stringify(usersDataArray, null, 4), 'utf8');
// Respond with a JSON object indicating success
res.status(200).json({ message: 'User added successfully' });
});
app.get('/getAllUsers', (req, res) => {
let allUsers = []
try {
const data = fs.readFileSync('data/users.json', 'utf8');
allUsers = JSON.parse(data);
} catch (err) {
console.error('Error reading users.json:', err);
}
// Respond with a JSON object indicating success or error
if (allUsers == null) {
res.status(500).json({ error: "allUsers was null" });
}
else if (allUsers) {
res.status(200).json(allUsers);
}
else {
res.status(500).json({ error: 'Failed to retrieve allUsers' });
}
});
app.get('/getAllVevs', (req, res) => {
let eventDataArray = [];
let events = [];
try {
const data = fs.readFileSync('data/events.json', 'utf8');
eventDataArray = JSON.parse(data);
} catch (err) {
console.error('Error reading events.json:', err);
}
events = eventDataArray.sort((a, b) => new Date(a.time) - new Date(b.time));
// Respond with a JSON object indicating success or error
if (events) {
res.status(200).json(events);
}
else {
res.status(500).json({ error: 'Failed to retrieve events' });
}
});
app.get('/getVevs', (req, res) => {
const user = req.headers.user;
let eventDataArray = [];
let events = [];
try {
const data = fs.readFileSync('data/events.json', 'utf8');
eventDataArray = JSON.parse(data);
} catch (err) {
console.error('Error reading events.json:', err);
}
events = eventDataArray;
if (user !== "none") {
events = filterItemsByUser(user, eventDataArray);
}
events = events.sort((a, b) => new Date(a.time) - new Date(b.time));
// Respond with a JSON object indicating success or error
if (user == null) {
res.status(500).json({ error: "User was null" });
}
else if (events) {
res.status(200).json(events);
}
else {
res.status(500).json({ error: 'Failed to retrieve events' });
}
});
// Serve uploaded images
app.use('/uploads', express.static('uploads'));
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});