|
@@ -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
|