-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
322 lines (263 loc) · 7.63 KB
/
index.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
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors');
const app = express()
app.use(express.json());
app.use(cors());
mongoose.connect("mongodb://localhost:27017/WEBproject")
app.use(express.static(__dirname + '/public'));
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true // Assuming usernames should be unique
},
password: {
type: String,
required: true
},
Gender: {
type: String,
required: false
},
Age: {
type: Number,
required: false
}
});
const ContactSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
message: {
type: String,
required: true
}
});
const AppointmentSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
doctorName: {
type: String,
required: true
},
date: {
type: String,
required: true
},
time: {
type: String,
required: true
}
});
const DoctorSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
type: {
type: String,
required: true
},
study: {
type: String,
required: true
},
location: {
type: String,
required: true
},
waitTime: {
type: String,
required: true
},
experience: {
type: String,
required: true
},
satisfiedPatients: {
type: Number,
required: true
},
rating: {
type: Number,
required: true
},
description: {
type: String,
required: true
},
additionalDescription: {
type: String,
required: true
}
});
const HospitalSchema = new mongoose.Schema({
Name: {
type: String,
required: true
},
Address: {
type: String,
required: true
},
Description: {
type: String,
required: true
},
AdditionalDescription: {
type: String,
required: true
},
Location: {
type: String,
required: true
},
TypesofDoctors: {
type: String,
required: true
},
});
const UserModel = mongoose.model("users", UserSchema)
const ContactModel = mongoose.model("contact", ContactSchema)
const AppointmentModel = mongoose.model("appointments", AppointmentSchema);
const DoctorModel = mongoose.model("doctors", DoctorSchema);
const HospitalModel = mongoose.model("hospitals", HospitalSchema);
app.get("/getUsers", (req, res) => {
UserModel.find({}).then(function (users) {
res.json(users)
}).catch(function (err) {
console.log(err)
})
})
app.get("/getDoctor", (req, res) => {
DoctorModel.find({}).then(function (users) {
res.json(users)
}).catch(function (err) {
console.log(err)
})
})
app.post("/signup", async (req, res) => {
try {
// Check if user with the same name already exists
let existingUser = await UserModel.findOne({ username: req.body.username })
if (existingUser) {
return res.status(400).json({ message: "User already exists" });
}
// Create a new user document
const newUser = new UserModel({ username: req.body.username, password: req.body.password, Gender: req.body.Gender, Age: req.body.Age });
// Save the new user to the database
await newUser.save();
res.status(201).json({ message: "User created successfully" });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
app.post("/bookAppointment", async (req, res) => {
try {
const newAppointment = new AppointmentModel({ name: req.body.name, doctorName: req.body.doctorName, date: req.body.date, time: req.body.time });
// Save the new user to the database
await newAppointment.save();
res.status(201).json({ message: "Appointment booked successfully" });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
app.post("/login", async (req, res) => {
if (req.body.password && req.body.username) {
let user = await UserModel.findOne(req.body).select("-password")
if (user) {
res.send('Logged in')
}
else {
res.send('No user found')
}
}
else {
res.send('username or password is empty')
}
})
app.post("/contact", async (req, res) => {
try {
const contactEntry = new ContactModel({ username: req.body.username, message: req.body.message });
await contactEntry.save();
res.status(201).json({ message: "Entered Successfully" });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
app.post('/getAppointments', async (req, res) => {
const userName = req.body.name;
AppointmentModel.find({ name: userName })
.then(function (appointments) {
res.json(appointments);
})
.catch(function (err) {
console.log(err);
res.status(500).json({ error: 'Internal server error' }); // Sending an error response
});
});
app.post('/deleteAppointment', async (req, res) => {
const appointmentData = req.body;
try {
const deletedAppointment = await AppointmentModel.findOneAndDelete(appointmentData);
if (!deletedAppointment) {
return res.status(404).json({ message: 'Appointment not found' });
}
res.status(200).json({ message: 'Appointment deleted successfully' });
} catch (error) {
console.error('Error deleting appointment:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/updateAppointment', async (req, res) => {
const { name, doctorName, date, time, newDate, newTime } = req.body;
try {
// Find the appointment based on provided data
const appointment = await AppointmentModel.findOne({ name: name, doctorName: doctorName, date: date, time: time });
if (!appointment) {
return res.status(404).json({ message: 'Appointment not found' });
}
// Update date and time fields with new values
appointment.date = newDate;
appointment.time = newTime;
// Save the updated appointment
await appointment.save();
res.status(200).json({ message: 'Appointment updated successfully' });
} catch (error) {
console.error('Error updating appointment:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/setAppointment', async (req, res) => {
try {
const appointmentEntry = new AppointmentModel({ name: req.body.name, doctorName: req.body.doctorName });
await appointmentEntry.save();
res.status(201).json({ message: "Entered Successfully" });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
app.get("/getHospital", (req, res) => {
HospitalModel.find({})
.then(function (hospitals) {
res.json(hospitals);
})
.catch(function (err) {
console.log(err);
res.status(500).json({ error: 'Internal server error' }); // Sending an error response
});
});
app.get('*', function (req, res) {
res.send("Invalid end point entered")
})
app.listen(3001, () => {
console.log("server is running")
})