2021-11-10 05:23:07 +00:00
|
|
|
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 { ProductSku } from '../core/product-sku';
|
|
|
|
|
|
|
|
import { Recipe } from './recipe';
|
|
|
|
|
|
|
|
const url = '/api/recipes';
|
|
|
|
const serviceName = 'RecipeService';
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
export class RecipeService {
|
|
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
|
|
|
|
get(id: string | null): Observable<Recipe> {
|
|
|
|
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
|
|
|
return this.http
|
|
|
|
.get<Recipe>(getUrl)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Recipe>;
|
|
|
|
}
|
|
|
|
|
|
|
|
list(): Observable<Recipe[]> {
|
|
|
|
return this.http
|
|
|
|
.get<Recipe[]>(`${url}/list`)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'getList'))) as Observable<Recipe[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
save(recipe: Recipe): Observable<Recipe> {
|
|
|
|
return this.http
|
|
|
|
.post<Recipe>(`${url}`, recipe)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Recipe>;
|
|
|
|
}
|
|
|
|
|
|
|
|
update(recipe: Recipe): Observable<Recipe> {
|
|
|
|
return this.http
|
|
|
|
.put<Recipe>(`${url}/${recipe.id}`, recipe)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Recipe>;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveOrUpdate(recipe: Recipe): Observable<Recipe> {
|
|
|
|
if (!recipe.id) {
|
|
|
|
return this.save(recipe);
|
|
|
|
}
|
|
|
|
return this.update(recipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(id: string): Observable<Recipe> {
|
|
|
|
return this.http
|
|
|
|
.delete<Recipe>(`${url}/${id}`)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Recipe>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getIngredientDetails(
|
|
|
|
id: string,
|
|
|
|
startDate: string | null,
|
|
|
|
finishDate: string | null,
|
|
|
|
): Observable<ProductSku> {
|
2022-07-17 03:47:20 +00:00
|
|
|
const getUrl = `${url}/ingredient-details/${id}`;
|
2021-11-10 05:23:07 +00:00
|
|
|
const options = {
|
|
|
|
params: new HttpParams(),
|
|
|
|
};
|
|
|
|
if (startDate !== null) {
|
|
|
|
options.params = options.params.set('s', startDate);
|
|
|
|
}
|
|
|
|
if (finishDate !== null) {
|
|
|
|
options.params = options.params.set('f', finishDate);
|
|
|
|
}
|
|
|
|
return this.http
|
|
|
|
.get<ProductSku>(getUrl, options)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, `get id=${id}`)),
|
|
|
|
) as Observable<ProductSku>;
|
|
|
|
}
|
|
|
|
}
|