58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
import { HttpClient, HttpHeaders, httpResource, HttpResourceRef } from '@angular/common/http';
|
|
import { Injectable, Signal, inject } from '@angular/core';
|
|
import { Observable } from 'rxjs';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
import { Tax } from '../core/tax';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
const url = '/api/taxes';
|
|
const serviceName = 'TaxService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class TaxService {
|
|
private http = inject(HttpClient);
|
|
private log = inject(ErrorLoggerService);
|
|
|
|
get(id: Signal<string | null>): HttpResourceRef<Tax | undefined> {
|
|
return httpResource<Tax>(() => {
|
|
const idVal = id();
|
|
return idVal === null || idVal === undefined ? url : `${url}/${idVal}`;
|
|
});
|
|
}
|
|
|
|
list(): HttpResourceRef<Tax[] | undefined> {
|
|
return httpResource<Tax[]>(() => `${url}/list`);
|
|
}
|
|
|
|
save(tax: Tax): Observable<Tax> {
|
|
return this.http
|
|
.post<Tax>(url, tax, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Tax>;
|
|
}
|
|
|
|
update(tax: Tax): Observable<Tax> {
|
|
return this.http
|
|
.put<Tax>(`${url}/${tax.id}`, tax, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Tax>;
|
|
}
|
|
|
|
saveOrUpdate(tax: Tax): Observable<Tax> {
|
|
if (!tax.id) {
|
|
return this.save(tax);
|
|
}
|
|
return this.update(tax);
|
|
}
|
|
|
|
delete(id: string): Observable<Tax> {
|
|
return this.http
|
|
.delete<Tax>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Tax>;
|
|
}
|
|
}
|