Overview
TheGeneroService provides methods to create, read, update, and delete book genres in the Biblioteca Virtual system. It handles all HTTP communications with the backend /generos endpoint.
Location: src/app/core/services/genero.ts
Constructor
inject() function to obtain HttpClient instance.
Properties
string
default:"${environment.apiUrl}/generos"
Base URL for genero endpoints, configured from environment settings.
Methods
getAll()
Retrieves all genres from the database.Observable<Genero[]> - Array of all genres
array
GET ${apiUrl}
getById()
Retrieves a specific genre by its ID.number
required
The unique identifier of the genre to retrieve
Observable<Genero> - Single genre object
Example:
GET ${apiUrl}/{id}
Use Case: Loading genre data for editing forms
create()
Creates a new genre in the database.Genero
required
Genre data to createRequired Properties:
nombre(string): Genre name
id(number): Should be omitted for new genres (server assigns)
Observable<Genero> - The created genre with server-assigned ID
Example:
POST ${apiUrl}
update()
Updates an existing genre.number
required
The ID of the genre to update
Genero
required
Updated genre dataProperties:
id(number): Genre ID (should match path parameter)nombre(string): Updated genre name
Observable<Genero> - The updated genre object
Example:
PUT ${apiUrl}/{id}
delete()
Deletes a genre from the database.number
required
The ID of the genre to delete
Observable<any> - Plain text response from server
Response Type: Text (uses responseType: 'text' to accept string responses from backend)
Example:
DELETE ${apiUrl}/{id}
Note: Deletion may fail if the genre has associated books. Handle this error appropriately in your UI.
Complete CRUD Example
Example component using all GeneroService methods:Form Example
Example form component for creating/editing genres:Usage in Forms
GeneroService is commonly used to populate dropdown selectors when creating/editing books:Data Model
Genero Interface
Error Handling
Common error scenarios and how to handle them:See Also
- LibroService - Managing books with genres
- AutorService - Managing book authors
- Genero Model - Genre data structure