Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/marchena96/Paradigma-lab1/llms.txt

Use this file to discover all available pages before exploring further.

This endpoint adds a new book to the library identified by libraryId. The libraryId is taken from the URL route — it is not read from the request body. The request body must include a name; a category is optional and defaults to an empty string when omitted. On success the endpoint returns 201 Created with the newly created book in the response body. If no library with the given libraryId is found, the endpoint returns 404 Not Found.

Endpoint

POST /api/libraries/{libraryId}/books

Authentication

No authentication is required for this endpoint. There is no [Authorize] attribute on the Add action.

Path Parameters

libraryId
integer
required
The unique identifier of the library to which the new book will be added.

Request Body

name
string
required
The title of the book to add.
category
string
The genre or category of the book. Optional — defaults to an empty string ("") if omitted.

Status Codes

StatusMeaning
201 CreatedBook was successfully added; the response body contains the new book object.
404 Not FoundNo library with the supplied libraryId exists.

Response Fields

id
integer
The auto-generated primary key assigned to the new book.
name
string
The title of the newly created book.
category
string
The genre or category of the book. Empty string if not provided in the request.
libraryId
integer
The ID of the library this book was added to. This is always the value taken from the URL route parameter, not the request body.

Example Request

curl -X POST https://localhost:7098/api/libraries/5/books \
  -H "Content-Type: application/json" \
  -d '{"name": "The Norton Anthology of English Literature", "category": "Anthology"}'

Example Response

{"id": 3, "name": "The Norton Anthology of English Literature", "category": "Anthology", "libraryId": 5}
When category is omitted from the request body, the service assigns it an empty string (""). This is required because the Category column in PostgreSQL is NOT NULL — a null value would violate the database constraint.

Controller Source

[HttpPost]
public async Task<IActionResult> Add(int libraryId, BookForm bookForm)
{
    var library = (await _librariesService.Get(new[] { libraryId })).FirstOrDefault();
    if (library == null)
        return NotFound();

    var book = new Book
    {
        Name = bookForm.Name,
        Category = bookForm.Category ?? string.Empty,
        LibraryId = libraryId
    };

    var createdBook = await _booksService.Add(book);
    return CreatedAtAction(nameof(GetAll), new { libraryId = createdBook.LibraryId }, createdBook);
}

Build docs developers (and LLMs) love