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
125import { Router } from "express";
// ํด๋์์ importํ๋ฉด, ์๋์ผ๋ก ํด๋์ index.js์์ ๊ฐ์ ธ์ด
import { categoryService } from "../services";
import { adminService } from "../services";
import { loginRequired, upload } from "../middlewares";
import { contentTypeChecker } from "../utils/content-type-checker";
const categoryRouter = Router();
// ์ ์ฒด ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํ
categoryRouter.get("/", async function (req, res, next) {
try {
// db์์ ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
const categories = await categoryService.getCategories();
// ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก์ JSON ํํ๋ก ํ๋ก ํธ์ ๋ณด๋
res.status(200).json(categories);
} catch (error) {
next(error);
}
});
// ํน์ ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด ์กฐํ
categoryRouter.get("/:id", loginRequired, async function (req, res, next) {
try {
const userId = req.currentUserId;
// ๊ด๋ฆฌ์ ๊ณ์ ๊ฒ์ฆ
await adminService.adminVerify(userId);
const categoryId = req.params.id;
const categoryInfo = await categoryService.getCategoryById(categoryId);
// ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด๋ฅผ JSON ํํ๋ก ํ๋ก ํธ์ ๋ณด๋
res.status(200).json(categoryInfo);
} catch (error) {
next(error);
}
});
// ์นดํ
๊ณ ๋ฆฌ ์ถ๊ฐ
categoryRouter.post(
"/add",
loginRequired,
upload.single("image-file"),
async function (req, res, next) {
try {
contentTypeChecker(req.body);
const userId = req.currentUserId;
// ๊ด๋ฆฌ์ ๊ณ์ ๊ฒ์ฆ
await adminService.adminVerify(userId);
const { location: img } = req.file;
const { name, description } = req.body;
// db์ ์นดํ
๊ณ ๋ฆฌ ์ถ๊ฐ
const newCategory = await categoryService.addCategory({
name,
description,
img,
});
// ์ถ๊ฐํ ์นดํ
๊ณ ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ JSON ํํ๋ก ํ๋ก ํธ์ ๋ณด๋
res.status(201).json(newCategory);
} catch (error) {
next(error);
}
}
);
// ์นดํ
๊ณ ๋ฆฌ ์์
categoryRouter.put(
"/:id/update",
loginRequired,
upload.single("image-file"),
async function (req, res, next) {
try {
contentTypeChecker(req.body);
const userId = req.currentUserId;
// ๊ด๋ฆฌ์ ๊ณ์ ๊ฒ์ฆ
await adminService.adminVerify(userId);
const categoryId = req.params.id;
const { location: img } = req.file;
const { name, description } = req.body;
const toUpdate = {
...(img && { img }),
...(name && { name }),
...(description && { description }),
};
// db์ ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด ์์
const updatedCategoryInfo = await categoryService.setCategory(
categoryId,
toUpdate
);
// ์์ ๋ ์นดํ
๊ณ ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ JSON ํํ๋ก ํ๋ก ํธ์ ๋ณด๋
res.status(200).json(updatedCategoryInfo);
} catch (error) {
next(error);
}
}
);
// ์นดํ
๊ณ ๋ฆฌ ์ญ์
categoryRouter.delete(
"/delete",
loginRequired,
async function (req, res, next) {
try {
const userId = req.currentUserId;
// ๊ด๋ฆฌ์ ๊ณ์ ๊ฒ์ฆ
await adminService.adminVerify(userId);
const categoryIdList = req.body.categoryIdList;
// db์์ ์นดํ
๊ณ ๋ฆฌ ์ญ์
const deleteCategoryInfo = await categoryService.deleteCategory(
categoryIdList
);
// ์ญ์ ๋ ์นดํ
๊ณ ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ JSON ํํ๋ก ํ๋ก ํธ์ ๋ณด๋
res.status(200).json(deleteCategoryInfo);
} catch (error) {
next(error);
}
}
);
export { categoryRouter };