Files
barker/bookie/src/app/sections/section.service.ts
tanshu d677cfb1ea Blacked and isorted the python files
Prettied and eslinted the typescript/html files
2020-10-11 10:56:29 +05:30

69 lines
1.9 KiB
TypeScript

import { HttpClient, HttpHeaders } 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 { Section } from '../core/section';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
};
const url = '/api/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 : `${url}/${id}`;
return <Observable<Section>>(
this.http
.get<Section>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`)))
);
}
list(): Observable<Section[]> {
return <Observable<Section[]>>(
this.http
.get<Section[]>(`${url}/list`)
.pipe(catchError(this.log.handleError(serviceName, 'list')))
);
}
save(section: Section): Observable<Section> {
return <Observable<Section>>(
this.http
.post<Section>(url, 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);
}
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')))
);
}
}