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

36 lines
1.2 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/internal/Observable';
import {catchError} from 'rxjs/operators';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {Attendance} from './attendance';
import {ErrorLoggerService} from '../core/error-logger.service';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
const url = '/api/attendance';
const serviceName = 'AttendanceService';
@Injectable({providedIn: 'root'})
export class AttendanceService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {
}
get(date: string): Observable<Attendance> {
const getUrl: string = (date === null) ? url : `${url}/${date}`;
return <Observable<Attendance>>this.http.get<Attendance>(getUrl)
.pipe(
catchError(this.log.handleError(serviceName, `get date=${date}`))
);
}
save(attendance: Attendance): Observable<Attendance> {
return <Observable<Attendance>>this.http.post<Attendance>(`${url}/${attendance.date}`, attendance, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'save'))
);
}
}