Skip to content

[JITERA] Implement PATCH /books/:id Endpoint for Partial Book Updates

chi le requested to merge feat/implement-patch-book-endpoint-1759139209 into main

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.
  • /controllers/bookController.js:

    • Implemented the patchBook handler, which calls the bookService.patchBook method.
    • 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.
  • /services/bookService.js:

    • Developed the patchBook method 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.

This implementation follows the project's conventions for error handling, request/response format, and partial update best practices. No further changes are necessary.

Merge request reports