import { HttpClient, HttpParams } 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 { Voucher } from '../core/voucher'; import { ClosingStock } from './closing-stock'; const url = '/api/closing-stock'; const serviceName = 'ClosingStockService'; @Injectable({ providedIn: 'root', }) export class ClosingStockService { constructor(private http: HttpClient, private log: ErrorLoggerService) {} list(date: string | null, costCentre: string | null): Observable { const listUrl = date === null ? url : `${url}/${date}`; const options = { params: new HttpParams() }; if (costCentre !== null) { options.params = options.params.set('d', costCentre); } return this.http .get(listUrl, options) .pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable; } save(closingStock: ClosingStock): Observable { return this.http .post(url, closingStock) .pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable; } post(date: string, costCentre: string): Observable { const options = { params: new HttpParams().set('d', costCentre) }; return this.http .post(`${url}/${date}`, {}, options) .pipe( catchError(this.log.handleError(serviceName, 'Post Voucher')), ) as Observable; } delete(date: string, costCentre: string): Observable { const options = { params: new HttpParams().set('d', costCentre) }; return this.http .delete(`${url}/${date}`, options) .pipe( catchError(this.log.handleError(serviceName, 'Delete Voucher')), ) as Observable; } }