πŸ“¦ SeolJaeHyeok / My-shopping-mall

πŸ“„ product-router.js Β· 181 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
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 };