Skip to content

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

chi le requested to merge feat/implement-patch-book-api-1759138630 into main

Created by: chi-jitera

Overview

This pull request implements the PATCH /books/:id API endpoint, allowing for partial updates of book information. The implementation ensures that if a book does not exist, a 404 error is returned, and for validation errors, a 400 error is returned.

Changes

  1. Controller Implementation:

    • In /controllers/bookController.js, the patchBook function has been defined. This function calls bookService.patchBook to handle the logic for updating book information.
    • Error handling has been added to return appropriate status codes based on the type of error encountered (404 for not found, 400 for validation errors).
    exports.patchBook = async (req, res) => {
        try {
            const book = await bookService.patchBook(req.params.id, req.body);
            res.json(book);
        } catch (error) {
            const status = error.message === 'Book not found' ? 404 : 400;
            res.status(status).json({ error: error.message });
        }
    };
  2. Route Definition:

    • In /routes/bookRoutes.js, the route for the PATCH method has been defined to link the endpoint to the patchBook controller function.
    router.patch('/books/:id', bookController.patchBook);

This implementation allows clients to send a PATCH request to update specific fields of a book, enhancing the API's functionality and usability.

Merge request reports