Skip to main content

Overview

The AutorService provides methods to create, read, update, and delete authors in the Biblioteca Virtual system. It handles all HTTP communications with the backend /autores endpoint. Location: src/app/core/services/autor.ts

Constructor

Uses Angular’s inject() function to obtain HttpClient instance.

Properties

string
default:"${environment.apiUrl}/autores"
Base URL for autor endpoints, configured from environment settings.

Methods

getAll()

Retrieves all authors from the database.
Returns: Observable<Autor[]> - Array of all authors
array
Array of author objects
number
Unique author identifier
string
Author’s full name
string
URL to the author’s photo
Example:
API Endpoint: GET ${apiUrl}

getById()

Retrieves a specific author by their ID.
number
required
The unique identifier of the author to retrieve
Returns: Observable<Autor> - Single author object Example:
API Endpoint: GET ${apiUrl}/{id} Use Case: Loading author data for editing forms

create()

Creates a new author in the database.
Autor
required
Author data to createRequired Properties:
  • nombre (string): Author’s full name
Optional Properties:
  • urlFoto (string): URL to author’s photo
  • id (number): Should be omitted for new authors (server assigns)
Returns: Observable<Autor> - The created author with server-assigned ID Example:
API Endpoint: POST ${apiUrl}

update()

Updates an existing author.
number
required
The ID of the author to update
Autor
required
Updated author dataProperties:
  • id (number): Author ID (should match path parameter)
  • nombre (string): Updated author name
  • urlFoto (string, optional): Updated photo URL
Returns: Observable<Autor> - The updated author object Example:
API Endpoint: PUT ${apiUrl}/{id}

delete()

Deletes an author from the database.
number
required
The ID of the author 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 author has associated books. Handle this error appropriately in your UI.

Complete CRUD Example

Example component using all AutorService methods:

Form Example

Example form component for creating/editing authors:

Data Model

Autor Interface

Error Handling

Common error scenarios and how to handle them:

See Also