[JITERA] Implement PATCH /books/:id API for Partial Book Updates
Created by: chi-jitera
Overview
This pull request implements the PATCH /books/:id API endpoint, allowing for partial updates of book information. The implementation ensures that if a book does not exist, a 404 error is returned, and for validation errors, a 400 error is returned.
Changes
-
Controller Implementation:
- In
/controllers/bookController.js, thepatchBookfunction has been defined. This function callsbookService.patchBookto handle the logic for updating book information. - Error handling has been added to return appropriate status codes based on the type of error encountered (404 for not found, 400 for validation errors).
exports.patchBook = async (req, res) => { try { const book = await bookService.patchBook(req.params.id, req.body); res.json(book); } catch (error) { const status = error.message === 'Book not found' ? 404 : 400; res.status(status).json({ error: error.message }); } }; - In
-
Route Definition:
- In
/routes/bookRoutes.js, the route for the PATCH method has been defined to link the endpoint to thepatchBookcontroller function.
router.patch('/books/:id', bookController.patchBook); - In
This implementation allows clients to send a PATCH request to update specific fields of a book, enhancing the API's functionality and usability.