Skip to main content

Overview

The Libro interface represents a book in the virtual library system. It includes book metadata and relationships to authors and genres.

Interface Definition

import { Autor } from './autor';
import { Genero } from './genero';

export interface Libro {
  id: number;
  titulo: string;
  portada?: string;
  anioPublicacion?: number;
  disponible: boolean;

  // Relaciones
  genero: Genero;
  autores: Autor[];
}

Properties

Basic Properties

id
number
required
Unique identifier for the book. Auto-generated by the backend.
titulo
string
required
The book’s title.
portada
string
Optional URL or path to the book’s cover image.
anioPublicacion
number
Optional publication year of the book.
disponible
boolean
required
Indicates whether the book is currently available for loan. true if available, false if currently loaned out.

Relationships

genero
Genero
required
The genre/category to which this book belongs. See Genero interface.
autores
Autor[]
required
Array of authors who wrote this book. A book can have multiple authors. See Autor interface.

Usage Examples

Fetching All Books

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

this.libroService.getAll().subscribe({
  next: (libros: Libro[]) => {
    libros.forEach(libro => {
      console.log(`${libro.titulo} by ${libro.autores.map(a => a.nombre).join(', ')}`);
      console.log(`Genre: ${libro.genero.nombre}`);
      console.log(`Available: ${libro.disponible ? 'Yes' : 'No'}`);
    });
  },
  error: (err) => console.error('Error fetching books:', err)
});

Creating a New Book

const nuevoLibro = {
  titulo: 'Cien años de soledad',
  portada: 'https://ejemplo.com/portada.jpg',
  anioPublicacion: 1967,
  disponible: true,
  genero: {
    id: 1,
    nombre: 'Realismo Mágico'
  },
  autores: [
    {
      id: 1,
      nombre: 'Gabriel García Márquez',
      urlFoto: 'https://ejemplo.com/gabo.jpg'
    }
  ]
};

this.libroService.create(nuevoLibro).subscribe({
  next: (libro: Libro) => {
    console.log('Book created:', libro.id);
  },
  error: (err) => console.error('Error creating book:', err)
});

Updating a Book

this.libroService.getById(1).subscribe({
  next: (libro: Libro) => {
    // Update availability
    const libroActualizado = {
      ...libro,
      disponible: false
    };
    
    this.libroService.update(libro.id, libroActualizado).subscribe({
      next: (updated) => console.log('Book updated'),
      error: (err) => console.error('Update failed:', err)
    });
  }
});

Deleting a Book

this.libroService.delete(libroId).subscribe({
  next: () => console.log('Book deleted successfully'),
  error: (err) => console.error('Delete failed:', err)
});

Filtering Available Books

this.libroService.getAll().subscribe({
  next: (libros: Libro[]) => {
    const librosDisponibles = libros.filter(libro => libro.disponible);
    console.log(`${librosDisponibles.length} books available for loan`);
  }
});
  • Autor - Book author interface
  • Genero - Book genre interface
  • Prestamo - Loan interface that references Libro

Notes

The disponible field is automatically updated by the backend when a loan is approved or a book is returned.
When creating or updating a book, ensure the genero and autores objects contain valid IDs that exist in the database.

Build docs developers (and LLMs) love