Skip to main content

Overview

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

Component Metadata

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

Properties

autores

Array containing all authors in the system. Each author object includes:
  • id: Author identifier
  • nombre: Author name
  • urlFoto: Optional photo URL

Injected Services

  • AutorService - Handles all author-related API operations

Lifecycle Hooks

ngOnInit

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

Methods

cargarAutores

Fetches all authors from the AutorService and populates the autores array:
  • On success: Updates autores array and logs the data to console
  • On error: Logs error message to console with prefix “Error al cargar autores:”
This method is called on initialization and after deleting an author to refresh the list.

eliminarAutor

Deletes an author from the system:
  1. Displays confirmation dialog “¿Estás seguro de eliminar este autor?”
  2. If confirmed, calls AutorService.delete() with the author ID
  3. On success:
    • Reloads the author list by calling cargarAutores()
  4. On error:
    • Logs error to console
    • Shows error alert “No se pudo eliminar el autor (quizás tiene libros asociados).”
The error handling specifically mentions associated books because the backend prevents deleting authors that are referenced by existing books. Parameters:
  • id - The numeric ID of the author to delete

Full Source Code

  • Autor - Author data structure