-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddroute.js
72 lines (66 loc) · 1.86 KB
/
addroute.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
const express = require("express");
let router = express.Router();
const hotelS = require("./models/hotel");
const reviewS = require("./models/reviews");
const { validationResult } = require("express-validator");
let m = require("./validators.js");
router.post("/hotels", m.form1, (req, res) => {
const errors = validationResult(req);
console.log(req.body);
// console.log(errors);
if (errors.isEmpty()) {
new hotelS({ ...req.body })
.save()
.then((result) => {
res.status(200).send(result);
})
.catch((err) => {
console.log(err);
res.status(402).send(err);
});
} else {
console.log(errors);
res.status(400).send(errors);
}
});
router.post("/reviews", m.form2, async (req, res) => {
const errors = validationResult(req);
if (errors.isEmpty()) {
try {
let review = new reviewS(req.body);
const ReviewSaveResult = await review.save();
const prev = await hotelS.findOne({ name: req.body.name });
let rev = [...prev.rev, ReviewSaveResult._id];
await hotelS.updateOne({ name: req.body.name }, { rev: rev });
await hotelS.findOne({ name: req.body.name });
res.status(200).send(ReviewSaveResult);
} catch (e) {
res.status(400).send(e);
}
} else {
res.status(400).send(errors);
}
/*
review
.save()
.then((result) => {
hotelS.findOne({ name: req.body.name }).then((res) => {
let p = [...res.rev, result._id];
hotelS
.findOneAndUpdate({ name: req.body.name }, { rev: p })
.then((res) => {
console.log(42, res);
})
.catch((e) => console.log(e));
});
})
.catch((error) => {
res.status(400).send(error);
console.log(error);
});
} else {
res.status(400).send(errors);
}
*/
});
module.exports = router;