Skip to content

BE-2: Create Book Record #2

@tecnodeveloper

Description

@tecnodeveloper

Implement a POST /book endpoint that creates a new book record in the database. The endpoint must validate input and persist valid data.

User Story

Given I hit the /book endpoint with a POST request
When I provide valid book data
Then it should create a record in the database

Request Body

{
  "id": 1,
  "name": "Book Name",
  "author": ["Author One"],
  "price": 100,
  "publisher": "Publisher Name"
}

Validation Rules

  • id

    • Must be a number
  • name

    • Must be a string
    • Required
  • author

    • Must be an array
    • Required
  • price

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

    • Must be a string
    • Optional

Tasks

  • Create /book POST route
  • Implement input validation logic
  • Handle validation errors (return 400 with message)
  • Connect to database
  • Insert record into books table
  • Return success response (201 Created)
  • Add Swagger documentation for endpoint
    • Define request body schema
    • Add example payload
    • Define responses (201, 400)

Acceptance Criteria

  • Endpoint accepts valid JSON input
  • Invalid input returns 400 Bad Request
  • Negative price is rejected
  • Valid request creates a record in DB
  • Response returns 201 Created
  • Swagger UI allows testing the endpoint

Testing Steps

  • Open Swagger UI

  • Send POST request to /book with valid payload

  • Verify response is 201 Created

  • Run SQL query:

    SELECT * FROM books
    ORDER BY id DESC
    LIMIT 1;
  • Confirm returned record matches inserted data

Definition of Done

  • Endpoint implemented and working
  • Validation is enforced
  • Data persists correctly in DB
  • Swagger docs updated
  • Code is linted and formatted

Metadata

Metadata

Labels

No labels
No labels

Projects

Status

In progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions