Add PATCH /books/:id endpoint for updating book title and author
Created by: yosuasilalahi-jitera
Overview
This pull request adds support for the PATCH /books/:id endpoint in the src/pages/api/books/[id].ts API route. The new functionality allows clients to update the title and/or author fields of a book by its id, using in-memory storage for demonstration purposes.
Changes
-
PATCH Method Handling:
- Added logic to handle
PATCHrequests in the API route. - Accepts
titleandauthorin the request body. - Validates that at least one of
titleorauthoris provided for update. - Finds the book by
idand updates the provided fields. - Returns the updated book object in the response.
- Added logic to handle
-
Error Handling:
- Returns
400 Bad Requestif theidis invalid or if neithertitlenorauthoris provided. - Returns
404 Not Foundif the book with the specifiediddoes not exist.
- Returns
-
Allowed Methods:
- Ensures only
PATCHandDELETEmethods are allowed, responding with405 Method Not Allowedfor others.
- Ensures only
-
Documentation Note:
- Added comments indicating that this is a demonstration implementation and should be replaced with persistent storage and proper middleware for production use.
Summary
This update improves the books API by enabling partial updates to book resources, following RESTful conventions. It also maintains robust error handling and clear method restrictions.