Skip to main content

Overview

The GeneroListComponent provides administrators with a view of all literary genres in the system. It allows creating new genres, editing existing ones, and deleting genres that are not associated with any books.

Component Metadata

Selector: app-genero-list Imports:
  • CommonModule - Angular common directives
  • RouterLink - Router navigation for creating and editing
Template: ./genero-list.html

Properties

generos

Array containing all literary genres in the system. Each genre object includes:
  • id: Genre identifier
  • nombre: Genre name (e.g., “Ficción”, “Ciencia Ficción”, “Romance”)

Injected Services

  • GeneroService - Handles all genre-related API operations

Lifecycle Hooks

ngOnInit

Called when the component initializes. Immediately loads all genres from the backend.

Methods

cargarGeneros

Fetches all genres from the GeneroService and populates the generos array:
  • On success: Updates generos array and logs the data to console
  • On error: Logs error message to console with prefix “Error al cargar géneros:”
This method is called on initialization and after deleting a genre to refresh the list.

eliminarGenero

Deletes a genre from the system:
  1. Displays confirmation dialog “¿Estás seguro de eliminar este género?”
  2. If confirmed, calls GeneroService.delete() with the genre ID
  3. On success:
    • Reloads the genre list by calling cargarGeneros()
    • Shows success alert “Género eliminado correctamente”
  4. On error:
    • Logs error to console
    • Shows error alert “No se pudo eliminar el género. Es probable que existan libros asociados a él.”
The error handling specifically mentions associated books because the backend prevents deleting genres that are referenced by existing books. Parameters:
  • id - The numeric ID of the genre to delete

Full Source Code

  • Genero - Genre data structure