Skip to main content

Overview

The GeneroService 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

Uses Angular’s 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.
Returns: Observable<Genero[]> - Array of all genres
array
Array of genre objects
number
Unique genre identifier
string
Genre name (e.g., “Ciencia Ficción”, “Romance”, “Terror”)
Example:
API Endpoint: GET ${apiUrl}

getById()

Retrieves a specific genre by its ID.
number
required
The unique identifier of the genre to retrieve
Returns: Observable<Genero> - Single genre object Example:
API Endpoint: 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
Optional Properties:
  • id (number): Should be omitted for new genres (server assigns)
Returns: Observable<Genero> - The created genre with server-assigned ID Example:
API Endpoint: 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
Returns: Observable<Genero> - The updated genre object Example:
API Endpoint: PUT ${apiUrl}/{id}

delete()

Deletes a genre from the database.
number
required
The ID of the genre to delete
Returns: Observable<any> - Plain text response from server Response Type: Text (uses responseType: 'text' to accept string responses from backend) Example:
API Endpoint: 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