import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, catchError, throwError } from 'rxjs'; import { IService } from 'src/app/_interfaces/servives/service'; import { environment } from 'src/environments/environment'; @Injectable({ providedIn: 'root', }) export class ServiceService { private SUrl = `${environment.apiBaseUrl}/services/`; httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }), }; constructor(private http: HttpClient) {} // Get all services getServices(): Observable { return this.http.get(this.SUrl); } // Get a single service by id getServiceById(id: number): Observable { const url = `${this.SUrl}/${id}`; return this.http.get(url); } // Create a new service createService(service: IService): Observable { const serviceData = JSON.stringify(service); console.log('service:==>', serviceData); console.log('Données envoyées pour créer le service...'); return this.http .post(this.SUrl, serviceData, this.httpOptions) .pipe(catchError(this.errorHandler)); } updateService(service: IService): Observable { const url = `${this.SUrl}/${service.nom_service}`; return this.http.put(url, service, { headers: new HttpHeaders({ 'Content-Type': 'application/json', }), }); } deleteService(id: number): Observable { const url = `${this.SUrl}/${id}`; return this.http.delete(url); } // eslint-disable-next-line @typescript-eslint/no-explicit-any private errorHandler(error: any): Observable { //console.error('Erreur lors de la création du service:', error); let errorMessage = ''; if (error.error instanceof ErrorEvent) { errorMessage = `Erreur côté client: ${error.error.message}`; } else { errorMessage = `Code d'erreur: ${error.status}\nMessage: ${error.message}`; } return throwError(errorMessage); } }