-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
256 lines (208 loc) · 7.03 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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
// Importing libraries and initializing them.
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors());
app.set('view-engine', 'ejs');
app.use(express.urlencoded({ extended: false }))
app.use(express.static(__dirname + '/views'));
const port = process.env.PORT
const db = require('./dbService');
//Defining the standard routes
app.get('/', (req, res) => {
res.render('index.ejs');
})
app.get('/restaurant', (req, res) => {
res.render('restaurant.ejs');
})
app.get('/charity', (req, res) => {
res.render('charity.ejs');
})
app.get('/pickup', (req, res) => {
res.render('pickup.ejs');
})
// Restaurant Login and Registration
app.post('/restaurant/login', async (req, res) => {
const userData = req.body;
// Perform registration logic here
db.loginRUser(userData)
.then((user) => {
if (user.restaurantName === userData.username &&
user.password === userData.password) {
const id = user.id;
res.redirect(`/restaurant/${id}`)
} else {
console.log("Username or password incorrect");
}
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
app.post('/restaurant/register', async (req, res) => {
const userData = req.body;
// Perform registration logic here
db.registerRUser(userData)
.then(() => {
res.redirect('/restaurant')
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
// Charity Login and Registration
app.post('/charity/login', async (req, res) => {
const userData = req.body;
// Perform registration logic here
db.loginCUser(userData)
.then((user) => {
if (user.charityName === userData.username &&
user.password === userData.password) {
const id = user.id;
res.redirect(`/charity/${id}`)
} else {
console.log("Username or password incorrect");
}
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
app.post('/charity/register', async (req, res) => {
const userData = req.body;
// Perform registration logic here
db.registerCUser(userData)
.then(() => {
res.redirect('/charity')
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
// Pickup Login and Registration
app.post('/pickup/login', async (req, res) => {
const userData = req.body;
// Perform registration logic here
db.loginPUser(userData)
.then((user) => {
if (user.deliveryagentName === userData.username &&
user.password === userData.password) {
const id = user.id;
res.redirect(`/pickup/${id}`)
} else {
console.log("Username or password incorrect");
}
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
app.post('/pickup/register', async (req, res) => {
const userData = req.body;
// Perform registration logic here
db.registerPUser(userData)
.then(() => {
res.redirect('/pickup')
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
// Get food Items on Restaurant dashboard
app.get('/restaurant/:id', (req, res) => {
const id = req.params.id;
Promise.all([db.getRUser(id), db.getRHistory(id)])
.then((results) => {
const [foodData, DeliveredData] = results;
// Assuming you want to render both sets of data on the same page
res.render('Rhome.ejs', { id, foodData, DeliveredData });
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
// Post food Items by Restaurant
app.post('/restaurant/:id/postFoodItem', async (req, res) => {
const food = req.body;
const id = req.params.id;
// Perform post logic here
db.postFood(food, id)
.then(() => {
res.redirect(`/restaurant/${id}`)
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
// Get food Items on Charity dashboard
app.get('/charity/:id', (req, res) => {
const id = req.params.id;
Promise.all([db.getCUser(id), db.getCHistory(id), db.getCView(id)])
.then((results) => {
const [foodData, DeliveredData, foodView] = results;
// Assuming you want to render both sets of data on the same page
res.render('Chome.ejs', { id, foodData, DeliveredData, foodView });
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
// Update food Items Status by Charity
app.post('/c/updateStatus/:id/:foodId', async (req, res) => {
const idm = req.params.id;
const id = req.params.foodId;
Promise.all([db.updateCStatusi(id, idm)])
.then((user) => {
res.redirect(`/charity/${idm}`)
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
});
// Get food Items on Pickup dashboard
app.get('/pickup/:id', (req, res) => {
const id = req.params.id;
Promise.all([db.getPUser(id), db.getPHistory(id), db.getPView(id)])
.then((results) => {
const [foodData, DeliveredData, foodView] = results;
// Assuming you want to render both sets of data on the same page
res.render('Phome.ejs', { id, foodData, DeliveredData, foodView });
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
})
// Update food Items Status by Pickup
app.post('/p/updateStatus/:id/:foodId', async (req, res) => {
const idm = req.params.id;
const id = req.params.foodId;
Promise.all([db.updatePStatusi(id, idm)])
.then((user) => {
res.redirect(`/pickup/${idm}`)
})
.catch(error => {
res.status(400).json({ success: false, message: error.message });
});
});
// Update food Items Status by Pickup to delivered
app.post('/p1/updateStatus/:id/:foodId', async (req, res) => {
const idm = req.params.id;
const id = req.params.foodId;
Promise.all([db.updatePStatus(id, idm)])
.then((user) => {
res.redirect(`/pickup/${idm}`)
})
.catch(error => {
console.log(error.sqlMessage);
res.status(400).json({ success: false, message: error.message });
});
});
// Logout route
app.delete('/logout', (req, res) => {
res.redirect('/')
})
//Assign a port to start the server
app.listen(port);