Skip to main content

Overview

The Autor interface represents an author in the virtual library system. Authors can be associated with one or more books.

Interface Definition

export interface Autor {
  id: number;
  nombre: string;
  urlFoto?: string;
}

Properties

id
number
required
Unique identifier for the author. Auto-generated by the backend.
nombre
string
required
The author’s full name.
urlFoto
string
Optional URL or path to the author’s photo/portrait.

Usage Examples

Fetching All Authors

import { AutorService } from './core/services/autor';
import { Autor } from './core/models/autor';

this.autorService.getAll().subscribe({
  next: (autores: Autor[]) => {
    autores.forEach(autor => {
      console.log(`${autor.nombre} (ID: ${autor.id})`);
      if (autor.urlFoto) {
        console.log(`Photo: ${autor.urlFoto}`);
      }
    });
  },
  error: (err) => console.error('Error fetching authors:', err)
});

Creating a New Author

const nuevoAutor: Autor = {
  id: 0, // Will be assigned by backend
  nombre: 'Gabriel García Márquez',
  urlFoto: 'https://ejemplo.com/fotos/gabo.jpg'
};

this.autorService.create(nuevoAutor).subscribe({
  next: (autor: Autor) => {
    console.log('Author created with ID:', autor.id);
  },
  error: (err) => console.error('Error creating author:', err)
});

Updating an Author

this.autorService.getById(1).subscribe({
  next: (autor: Autor) => {
    const autorActualizado: Autor = {
      ...autor,
      nombre: 'Gabriel José de la Concordia García Márquez',
      urlFoto: 'https://ejemplo.com/fotos/gabo-updated.jpg'
    };
    
    this.autorService.update(autor.id, autorActualizado).subscribe({
      next: (updated) => console.log('Author updated:', updated),
      error: (err) => console.error('Update failed:', err)
    });
  }
});

Deleting an Author

this.autorService.delete(autorId).subscribe({
  next: (response) => {
    console.log('Author deleted:', response);
  },
  error: (err) => {
    console.error('Delete failed:', err);
    // Note: May fail if author is associated with existing books
  }
});

Using Authors in Book Creation

import { Libro } from './core/models/libro';

// Fetch authors to associate with a book
this.autorService.getAll().subscribe({
  next: (autores: Autor[]) => {
    const selectedAuthors = autores.filter(a => 
      a.nombre.includes('García Márquez')
    );
    
    const nuevoLibro = {
      titulo: 'El amor en los tiempos del cólera',
      autores: selectedAuthors,
      // ... other properties
    };
    
    this.libroService.create(nuevoLibro).subscribe(/* ... */);
  }
});

Displaying Author Information in Components

// In your component
autores: Autor[] = [];

ngOnInit() {
  this.loadAutores();
}

loadAutores() {
  this.autorService.getAll().subscribe({
    next: (data) => {
      this.autores = data;
    },
    error: (err) => {
      console.error('Error loading authors:', err);
    }
  });
}

getAutorById(id: number): Autor | undefined {
  return this.autores.find(autor => autor.id === id);
}

Template Usage

<!-- Display author list -->
<div *ngFor="let autor of autores" class="author-card">
  <img *ngIf="autor.urlFoto" [src]="autor.urlFoto" [alt]="autor.nombre">
  <h3>{{ autor.nombre }}</h3>
</div>

<!-- Display book authors -->
<div *ngIf="libro">
  <h2>{{ libro.titulo }}</h2>
  <p>Por: 
    <span *ngFor="let autor of libro.autores; let last = last">
      {{ autor.nombre }}<span *ngIf="!last">, </span>
    </span>
  </p>
</div>
  • Libro - Book interface that references Autor in the autores array

Notes

Deleting an author that is associated with existing books may fail due to foreign key constraints. Consider implementing a check or cascade delete strategy.
The urlFoto field is optional but recommended for better user experience in the UI.

Build docs developers (and LLMs) love