44 lines
1.5 KiB
TypeScript
44 lines
1.5 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 { SectionPrinter } from '../core/section-printer';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
const url = '/api/section-printers';
|
|
const serviceName = 'SectionPrinterService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class SectionPrinterService {
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
get(id: string | null): Observable<SectionPrinter[]> {
|
|
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
|
return this.http
|
|
.get<SectionPrinter[]>(getUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<
|
|
SectionPrinter[]
|
|
>;
|
|
}
|
|
|
|
save(sectionId: string, list: SectionPrinter[]): Observable<SectionPrinter[]> {
|
|
return this.http
|
|
.post<SectionPrinter[]>(`${url}/${sectionId}`, list, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<SectionPrinter[]>;
|
|
}
|
|
|
|
delete(id: string): Observable<SectionPrinter[]> {
|
|
return this.http
|
|
.delete<SectionPrinter[]>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<
|
|
SectionPrinter[]
|
|
>;
|
|
}
|
|
}
|