Created
May 6, 2018 04:52
-
-
Save budasuyasa/b3df9eddbd6740157ea057df632b6073 to your computer and use it in GitHub Desktop.
medium-post-simple-rest-07
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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