Overview
The TreatmentService is an Angular injectable service that manages the catalog of available dental treatments including their descriptions, pricing, duration, and categories.
Interface
Tratamiento
Represents a treatment offering in the dental clinic.
export interface Tratamiento {
id : number ;
nombre : string ;
categoria : string ;
descripcion : string ;
duracion : number ;
precio : number ;
}
Unique identifier for the treatment
Category of the treatment (e.g., ‘Preventiva’, ‘Cirugía’, ‘Estética’, ‘Ortodoncia’)
Detailed description of the treatment
Duration of the treatment in minutes
Price of the treatment in the local currency
Methods
getTratamientos()
Retrieves all available treatments.
getTratamientos (): Tratamiento []
Array containing all treatment records
Basic Usage
Filter by Category
Sort by Price
Get Treatment Statistics
import { TreatmentService } from './services/treatment.service' ;
export class TreatmentListComponent {
constructor ( private treatmentService : TreatmentService ) {}
loadTreatments () {
const treatments = this . treatmentService . getTratamientos ();
console . log ( `Available treatments: ${ treatments . length } ` );
}
}
addTratamiento()
Adds a new treatment to the catalog.
addTratamiento ( tratamiento : Omit < Tratamiento , 'id' > ): Tratamiento
Treatment data object with the following properties:
nombre (string): Name of the treatment
categoria (string): Treatment category
descripcion (string): Treatment description
duracion (number): Duration in minutes
precio (number): Price of the treatment
The newly created treatment object with assigned ID
Basic Usage
From Form Data
With Validation
const newTreatment = {
nombre: 'Endodoncia' ,
categoria: 'Cirugía' ,
descripcion: 'Tratamiento del conducto radicular' ,
duracion: 90 ,
precio: 300
};
const created = this . treatmentService . addTratamiento ( newTreatment );
console . log ( `Created treatment with ID: ${ created . id } ` );
updateTratamiento()
Updates an existing treatment in the catalog.
updateTratamiento ( tratamiento : Tratamiento ): void
Complete treatment object including the ID to update
Basic Usage
Update Price Only
From Edit Form
const updatedTreatment : Tratamiento = {
id: 1 ,
nombre: 'Limpieza dental profunda' ,
categoria: 'Preventiva' ,
descripcion: 'Limpieza profesional completa con pulido' ,
duracion: 60 ,
precio: 90
};
this . treatmentService . updateTratamiento ( updatedTreatment );
deleteTratamiento()
Removes a treatment from the catalog.
deleteTratamiento ( id : number ): void
The unique identifier of the treatment to delete
Basic Usage
With Confirmation
Bulk Delete
this . treatmentService . deleteTratamiento ( 5 );
Usage Example
import { Component , OnInit } from '@angular/core' ;
import { TreatmentService , Tratamiento } from './services/treatment.service' ;
@ Component ({
selector: 'app-treatment-catalog' ,
templateUrl: './treatment-catalog.component.html'
})
export class TreatmentCatalogComponent implements OnInit {
treatments : Tratamiento [] = [];
categories : string [] = [];
selectedCategory : string = 'all' ;
constructor ( private treatmentService : TreatmentService ) {}
ngOnInit () {
this . loadTreatments ();
}
loadTreatments () {
this . treatments = this . treatmentService . getTratamientos ();
this . categories = [ ... new Set ( this . treatments . map ( t => t . categoria ))];
}
getFilteredTreatments () : Tratamiento [] {
if ( this . selectedCategory === 'all' ) {
return this . treatments ;
}
return this . treatments . filter ( t => t . categoria === this . selectedCategory );
}
addTreatment ( treatmentData : Omit < Tratamiento , 'id' >) {
const newTreatment = this . treatmentService . addTratamiento ( treatmentData );
this . loadTreatments ();
return newTreatment ;
}
updateTreatmentPrice ( id : number , newPrice : number ) {
const treatment = this . treatments . find ( t => t . id === id );
if ( treatment ) {
treatment . precio = newPrice ;
this . treatmentService . updateTratamiento ( treatment );
}
}
removeTreatment ( id : number ) {
this . treatmentService . deleteTratamiento ( id );
this . loadTreatments ();
}
}