[JITERA] Implement PATCH /books/:id Endpoint for Partial Book Updates
Created by: chi-jitera
Overview
This pull request implements the PATCH /books/:id endpoint, allowing for partial updates to book records in the database. This feature enhances the existing functionality by enabling clients to update only specific fields of a book without needing to send the entire book object.
Changes Explained
-
/routes/bookRoutes.js:- Registered the PATCH endpoint using
router.patch('/books/:id', bookController.patchBook);. This allows clients to send PATCH requests to update specific fields of a book.
- Registered the PATCH endpoint using
-
/controllers/bookController.js:- Implemented the
patchBookhandler, which calls thebookService.patchBookmethod. - Added error handling for 404 (book not found) and 400 (validation errors) to ensure proper response codes are returned based on the outcome of the operation.
- Implemented the
-
/services/bookService.js:- Developed the
patchBookmethod that:- Finds the book by ID.
- Performs partial validation on the fields present in the update request.
- Updates only the provided fields in the book object.
- Saves the updated book back to the database.
- Ensured that the method adheres to best practices for error handling and validation.
- Developed the
This implementation follows the project's conventions for error handling, request/response format, and partial update best practices. No further changes are necessary.