Skip to main content

Overview

The LibroService provides methods to create, read, update, and delete books in the Biblioteca Virtual system. It handles all HTTP communications with the backend /libros endpoint. Location: src/app/core/services/libro.ts

Constructor

Uses Angular’s inject() function to obtain HttpClient instance.

Properties

string
default:"${environment.apiUrl}/libros"
Base URL for libro endpoints, configured from environment settings.

Methods

getAll()

Retrieves all books from the library.
Returns: Observable<Libro[]> - Array of all books
array
Array of book objects
number
Unique book identifier
string
Book title
string
URL to the book cover image
number
Year of publication
boolean
Whether the book is available for borrowing
Genero
Book’s genre (id and nombre)
Autor[]
Array of authors (id, nombre, urlFoto)
Example:
API Endpoint: GET ${apiUrl}

getById()

Retrieves a specific book by its ID.
number
required
The unique identifier of the book to retrieve
Returns: Observable<Libro> - Single book object Example:
API Endpoint: GET ${apiUrl}/{id} Use Case: Loading book data for editing forms

create()

Creates a new book in the library.
object
required
Book data to create. Must include genero and autores.Required Properties:
  • titulo (string): Book title
  • disponible (boolean): Availability status
  • genero (Genero): Genre object with id and nombre
  • autores (Autor[]): Array of author objects
Optional Properties:
  • portada (string): Cover image URL
  • anioPublicacion (number): Publication year
Returns: Observable<Libro> - The created book with server-assigned ID Example:
API Endpoint: POST ${apiUrl}

update()

Updates an existing book.
number
required
The ID of the book to update
object
required
Updated book data. Should include all required fields (titulo, disponible, genero, autores)
Returns: Observable<Libro> - The updated book object Example:
API Endpoint: PUT ${apiUrl}/{id}

delete()

Deletes a book from the library.
number
required
The ID of the book to delete
Returns: Observable<any> - Plain text response from server Response Type: Text (uses responseType: 'text' to handle empty or plain text responses) Example:
API Endpoint: DELETE ${apiUrl}/{id}

Complete CRUD Example

Example component using all LibroService methods:

Data Models

Libro Interface

Error Handling

All methods return Observables that may emit errors:

See Also