Skip to content

Commit

Permalink
selesai mengerjakan delete dan update product
Browse files Browse the repository at this point in the history
  • Loading branch information
sintaanggunpuspita committed Dec 17, 2020
1 parent 4e1bc35 commit 2aa73de
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
57 changes: 57 additions & 0 deletions controllers/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,62 @@ module.exports = {
message: 'Internal server error!'
})
}
},
deleteProduct: async (req, res) => {
try {
const products = await productModel.getProductById(req.params.id)
if (!products[0]) return res.status(404).send({
status: 'Failed',
statusCode: 404,
message: 'Product not found!'
})

await productModel.deleteProduct(req.params.id)
return res.status(200).send({
status: 'Success',
statusCode: 200,
deleteId: req.params.id,
message: 'Product deleted'
})
} catch (error) {
console.log(error)
return res.status(500).send({
status: 'Failed',
statusCode: 500,
message: 'Internal server error!'
})
}
},
updateProduct: async (req, res) => {
try {
const products = await productModel.getProductById(req.params.id)
if (!products[0]) return res.status(404).send({
status: 'Failed',
statusCode: 404,
message: 'Product not found!'
})

const newProductData = {
name: req.body.name || products[0].name,
price: req.body.price || products[0].price,
description: req.body.description || products[0].description
}

await productModel.updateProduct(newProductData, req.params.id)

return res.status(201).send({
status: 'Success',
statusCode: 201,
updateId: req.params.id,
message: 'Update product success'
})
} catch (error) {
console.log(error)
return res.status(500).send({
status: 'Failed',
statusCode: 500,
message: 'Internal server error!'
})
}
}
}
18 changes: 18 additions & 0 deletions models/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,23 @@ module.exports = {
.catch(error => {
throw error
})
},
deleteProduct: (id) => {
return db(`DELETE FROM products WHERE id = '${id}'`)
.then(result => {
return result
})
.catch(error => {
throw error
})
},
updateProduct: (data, id) => {
return db(`UPDATE products SET name = '${data.name}', description = '${data.description}', price = ${data.price} WHERE id = '${id}'`)
.then(result => {
return result
})
.catch(error => {
throw error
})
}
}
4 changes: 3 additions & 1 deletion routes/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ const router = require('express').Router()
module.exports = router
.get('/', verifyUser, product.getAllProducts)
.get('/:id', verifyUser, product.getProductById)
.post('/create', [verifyUser, verifyRole, upload], product.createProduct)
.post('/create', [verifyUser, verifyRole, upload], product.createProduct)
.patch('/:id', [verifyUser, verifyRole], product.updateProduct)
.delete('/:id', [verifyUser, verifyRole], product.deleteProduct)

0 comments on commit 2aa73de

Please sign in to comment.