Skip to content

Add PATCH method to /api/books/[id] for updating book title and author

Yosua Silalahi requested to merge feat/api-books-patch-method-1767612966 into main

Created by: yosuasilalahi-jitera

Overview

This pull request adds support for the PATCH HTTP method to the /api/books/[id] endpoint. The new functionality allows clients to update the title and/or author fields of a book by its id. The implementation continues to use in-memory storage for demonstration purposes.

Changes

  • PATCH Method Handling:

    • The API now accepts PATCH requests to /api/books/:id.
    • The request body can include title and/or author fields.
    • At least one of these fields must be provided; otherwise, a 400 Bad Request error is returned.
  • Validation and Error Handling:

    • Validates that the id parameter is a string.
    • Checks that the book exists; returns 404 if not found.
    • Ensures at least one updatable field is present in the request body.
    • Returns appropriate error messages for invalid input or missing book.
  • Book Update Logic:

    • Updates the specified fields (title and/or author) in the in-memory book object.
    • Returns the updated book object in the response.
  • Allowed Methods:

    • The endpoint now explicitly allows only PATCH and DELETE methods.
    • Returns 405 Method Not Allowed for unsupported methods.

Notes

  • This implementation is for demonstration and testing purposes only.
  • For production, replace the in-memory storage with a persistent database (e.g., MongoDB) and add authentication, authorization, and validation as needed.

Merge request reports