Skip to content

[JITERA] Add patchBook function to bookService

chi le requested to merge feat/patch-book-function-1759137497 into main

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 patchBook function:

    • 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 publishedYear field 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 updateData object, preserving the rest of the book's data.
    • Finally, it saves the updated book and returns it.
  • Validation Logic:

    • The validation logic for publishedYear is reused from the existing validateBookData function to maintain consistency across book creation and updates.

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.

Merge request reports