Add PATCH method to /api/books/[id] for updating book title and author
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
titleand/orauthorfields. - At least one of these fields must be provided; otherwise, a 400 Bad Request error is returned.
- The API now accepts PATCH requests to
-
Validation and Error Handling:
- Validates that the
idparameter 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.
- Validates that the
-
Book Update Logic:
- Updates the specified fields (
titleand/orauthor) in the in-memory book object. - Returns the updated book object in the response.
- Updates the specified fields (
-
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.