If you've followed along till this point, then the following will be of no surprise to you. Do note that because we are deleting a book, the deleteOne method from mongoose will either return a 1 for successful deletion or a 0 for a failed deletion. For a failed deletion to happen, it will have been an invalid bookId was inputted. Recall that we have already taken care of that in our bookPermission middleware, so no error handling is necessary here.
Once all our error handling will be taken care of by the isAuthenticated and bookPermission middleware. If there are no errors, we should show a success message like the following.
const globalResponseDto = require('../../responses/globalResponseDto')
const catchException = require('../../utils/catchExceptions')
const bookService = require('../../domain/services/book.service')
/**
* Deletes an existing new book listing.
*/
const deleteABook = catchException(async (req, res) => {
const book = await bookService.deleteBookById(req.params.id)
res.json(
globalResponseDto({
status: 'success',
code: 200,
message: `The book with the id: ${book.id} was successfully deleted.`,
data: null,
errors: null
})
)
})
module.exports = deleteABook