91 lines
2.9 KiB
TypeScript
91 lines
2.9 KiB
TypeScript
import { HttpClient, HttpHeaders, 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 { Product } from '../core/product';
|
|
import { ProductSku } from '../core/product-sku';
|
|
|
|
const url = '/api/products';
|
|
const serviceName = 'ProductService';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class ProductService {
|
|
constructor(private http: HttpClient, private log: 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>;
|
|
}
|
|
|
|
list(): Observable<Product[]> {
|
|
return this.http
|
|
.get<Product[]>(`${url}/list`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'getList'))) as Observable<Product[]>;
|
|
}
|
|
|
|
save(product: Product): Observable<Product> {
|
|
return this.http
|
|
.post<Product>(`${url}`, product)
|
|
.pipe(catchError(this.log.handleError(serviceName, '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>;
|
|
}
|
|
|
|
saveOrUpdate(product: Product): Observable<Product> {
|
|
if (!product.id) {
|
|
return this.save(product);
|
|
}
|
|
return this.update(product);
|
|
}
|
|
|
|
delete(id: string): Observable<Product> {
|
|
return this.http
|
|
.delete<Product>(`${url}/${id}`)
|
|
.pipe(catchError(this.log.handleError(serviceName, '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[]
|
|
>;
|
|
}
|
|
|
|
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[]
|
|
>;
|
|
}
|
|
}
|