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'; import { environment } from 'src/environments/environment'; @Injectable({ providedIn: 'root', }) export class TransactionService { private HtUrl = `${environment.apiBaseUrl}/api/tableau_bord/1`; private TUrl = `${environment.apiBaseUrl}/transactions`; httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }), }; constructor(private httpClient: HttpClient) {} // Get all transactions getAll(): Observable { return this.httpClient .get(this.TUrl) .pipe(catchError(this.errorHandler)); } // Get merchant data for a table getDataTable(): Observable { return this.httpClient .get(this.HtUrl) .pipe(catchError(this.errorHandler)); } // Create a new transaction create(transaction: ITransaction): Observable { return this.httpClient .post(this.TUrl, transaction, this.httpOptions) .pipe(catchError(this.errorHandler)); } // Find a transaction by id find(transaction_id: number): Observable { const url = `${this.TUrl}/${transaction_id}`; return this.httpClient .get(url) .pipe(catchError(this.errorHandler)); } // Update an existing transaction update( transaction_id: number, transaction: ITransaction ): Observable { const url = `${this.TUrl}/${transaction_id}`; return this.httpClient .put(url, transaction, this.httpOptions) .pipe(catchError(this.errorHandler)); } // Delete a transaction delete(transaction_id: number): Observable { const url = `${this.TUrl}/${transaction_id}`; return this.httpClient .delete(url, this.httpOptions) .pipe(catchError(this.errorHandler)); } // Error handler private 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(() => new Error(errorMessage)); } }