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
181import { Router } from "express";
import { loginRequired } from "../middlewares";
import { productService } from "../services";
import { upload } from "../middlewares";
import { contentTypeChecker } from "../utils/content-type-checker";
const productRouter = Router();
// μ 체 μν λͺ©λ‘ μ‘°ν
productRouter.get("/", async function (req, res, next) {
try {
// dbμμ μ 체 μν λͺ©λ‘ κ°μ Έμ΄
const products = await productService.getProducts();
// μν λͺ©λ‘(λ°°μ΄)μ JSON ννλ‘ νλ‘ νΈμ 보λ
res.status(200).json(products);
} catch (error) {
next(error);
}
});
//μν μμΈμ 보 μ‘°ν
productRouter.get("/:id", async function (req, res, next) {
try {
const productId = req.params.id;
//dbμμ μν μ 보λ₯Ό κ°μ Έμ΄
const productInfo = await productService.getProductByProductId(productId);
// μν μ 보λ₯Ό JSON ννλ‘ νλ‘ νΈμ 보λ
res.status(200).json(productInfo);
} catch (error) {
next(error);
}
});
// μΉ΄ν
κ³ λ¦¬λ³ μν λͺ©λ‘ μ‘°ν
productRouter.get("/category/:category", async (req, res, next) => {
try {
const category = req.params.category;
// νΉμ μΉ΄ν
κ³ λ¦¬μ λ§λ μν λͺ©λ‘μ μ»μ
const products = await productService.getProductsByCategory(category);
// μν λͺ©λ‘(λ°°μ΄)μ JSON ννλ‘ νλ‘ νΈμ 보λ
res.status(200).json(products);
} catch (error) {
next(error);
}
});
//μν μμ μν΄ μν λ°μ΄ν° μ‘°ν
productRouter.get(
"/:id/update",
loginRequired,
async function (req, res, next) {
try {
// ν ν°μμ userId μΆμΆ
const userId = req.currentUserId;
const productId = req.params.id;
// dbμμ userκ° λ±λ‘ν μνμ κ°μ Έμ΄
const productInfo = await productService.getProductForUpdate(
productId,
userId
);
// μν λ°μ΄ν°λ₯Ό JSON ννλ‘ νλ‘ νΈμ 보λ
res.status(200).json(productInfo);
} catch (error) {
next(error);
}
}
);
// μν νλ§€
productRouter.post(
"/add",
upload.single("image-file"),
loginRequired,
async (req, res, next) => {
try {
// Content-Type 체ν¬
contentTypeChecker(req.body);
const userId = req.currentUserId;
const { location: img } = req.file;
const {
name,
price,
category,
quantity,
brandName,
keyword,
shortDescription,
detailDescription,
} = req.body;
// μ λ°μ΄ν°λ₯Ό μν dbμ μΆκ°
const newProduct = await productService.addProduct({
name,
price,
img,
category,
quantity,
brandName,
keyword,
shortDescription,
detailDescription,
userId,
});
// μΆκ°λ μνμ λ°μ΄ν°λ₯Ό JSON ννλ‘ νλ‘ νΈμ 보λ
res.status(201).json(newProduct);
} catch (error) {
next(error);
}
}
);
//μν μ 보 μμ
productRouter.put(
"/:id/update",
loginRequired,
upload.single("image-file"),
async function (req, res, next) {
try {
contentTypeChecker(req.body);
const userId = req.currentUserId;
const productId = req.params.id;
const { location: img } = req.file;
const {
name,
price,
category,
quantity,
brandName,
keyword,
shortDescription,
detailDescription,
} = req.body;
const toUpdate = {
...(img && { img }),
...(name && { name }),
...(price && { price }),
...(category && { category }),
...(quantity && { quantity }),
...(brandName && { brandName }),
...(keyword && { keyword }),
...(shortDescription && { shortDescription }),
...(detailDescription && { detailDescription }),
};
// μν μ 보λ₯Ό μ
λ°μ΄νΈν¨.
const updatedProductInfo = await productService.setProduct(
userId,
productId,
toUpdate
);
// μ
λ°μ΄νΈν μν μ 보λ₯Ό JSON ννλ‘ νλ‘ νΈμ 보λ
res.status(200).json(updatedProductInfo);
} catch (error) {
next(error);
}
}
);
//μν νλ§€ μμ
productRouter.delete("/delete", loginRequired, async function (req, res, next) {
try {
// Content-Type 체ν¬
contentTypeChecker(req.body);
const productIdList = req.body.productIdList;
const userId = req.currentUserId;
// νλ§€μ κ³μ κ²μ¦
await productService.checkProductsForDelete(userId, productIdList);
// dbμ νμ μ 보 μμ
const deleteProductInfo = await productService.deleteProduct(productIdList);
// μν μμ μ 보 λ°μ΄ν°λ₯Ό JSON ννλ‘ νλ‘ νΈμ 보λ
res.status(200).json(deleteProductInfo);
} catch (error) {
next(error);
}
});
export { productRouter };