oggui/ogWebconsole/src/app/components/calendar/data.service.ts

56 lines
1.8 KiB
TypeScript

import { inject } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ConfigService } from '@services/config.service';
@Injectable({
providedIn: 'root'
})
export class DataService {
public translate = inject(TranslateService);
baseUrl: string;
private apiUrl: string;
constructor(private http: HttpClient, private configService: ConfigService) {
this.baseUrl = this.configService.apiUrl;
this.apiUrl = `${this.baseUrl}/remote-calendars?page=1&itemsPerPage=1000`;
}
getRemoteCalendars(filters: { [key: string]: string }): Observable<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 response['hydra:member'];
} else {
throw new Error('Unexpected response format');
}
}),
catchError(error => {
console.error(this.translate.instant('errorFetchingRemoteCalendars'), error);
return throwError(error);
})
);
}
getRemoteCalendar(id: string): Observable<any> {
return this.http.get<any>(`${this.baseUrl}${id}`).pipe(
map(response => {
if (response.name && response.remoteCalendarRules) {
return response;
} else {
throw new Error('Unexpected response format');
}
}),
catchError(error => {
console.error(this.translate.instant('fetchingCalendarError'), error);
return throwError(error);
})
);
}
}