Consolidated commit message to v22 signals
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient, httpResource, HttpParams } from '@angular/common/http';
|
||||
import { inject, Injectable, Signal } from '@angular/core';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
|
||||
@@ -8,36 +8,32 @@ import { Product } from '../core/product';
|
||||
import { ProductSku } from '../core/product-sku';
|
||||
|
||||
const url = '/api/products';
|
||||
const serviceName = 'ProductService';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ProductService {
|
||||
private http = inject(HttpClient);
|
||||
private log = inject(ErrorLoggerService);
|
||||
|
||||
get(id: string | null): Observable<Product> {
|
||||
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
||||
return this.http
|
||||
.get<Product>(getUrl)
|
||||
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Product>;
|
||||
get(id: Signal<string | null>) {
|
||||
return httpResource<Product>(() => {
|
||||
const getUrl: string = id() === null ? `${url}` : `${url}/${id()}`;
|
||||
return getUrl;
|
||||
});
|
||||
}
|
||||
|
||||
list(): Observable<Product[]> {
|
||||
return this.http
|
||||
.get<Product[]>(`${url}/list`)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'getList'))) as Observable<Product[]>;
|
||||
list() {
|
||||
return httpResource<Product[]>(() => `${url}/list`);
|
||||
}
|
||||
|
||||
save(product: Product): Observable<Product> {
|
||||
return this.http
|
||||
.post<Product>(`${url}`, product)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Product>;
|
||||
.pipe(catchError(this.log.handleError('ProductService', 'save'))) as Observable<Product>;
|
||||
}
|
||||
|
||||
update(product: Product): Observable<Product> {
|
||||
return this.http
|
||||
.put<Product>(`${url}/${product.id}`, product)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Product>;
|
||||
.pipe(catchError(this.log.handleError('ProductService', 'update'))) as Observable<Product>;
|
||||
}
|
||||
|
||||
saveOrUpdate(product: Product): Observable<Product> {
|
||||
@@ -50,38 +46,41 @@ export class ProductService {
|
||||
delete(id: string): Observable<Product> {
|
||||
return this.http
|
||||
.delete<Product>(`${url}/${id}`)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Product>;
|
||||
.pipe(catchError(this.log.handleError('ProductService', 'delete'))) as Observable<Product>;
|
||||
}
|
||||
|
||||
autocompleteProduct(query: string, isPurchased: boolean | null): Observable<ProductSku[]> {
|
||||
const options = {
|
||||
params: new HttpParams().set('q', query),
|
||||
};
|
||||
if (isPurchased !== null) {
|
||||
options.params = options.params.set('p', isPurchased.toString());
|
||||
}
|
||||
return this.http
|
||||
.get<ProductSku[]>(`${url}/q-product`, options)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'autocomplete'))) as Observable<ProductSku[]>;
|
||||
autocompleteProduct(query: Signal<string | null>, isPurchased: Signal<boolean | null>) {
|
||||
return httpResource<ProductSku[]>(() => {
|
||||
const q = query();
|
||||
if (!q) return undefined;
|
||||
let params = new HttpParams().set('q', q || '');
|
||||
if (isPurchased() !== null) {
|
||||
params = params.set('p', isPurchased.toString());
|
||||
}
|
||||
return { url: `${url}/q-product`, params };
|
||||
});
|
||||
}
|
||||
|
||||
autocompleteSku(
|
||||
query: string,
|
||||
isPurchased: boolean | null,
|
||||
date?: string,
|
||||
vendorId?: string,
|
||||
): Observable<ProductSku[]> {
|
||||
const options = {
|
||||
params: new HttpParams().set('q', query),
|
||||
};
|
||||
if (isPurchased !== null) {
|
||||
options.params = options.params.set('p', isPurchased.toString());
|
||||
}
|
||||
if (!!vendorId && !!date) {
|
||||
options.params = options.params.set('v', vendorId as string).set('d', date as string);
|
||||
}
|
||||
return this.http
|
||||
.get<ProductSku[]>(`${url}/q-sku`, options)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'autocomplete'))) as Observable<ProductSku[]>;
|
||||
query: Signal<string | null>,
|
||||
isPurchased: Signal<boolean | null>,
|
||||
date: Signal<string | null>,
|
||||
vendorId: Signal<string | null>,
|
||||
) {
|
||||
return httpResource<ProductSku[]>(() => {
|
||||
const q = query();
|
||||
if (!q) return undefined;
|
||||
let params = new HttpParams().set('q', q || '');
|
||||
const p = isPurchased();
|
||||
if (p !== null) {
|
||||
params = params.set('p', p.toString());
|
||||
}
|
||||
const v = vendorId();
|
||||
const d = date();
|
||||
if (!!v && !!d) {
|
||||
params = params.set('v', v).set('d', d);
|
||||
}
|
||||
return { url: `${url}/q-sku`, params };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user