Skip to content

[JITERA] Add PATCH /books/:id endpoint for partial updates

chi le requested to merge feat/add-patch-books-endpoint-1757337513 into main

Created by: chi-jitera

Overview

This pull request introduces a new PATCH endpoint for updating book records in the application. The addition of the PATCH /books/:id route allows for partial updates to book resources, adhering to RESTful best practices.

Changes

  1. Route Definition:

    • Added a new route in /routes/bookRoutes.js for handling PATCH requests.
    router.patch('/books/:id', bookController.updateBook); // PATCH for partial updates
  2. Controller Logic:

    • The existing updateBook function in /controllers/bookController.js is utilized for both PUT and PATCH requests. This function is capable of handling partial updates due to Mongoose's findByIdAndUpdate method, which supports updating only the fields provided in the request body.
  3. Service Logic:

    • The service function updateBook in /services/bookService.js remains unchanged, as it already accommodates partial updates through Mongoose's capabilities.
  4. Model Definition:

    • The Mongoose schema in /models/Book.js is unaffected by this change, as the PATCH functionality leverages the existing model structure.

This implementation ensures that clients can now perform partial updates on book records, enhancing the flexibility and usability of the API.

Merge request reports