Skip to content

BE-3: Edit Book Record #3

@tecnodeveloper

Description

@tecnodeveloper

Implement an endpoint to update an existing book record in the database. The endpoint should validate input and only update the specified book.

User Story

Given a book already exists
When I send a request to update the book
Then the book record should be updated in the database

Endpoint

PUT /book/:id

Request Body

{
  "name": "Updated Book Name",
  "author": ["Updated Author"],
  "price": 150,
  "publisher": "Updated Publisher"
}

Validation Rules

  • id (from params)

    • Must be a number
    • Must exist in DB
  • name

    • Must be a string
    • Optional (but if provided, must be valid)
  • author

    • Must be an array
    • Optional
  • price

    • Must be a number
    • Must be >= 0 (no negative values)
    • Optional
  • publisher

    • Must be a string
    • Optional

Tasks

  • Create PUT /book/:id route

  • Create/update controller for edit functionality

  • Validate route param (id)

  • Validate request body fields

  • Check if book exists

    • Return 404 Not Found if not exists
  • Update record in books table

  • Return success response

  • Add Swagger documentation

    • Define path param
    • Define request schema
    • Add responses (200, 400, 404)

Acceptance Criteria

  • Valid request updates the book record
  • Partial updates are allowed
  • Invalid input returns 400 Bad Request
  • Updating non-existent book returns 404 Not Found
  • Negative price is rejected
  • Response returns updated record or success message
  • Swagger UI supports testing this endpoint

Testing Steps

  • Create a book (via POST /book)

  • Copy its id

  • Send PUT request to:

    /book/:id
    
  • Provide updated fields

  • Verify response is 200 OK

  • Run SQL query:

    SELECT * FROM books
    WHERE id = <your_id>;
  • Confirm fields are updated correctly

Definition of Done

  • Endpoint implemented and working
  • Validation handled properly
  • Non-existent records handled correctly
  • Data updated in DB
  • Swagger docs updated
  • Code is linted and formatted

Metadata

Metadata

Labels

No labels
No labels

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions