[JITERA] Add PATCH /books/:id Endpoint for Partial Updates
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
-
Updated
bookController.js- Added a new method
patchBookto 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 }); } }; - Added a new method
-
Updated
bookRoutes.js- Added a new route for the PATCH method to allow partial updates of books.
router.patch('/books/:id', bookController.patchBook); -
Updated
bookService.js- Added a new method
patchBookto 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; }; - Added a new method
These changes collectively enable the functionality for partial updates of book records, improving the flexibility of the API.