-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (57 loc) · 1.78 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
const express = require("express")
const mongoose = require("mongoose")
const cors = require("cors")
const app = express()
const StudentModel = require("./models/Students")
app.use(cors())
app.use(express.json())
mongoose.connect("mongodb://localhost:27017/student")
app.listen(3001, () => {
console.log("ABD STARTED TO CONQUER THE WORLD")
})
app.post("/register", (request, response) => {
const { email } = request.body
StudentModel.findOne({ email: email })
.then(existing => {
if (existing) {
response.json("Account Already Existing")
}
else {
StudentModel.create(request.body)
.then(NewStudent => { (response.json(NewStudent)) })
.catch(err => (response.json(err)))
}
}
)
.catch(err => { response.json(err) })
}
)
app.post("/login", (request, response) => {
const { email, password } = request.body
StudentModel.findOne({ email: email })
.then(user => {
if (user.password===password) {
response.json(user._id)
}
else {
response.json("Invalid")
}
})
.catch(err => { response.json(err) })
})
app.put("/room_booking", async (req, res) => {
const { id, room } = req.body;
try {
const student = await StudentModel.findById({_id:id});
if (student.roomNumber !== room) {
student.roomNumber = room;
await student.save();
return res.json("Success");
} else {
return res.json("Invalid");
}
} catch (err) {
console.log(err);
return res.json("Ivalid");
}
});