Skip to content

Add PATCH /books/:id endpoint for updating book title and author

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

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 PATCH requests in the API route.
    • Accepts title and author in the request body.
    • Validates that at least one of title or author is provided for update.
    • Finds the book by id and updates the provided fields.
    • Returns the updated book object in the response.
  • Error Handling:

    • Returns 400 Bad Request if the id is invalid or if neither title nor author is provided.
    • Returns 404 Not Found if the book with the specified id does not exist.
  • Allowed Methods:

    • Ensures only PATCH and DELETE methods are allowed, responding with 405 Method Not Allowed for others.
  • 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.

Merge request reports