brewman/overlord/src/app/period/period.service.ts

58 lines
1.7 KiB
TypeScript

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { catchError } from 'rxjs/operators';
import { ErrorLoggerService } from '../core/error-logger.service';
import { Period } from './period';
const url = '/api/periods';
const serviceName = 'PeriodService';
@Injectable({ providedIn: 'root' })
export class PeriodService {
constructor(
private http: HttpClient,
private log: ErrorLoggerService,
) {}
get(id: string | null): Observable<Period> {
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
return this.http
.get<Period>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Period>;
}
list(): Observable<Period[]> {
return this.http
.get<Period[]>(`${url}/list`)
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<Period[]>;
}
save(period: Period): Observable<Period> {
return this.http
.post<Period>(`${url}`, period)
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Period>;
}
update(period: Period): Observable<Period> {
return this.http
.put<Period>(`${url}/${period.id}`, period)
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Period>;
}
saveOrUpdate(period: Period): Observable<Period> {
if (!period.id) {
return this.save(period);
}
return this.update(period);
}
delete(id: string): Observable<Period> {
return this.http
.delete<Period>(`${url}/${id}`)
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Period>;
}
}