Skip to content

Instantly share code, notes, and snippets.

@budasuyasa
Created May 6, 2018 04:52
Show Gist options
  • Save budasuyasa/b3df9eddbd6740157ea057df632b6073 to your computer and use it in GitHub Desktop.
Save budasuyasa/b3df9eddbd6740157ea057df632b6073 to your computer and use it in GitHub Desktop.
medium-post-simple-rest-07
app.delete('/book/:isbn',[
//Set form validation rule
check('isbn')
.isLength({ min: 5 })
.isNumeric()
.custom(value => {
return book.findOne({where: {isbn: value}}).then(b => {
if(!b){
throw new Error('ISBN not found');
}
})
}
),
], (req, res) => {
book.destroy({where: {isbn: req.params.isbn}})
.then(affectedRow => {
if(affectedRow){
return {
"status":"success",
"message": "Book deleted",
"data": null
}
}
return {
"status":"error",
"message": "Failed",
"data": null
}
})
.then(r => {
res.json(r)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment