68 lines
2.0 KiB
TypeScript
68 lines
2.0 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 { Modifier } from '../core/modifier';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
|
|
const url = '/api/modifiers';
|
|
const serviceName = 'ModifierService';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class ModifierService {
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
get(id: string | null): Observable<Modifier> {
|
|
const getUrl: string = id === null ? url : `${url}/${id}`;
|
|
return <Observable<Modifier>>(
|
|
this.http
|
|
.get<Modifier>(getUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`)))
|
|
);
|
|
}
|
|
|
|
list(): Observable<Modifier[]> {
|
|
return <Observable<Modifier[]>>(
|
|
this.http
|
|
.get<Modifier[]>(`${url}/list`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'getList')))
|
|
);
|
|
}
|
|
|
|
save(modifier: Modifier): Observable<Modifier> {
|
|
return <Observable<Modifier>>(
|
|
this.http
|
|
.post<Modifier>(url, modifier, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save')))
|
|
);
|
|
}
|
|
|
|
update(modifier: Modifier): Observable<Modifier> {
|
|
return <Observable<Modifier>>(
|
|
this.http
|
|
.put<Modifier>(`${url}/${modifier.id}`, modifier, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update')))
|
|
);
|
|
}
|
|
|
|
saveOrUpdate(modifier: Modifier): Observable<Modifier> {
|
|
if (!modifier.id) {
|
|
return this.save(modifier);
|
|
}
|
|
return this.update(modifier);
|
|
}
|
|
|
|
delete(id: string): Observable<Modifier> {
|
|
return <Observable<Modifier>>(
|
|
this.http
|
|
.delete<Modifier>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete')))
|
|
);
|
|
}
|
|
}
|