[JITERA] Add patchBook function to bookService
Created by: chi-jitera
Overview
This pull request introduces a new function patchBook in the bookService.js file, which allows for partial updates of book fields. This function validates only the fields that are provided in the update request, ensuring that the rest of the book's data remains unchanged.
Changes Explained
-
Added
patchBookfunction:- This function retrieves the existing book by its ID.
- It checks if the book exists; if not, it throws a 'Book not found' error.
- It validates the
publishedYearfield if it is present in the update data, ensuring it is not set in the future. - It updates only the fields that are present in the
updateDataobject, preserving the rest of the book's data. - Finally, it saves the updated book and returns it.
-
Validation Logic:
- The validation logic for
publishedYearis reused from the existingvalidateBookDatafunction to maintain consistency across book creation and updates.
- The validation logic for
This enhancement improves the flexibility of the book management system by allowing users to update only specific fields without needing to resend the entire book object.