๐Ÿ“ฆ SeolJaeHyeok / My-shopping-mall

๐Ÿ“„ category-router.js ยท 125 lines
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 };