import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, catchError, throwError } from 'rxjs'; import { ITransaction, IMerchantData, } from 'src/app/_interfaces/trafics/transaction/transaction'; @Injectable({ providedIn: 'root', }) export class TransactionService { //private HtUrl = 'http://192.168.1.223:8001/transactions/'; private HtUrl = 'http://192.168.1.223:8001/api/tableau_bord/1/'; private TUrl = 'http://192.168.1.223:8001/transactions/'; httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }), }; constructor(private httpClient: HttpClient) {} getAll(): Observable { return this.httpClient .get(this.TUrl) .pipe(catchError(this.errorHandler)); } getDataTable(): Observable { return this.httpClient.get(this.HtUrl); } create(transaction: ITransaction): Observable { return this.httpClient .post( this.HtUrl, JSON.stringify(transaction), this.httpOptions ) .pipe(catchError(this.errorHandler)); } find(transaction_id: number): Observable { return this.httpClient .get(`${this.HtUrl}/${transaction_id}`) .pipe(catchError(this.errorHandler)); } update( transaction_id: number, transaction: ITransaction ): Observable { const url = `${this.HtUrl}/${transaction_id}`; return this.httpClient .put(url, transaction, this.httpOptions) .pipe(catchError(this.errorHandler)); } // update(transaction_id: number, transaction: ITransaction): Observable { // return this.httpClient; // transaction.pipe(catchError(this.errorHandler)); // } delete(transaction_id: number) { return this.httpClient .delete(`${this.HtUrl}/${transaction_id}`, this.httpOptions) .pipe(catchError(this.errorHandler)); } // eslint-disable-next-line @typescript-eslint/no-explicit-any errorHandler(error: any) { let errorMessage = ''; if (error.error instanceof ErrorEvent) { errorMessage = error.error.message; } else { errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`; } return throwError(errorMessage); } }