2 Commity 3d8488e9c9 ... 2709e247ac

Autor SHA1 Wiadomość Data
  허용운 2709e247ac 상품 삭제 5 lat temu
  허용운 9375661594 상품 수정 추가 5 lat temu
1 zmienionych plików z 47 dodań i 0 usunięć
  1. 47 0
      routes/products.js

+ 47 - 0
routes/products.js

@@ -54,5 +54,52 @@ router.get('/products/:id', async (req, res) => {
   }
 })
 
+router.put('/products/:id', upload.single('photo'), async (req, res) => {
+  try {
+    let product = await Product.findOneAndUpdate( 
+      { _id: req.params.id }, 
+      {
+        $set: {
+          title: req.body.title,
+          price: req.body.price,
+          category: req.body.categoryID,
+          description: req.body.description,
+          photo: req.file.location,
+          stockQuantity: req.body.stockQuantity,
+          owner: req.body.ownerID
+        }
+      },
+      { upsert: true }
+    )
+    
+    res.json({
+      success: true,
+      updateProduct: product
+    })
+  } catch (err) {
+    res.status(500).json({
+      success: false,
+      message: err.message
+    })
+  }
+})
+
+router.delete('/products/:id', async (req, res) => {
+  try {
+    let deleteProduct = await Product.findOneAndDelete( { _id: req.params.id })
+    
+    if (deleteProduct) {
+      res.json({
+        success: true,
+        message: 'Successfully deleted'
+      })
+    }
+  } catch (err) {
+    res.status(500).json({
+      success: false,
+      message: err.message
+    })
+  }
+})
 
 module.exports = router