barker/bookie/src/app/sections/section.service.ts

66 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-06-22 20:05:00 +00:00
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {ErrorLoggerService} from '../core/error-logger.service';
import {catchError} from 'rxjs/operators';
import {Observable} from 'rxjs/internal/Observable';
import {Section} from '../core/section';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
const url = '/v1/sections';
const serviceName = 'SectionService';
@Injectable({
providedIn: 'root'
})
export class SectionService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {
}
get(id: string): Observable<Section> {
const getUrl: string = (id === null) ? `${url}/new` : `${url}/${id}`;
return <Observable<Section>>this.http.get<Section>(getUrl)
.pipe(
catchError(this.log.handleError(serviceName, `get id=${id}`))
);
}
list(): Observable<Section[]> {
const options = {params: new HttpParams().set('l', '')};
return <Observable<Section[]>>this.http.get<Section[]>(url, options)
.pipe(
catchError(this.log.handleError(serviceName, 'list'))
);
}
save(section: Section): Observable<Section> {
return <Observable<Section>>this.http.post<Section>(`${url}/new`, section, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'save'))
);
}
update(section: Section): Observable<Section> {
return <Observable<Section>>this.http.put<Section>(`${url}/${section.id}`, section, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'update'))
);
}
saveOrUpdate(section: Section): Observable<Section> {
if (!section.id) {
return this.save(section);
} else {
return this.update(section);
}
}
delete(id: string): Observable<Section> {
return <Observable<Section>>this.http.delete<Section>(`${url}/${id}`, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'delete'))
);
}
}