179 lines
5.4 KiB
TypeScript
179 lines
5.4 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';
|
|
import { UnidadOrganizativa } from './model';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class DataService {
|
|
|
|
private apiUrl = 'http://127.0.0.1:8001/organizational-units?page=1&itemsPerPage=1000';
|
|
private clientsUrl = 'http://127.0.0.1:8001/clients?page=1&itemsPerPage=1000';
|
|
|
|
constructor(private http: HttpClient) {}
|
|
|
|
getOrganizationalUnits(search: string = ''): Observable<UnidadOrganizativa[]> {
|
|
let url = `${this.apiUrl}&type=organizational-unit`;
|
|
if (search) {
|
|
url += `&name=${encodeURIComponent(search)}`;
|
|
}
|
|
|
|
return this.http.get<any>(url).pipe(
|
|
map(response => {
|
|
if (response['hydra:member'] && Array.isArray(response['hydra:member'])) {
|
|
return response['hydra:member'];
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching unidades organizativas', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
getChildren(id: string): Observable<any[]> {
|
|
return this.http.get<any>(`${this.apiUrl}&parent.id=${id}`).pipe(
|
|
map(response => {
|
|
if (response['hydra:member'] && Array.isArray(response['hydra:member'])) {
|
|
return response['hydra:member']
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching children', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
getClients(id: string): Observable<any[]> {
|
|
return this.http.get<any>(`${this.clientsUrl}&organizationalUnit.id=${id}`).pipe(
|
|
map(response => {
|
|
if (response['hydra:member'] && Array.isArray(response['hydra:member'])) {
|
|
return response['hydra:member']
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching clients', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
getHardwareProfiles(): Observable<any[]> {
|
|
const url = 'http://127.0.0.1:8001/hardware-profiles';
|
|
return this.http.get<any>(url).pipe(
|
|
map(response => {
|
|
if (response['hydra:member'] && Array.isArray(response['hydra:member'])) {
|
|
return response['hydra:member']
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching clients', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
deleteElement(uuid: string, type: string): Observable<void> {
|
|
const url = type === 'client'
|
|
? `http://127.0.0.1:8001/clients/${uuid}`
|
|
: `http://127.0.0.1:8001/organizational-units/${uuid}`;
|
|
return this.http.delete<void>(url).pipe(
|
|
catchError(error => {
|
|
console.error('Error deleting element', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
changeParent(uuid: string): Observable<void> {
|
|
const url = `http://127.0.0.1:8001/organizational-units/${uuid}/change-parent`;
|
|
// @ts-ignore
|
|
return this.http.post<void>(url).pipe(
|
|
catchError(error => {
|
|
console.error('Error deleting element', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
getFilteredResults(filter1: string, filter2: string, filterName: string, page: number, pageSize: number): Observable<any> {
|
|
let params = new HttpParams();
|
|
|
|
if (filter2 && filter2 !== 'none') {
|
|
params = params.set('type', filter2);
|
|
}
|
|
if (filterName) {
|
|
params = params.set('name', filterName);
|
|
}
|
|
|
|
params = params.set('page', page.toString());
|
|
params = params.set('itemsPerPage', pageSize.toString());
|
|
|
|
const url = filter1 === 'client' ? this.clientsUrl : this.apiUrl;
|
|
|
|
return this.http.get<any>(url, { params }).pipe(
|
|
map(response => {
|
|
if (response['hydra:member'] && Array.isArray(response['hydra:member'])) {
|
|
return {
|
|
results: response['hydra:member'],
|
|
total: response['hydra:totalItems'] || response['hydra:member'].length
|
|
};
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching data', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
getFilters(): Observable<any> {
|
|
return this.http.get<any>('http://127.0.0.1:8001/views?page=1&itemsPerPage=30').pipe(
|
|
map(response => {
|
|
if (response['hydra:member'] && Array.isArray(response['hydra:member'])) {
|
|
return response['hydra:member'];
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching filters', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
getFilter(id: string): Observable<any> {
|
|
return this.http.get<any>('http://127.0.0.1:8001/views/' + id).pipe(
|
|
map(response => {
|
|
|
|
|
|
if (response.name && response.filters) {
|
|
return response;
|
|
} else {
|
|
throw new Error('Unexpected response format');
|
|
}
|
|
}),
|
|
catchError(error => {
|
|
console.error('Error fetching filters', error);
|
|
return throwError(error);
|
|
})
|
|
);
|
|
}
|
|
|
|
|
|
}
|