55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
|
|
import { Injectable } from '@angular/core';
|
|
import {HttpClient, HttpParams} from '@angular/common/http';
|
|
import { Observable, throwError } from 'rxjs';
|
|
import { catchError, map } from 'rxjs/operators';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class DataService {
|
|
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
|
|
private apiUrl = `${this.baseUrl}/commands?page=1&itemsPerPage=1000`;
|
|
|
|
constructor(private http: HttpClient) {}
|
|
|
|
getCommands(filters: { [key: string]: string }): Observable<{ totalItems: any; data: any }> {
|
|
const params = new HttpParams({ fromObject: filters });
|
|
|
|
return this.http.get<any>(this.apiUrl, { params }).pipe(
|
|
map(response => {
|
|
if (response['hydra:member'] && Array.isArray(response['hydra:member'])) {
|
|
return {
|
|
data: response['hydra:member'],
|
|
totalItems: response['hydra:totalItems']
|
|
}
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching commands', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
getCommand(id: string): Observable<any> {
|
|
return this.http.get<any>(`${this.baseUrl}${id}`).pipe(
|
|
map(response => {
|
|
if (response.name) {
|
|
return response;
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching user group', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
|
|
}
|