71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
import { SettleOption } from '../core/settle-option';
|
|
import { VoucherType } from '../sales/bills/voucher-type';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
const url = '/api/settle-options';
|
|
const serviceName = 'SettleOptionService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class SettleOptionService {
|
|
constructor(
|
|
private http: HttpClient,
|
|
private log: ErrorLoggerService,
|
|
) {}
|
|
|
|
get(id: string | null): Observable<SettleOption> {
|
|
const getUrl: string = id === null ? url : `${url}/${id}`;
|
|
return this.http
|
|
.get<SettleOption>(getUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<SettleOption>;
|
|
}
|
|
|
|
list(): Observable<SettleOption[]> {
|
|
return this.http
|
|
.get<SettleOption[]>(`${url}/list`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<SettleOption[]>;
|
|
}
|
|
|
|
listForType(voucherType: VoucherType): Observable<SettleOption[]> {
|
|
return this.http
|
|
.get<SettleOption[]>(`${url}/for-type/${voucherType}`)
|
|
.pipe(catchError(this.log.handleError(serviceName, `listForType voucherType=${voucherType}`))) as Observable<
|
|
SettleOption[]
|
|
>;
|
|
}
|
|
|
|
save(settleOption: SettleOption): Observable<SettleOption> {
|
|
return this.http
|
|
.post<SettleOption>(url, settleOption, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<SettleOption>;
|
|
}
|
|
|
|
update(settleOption: SettleOption): Observable<SettleOption> {
|
|
return this.http
|
|
.put<SettleOption>(`${url}/${settleOption.id}`, settleOption, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<SettleOption>;
|
|
}
|
|
|
|
saveOrUpdate(settleOption: SettleOption): Observable<SettleOption> {
|
|
if (!settleOption.id) {
|
|
return this.save(settleOption);
|
|
}
|
|
return this.update(settleOption);
|
|
}
|
|
|
|
delete(id: number): Observable<SettleOption> {
|
|
return this.http
|
|
.delete<SettleOption>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<SettleOption>;
|
|
}
|
|
}
|