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
| // GET
// "/home" 라우트
router.get(
["/", "/order"],
asynchandler(async (req, res) => {
const contacts = await Contact.find();
res.render("order", { contacts: contacts, layout: mainLayout }); //미리 설정한 레이아웃 사용
})
);
// POST
router.post(
"/newOrder",
asynchandler(async (req, res) => {
const { devicename, casewhat, sangtae, name, phone, email } = req.body;
if(!devicename || !casewhat || !sangtae || !name || !phone || !email) {
return res.status(400).send('필수값이 입력되지 않았습니다.');
}
const contact = await Contact.create({
devicename,
casewhat,
sangtae,
name,
phone,
email,
});
// res.status(201).send("사용자 데이터 생성됨");
res.redirect("/order"); //mid add
})
)
// PUT
router.put(
"/order/:id",
asynchandler(async (req, res) => {
const id = req.params.id;
const { devicename, casewhat, sangtae, name, phone, email } = req.body;
const contact = await Contact.findById(id);
if(!contact) {
res.status(404);
throw new Error('사용자 데이터 찾을 수 없음');
}
// 수정
contact.devicename = devicename;
contact.casewhat = casewhat;
contact.sangtae = sangtae;
contact.name = name;
contact.email = email;
contact.phone = phone;
// 저장
contact.save();
//mid add
res.redirect("/order");
})
)
// DELETE
router.delete(
"/delete/:id",
asynchandler(async (req, res) => {
await Contact.deleteOne({_id: req.params.id});
res.redirect('/order');
})
);
|