Skip to content

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

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

Created by: chi-jitera

Overview

This pull request introduces a new PATCH /books/:id endpoint to allow partial updates of book records. This change enhances the existing API by enabling clients to update only specific fields of a book without needing to send the entire book object.

Changes Made

  1. Updated bookController.js

    • Added a new method patchBook to handle PATCH requests for partial updates.
    exports.patchBook = async (req, res) => {
        try {
            const book = await bookService.patchBook(req.params.id, req.body);
            res.json(book);
        } catch (error) {
            res.status(400).json({ error: error.message });
        }
    };
  2. Updated bookRoutes.js

    • Added a new route for the PATCH method to allow partial updates of books.
    router.patch('/books/:id', bookController.patchBook);
  3. Updated bookService.js

    • Added a new method patchBook to handle partial updates of book data.
    exports.patchBook = async (bookId, updateData) => {
        const book = await Book.findByIdAndUpdate(bookId, updateData, { new: true, runValidators: true });
        if (!book) throw new Error('Book not found');
        return book;
    };

These changes collectively enable the functionality for partial updates of book records, improving the flexibility of the API.

Merge request reports