96 lines
2.9 KiB
TypeScript
96 lines
2.9 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 { Customer } from '../core/customer';
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
import { PagedResult } from '../core/paged-result';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
const url = '/api/customers';
|
|
const serviceName = 'CustomerService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class CustomerService {
|
|
private http = inject(HttpClient);
|
|
private log = inject(ErrorLoggerService);
|
|
|
|
get(id: Signal<string | null> | string | null): HttpResourceRef<Customer | undefined> {
|
|
return httpResource<Customer>(() => {
|
|
const idVal = typeof id === 'function' ? id() : id;
|
|
return idVal === null || idVal === undefined ? url : `${url}/${idVal}`;
|
|
});
|
|
}
|
|
|
|
list(q: string | null, page = 0, size = 50, field = 'name', direction = 'asc'): Observable<PagedResult<Customer>> {
|
|
const params = {
|
|
...(q ? { q } : {}),
|
|
p: page,
|
|
s: size,
|
|
f: field,
|
|
d: direction,
|
|
};
|
|
return this.http
|
|
.get<PagedResult<Customer>>(`${url}/list`, { params })
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<PagedResult<Customer>>;
|
|
}
|
|
|
|
query(
|
|
q: Signal<string>,
|
|
p: Signal<number>,
|
|
s: Signal<number>,
|
|
f: Signal<string>,
|
|
d: Signal<string>,
|
|
): HttpResourceRef<PagedResult<Customer> | undefined> {
|
|
return httpResource<PagedResult<Customer>>(() => {
|
|
return {
|
|
url: `${url}/list`,
|
|
params: {
|
|
q: q(),
|
|
p: p(),
|
|
s: s(),
|
|
f: f(),
|
|
d: d(),
|
|
},
|
|
};
|
|
});
|
|
}
|
|
|
|
save(customer: Customer): Observable<Customer> {
|
|
return this.http
|
|
.post<Customer>(`${url}`, customer, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Customer>;
|
|
}
|
|
|
|
update(customer: Customer): Observable<Customer> {
|
|
return this.http
|
|
.put<Customer>(`${url}/${customer.id}`, customer, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Customer>;
|
|
}
|
|
|
|
saveOrUpdate(customer: Customer): Observable<Customer> {
|
|
if (!customer.id) {
|
|
return this.save(customer);
|
|
}
|
|
return this.update(customer);
|
|
}
|
|
|
|
delete(id: string): Observable<Customer> {
|
|
return this.http
|
|
.delete<Customer>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Customer>;
|
|
}
|
|
|
|
autocomplete(query: Signal<string | null> | string | null): HttpResourceRef<Customer[] | undefined> {
|
|
return httpResource<Customer[]>(() => {
|
|
const qVal = typeof query === 'function' ? query() : query;
|
|
return `${url}/query?q=${qVal ?? ''}`;
|
|
});
|
|
}
|
|
}
|