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
101import { Router } from "express";
// ํด๋์์ importํ๋ฉด, ์๋์ผ๋ก ํด๋์ index.js์์ ๊ฐ์ ธ์ด
import { loginRequired } from "../middlewares";
import { orderService } from "../services";
import { contentTypeChecker } from "../utils/content-type-checker";
const orderRouter = Router();
// ๋ด ์ฃผ๋ฌธ ๋ชฉ๋ก ์กฐํ
orderRouter.get("/orderList", loginRequired, async function (req, res, next) {
try {
// ํ ํฐ์์ userId ์ถ์ถ
const userId = req.currentUserId;
// db์ ์ฃผ๋ฌธ ๋ชฉ๋ก ์กฐํ
const orderInfo = await orderService.getOrders(userId);
// front์ ๋ฐ์ดํฐ ์ ๋ฌ
res.status(200).json(orderInfo);
} catch (error) {
next(error);
}
});
// ์ฃผ๋ฌธํ๊ธฐ
orderRouter.post("/complete", loginRequired, async (req, res, next) => {
try {
// Content-Type ์ฒดํฌ
contentTypeChecker(req.body);
// ํ ํฐ์์ userId ์ถ์ถ
const userId = req.currentUserId;
const {
addressName,
receiverName,
receiverPhoneNumber,
postalCode,
address1,
address2,
request,
orderList,
totalPrice,
shippingFee,
} = req.body;
// ๊ฐ ์ฃผ์ ์ ๋ณด๋ฅผ ํ๋์ address ๊ฐ์ฒด์ ์ฝ์
const address = {
addressName,
receiverName,
receiverPhoneNumber,
postalCode,
address1,
address2,
};
// ํ ๋ฒ ์ฃผ๋ฌธํ ๋ ์ํ์ด ์ฌ๋ฌ๊ฐ ์ด๋ฏ๋ก, ๊ฐ์ฒด๋ก ๋ฐ์ ๋ฐฐ์ด๋ก ๋ณํํ๋ค.
const wholeorderList = Object.values(orderList).map((e) => ({
productId: e.productId,
title: e.title,
quantity: e.quantity,
price: e.price,
status: e.status,
}));
// db์ ์ฃผ๋ฌธ ์ ๋ณด ์ถ๊ฐ
const newOrder = await orderService.addOrder({
userId,
address,
request,
orderList: wholeorderList,
totalPrice,
shippingFee,
});
// front์ ์๋ก ์ถ๊ฐ๋ ์ฃผ๋ฌธ ์ ๋ณด ์ ๋ฌ
res.status(201).json(newOrder);
} catch (error) {
next(error);
}
});
// ์ฃผ๋ฌธ ์ํ ์ญ์
orderRouter.delete("/delete", loginRequired, async function (req, res, next) {
try {
// Content-Type ์ฒดํฌ
contentTypeChecker(req.body);
const { orderId, productId } = req.body;
// ํ ํฐ์์ userId ์ถ์ถ
const userId = req.currentUserId;
// db์ ์ฃผ๋ฌธ์ ํน์ ์ํ ์ญ์
const deleteOrderInfo = await orderService.deleteOrderProduct({
orderId,
productId,
userId,
});
// front์ ์ฃผ๋ฌธ ์ ๋ณด ์ ๋ฌ
res.status(200).json(deleteOrderInfo);
} catch (error) {
next(error);
}
});
export { orderRouter };