Skip to content

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

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

Created by: chi-jitera

Overview

This pull request introduces a new endpoint to allow partial updates of book records via a PATCH request. The new functionality is implemented in the bookRoutes.js and bookController.js files, along with necessary changes in the bookService.js to handle the update logic.

Changes

  1. Updated bookRoutes.js:

    • Added a new route for the PATCH method to handle requests to /books/:id.
    • This route maps to the patchBook method in the bookController.
    // routes/bookRoutes.js
    router.patch('/books/:id', bookController.patchBook);
  2. Implemented patchBook method in bookController.js:

    • Created the patchBook method to handle the logic for updating book records.
    • The method validates the input and updates only the fields provided in the request body.
    // controllers/bookController.js
    async patchBook(req, res) {
        const { id } = req.params;
        const updates = req.body;
        // Validate and update logic here
    }
  3. Updated bookService.js:

    • Implemented the patchBook method to perform the actual update in the database.
    • Ensured that only the fields present in the request body are updated.
    // services/bookService.js
    async patchBook(id, updates) {
        // Logic to update the book in the database
    }

This new functionality will enhance the API by allowing clients to update only specific fields of a book without needing to send the entire book object.

Merge request reports