From 3bfbbd778ad25036238a5eed3c82186ff29ef1de Mon Sep 17 00:00:00 2001 From: Kenta420 Date: Tue, 24 Oct 2023 18:01:52 +0700 Subject: [PATCH] update getting file recipe --- .../src/app/core/services/material.service.ts | 12 +- .../src/app/core/services/recipe.service.ts | 97 +++++++++------- .../recipe-details.component.ts | 7 +- .../features/recipes/recipes.component.html | 14 ++- .../app/features/recipes/recipes.component.ts | 69 ++++++++--- server/data/data.go | 109 ++++++++---------- server/helpers/filereader.go | 16 ++- server/models/recipe.go | 2 +- server/routers/material.go | 24 +++- server/routers/recipe.go | 45 ++++++-- 10 files changed, 243 insertions(+), 152 deletions(-) diff --git a/client/src/app/core/services/material.service.ts b/client/src/app/core/services/material.service.ts index 4128cd9..0daa4db 100644 --- a/client/src/app/core/services/material.service.ts +++ b/client/src/app/core/services/material.service.ts @@ -10,13 +10,15 @@ export class MaterialService { getMaterialCodes( matIds?: number[], - version?: string + country?: string, + filename?: string ): Observable { return this._httpClient.get( `${environment.api}/materials/code`, { params: { - version: version || '', + country: country || '', + filename: filename || '', mat_ids: matIds?.join(',') || '', }, withCredentials: true, @@ -26,13 +28,15 @@ export class MaterialService { getMaterialSettingById( id: number, - version?: string + country?: string, + filename?: string ): Observable { return this._httpClient.get( `${environment.api}/materials/setting/${id}`, { params: { - version: version || '', + country: country || '', + filename: filename || '', }, withCredentials: true, } diff --git a/client/src/app/core/services/recipe.service.ts b/client/src/app/core/services/recipe.service.ts index 4764a0d..f1eee4a 100644 --- a/client/src/app/core/services/recipe.service.ts +++ b/client/src/app/core/services/recipe.service.ts @@ -6,7 +6,8 @@ import { environment } from 'src/environments/environment'; import { RecipeMetaData } from 'src/app/shared/types/recipe'; interface RecipeParams { - version: string; + filename: string; + country: string; offset: number; take: number; search: string; @@ -28,46 +29,49 @@ export class RecipeService { take: 10, offset: 0, search: '', - version: this.getCurrentVersion(), + country: this.getCurrentCountry(), + filename: this.getCurrentFile(), } ): Observable<{ fileName: string; recipes: Recipe; hasMore: boolean; }> { - return this._httpClient - .get<{ - fileName: string; - recipes: Recipe; - hasMore: boolean; - }>(environment.api + '/recipes', { - params: { - offset: params.offset, - take: params.take, - search: params.search, - version: params.version, - }, - withCredentials: true, - responseType: 'json', - }) - .pipe( - tap((data) => { - if (data.fileName !== this.getCurrentVersion()) { - localStorage.setItem('currentRecipeVersion', data.fileName); - } - }) - ); + return this._httpClient.get<{ + fileName: string; + recipes: Recipe; + hasMore: boolean; + }>(environment.api + '/recipes', { + params: { + offset: params.offset, + take: params.take, + search: params.search, + country: params.country, + filename: params.filename, + }, + withCredentials: true, + responseType: 'json', + }); } - getCurrentVersion(): string { - const currentRecipeVersion = localStorage.getItem('currentRecipeVersion'); - if (currentRecipeVersion) { - return currentRecipeVersion; + getCurrentFile(): string { + const currentRecipeFile = localStorage.getItem('currentRecipeFile'); + if (currentRecipeFile) { + return currentRecipeFile; } return 'coffeethai02_580.json'; } + getCurrentCountry(): string { + const currentRecipeCountry = localStorage.getItem('currentRecipeCountry'); + if (currentRecipeCountry) { + return currentRecipeCountry; + } + + return 'Thailand'; + } + getRecipesById(id: string): Observable<{ recipe: Recipe01; recipeMetaData: RecipeMetaData; @@ -103,23 +107,28 @@ export class RecipeService { return this.countries; } - getRecipeFileNames(country: string): string[] { - return this.recipeFiles[country]; + getRecipeFileNames(country: string): string[] | null { + return this.recipeFiles[country] ?? null; } - editChanges(version: string, change: any){ - console.log("target version = ", version); - console.log("change in edit: ",change.value) - return this._httpClient.post<{ - status: string - }>( - environment.api + ('/recipes/edit/'+version), - change.value, - { withCredentials: true, responseType: 'json', } - ).subscribe({ - next(value) { - console.log(value, change.value) - }, - }); + editChanges(country: string, filename: string, change: any) { + console.log('target version = ', filename); + console.log('change in edit: ', change.value); + return this._httpClient + .post<{ + status: string; + }>( + environment.api + ('/recipes/edit/' + country + '/' + filename), + change.value, + { + withCredentials: true, + responseType: 'json', + } + ) + .subscribe({ + next(value) { + console.log(value, change.value); + }, + }); } } diff --git a/client/src/app/features/recipes/recipe-details/recipe-details.component.ts b/client/src/app/features/recipes/recipe-details/recipe-details.component.ts index 877452c..09e39b0 100644 --- a/client/src/app/features/recipes/recipe-details/recipe-details.component.ts +++ b/client/src/app/features/recipes/recipe-details/recipe-details.component.ts @@ -188,12 +188,13 @@ export class RecipeDetailsComponent implements OnInit { console.log('confirm save'); // TODO: update value in targeted recipe this._recipeService.editChanges( - this._recipeService.getCurrentVersion(), + this._recipeService.getCurrentCountry(), + this._recipeService.getCurrentFile(), { - ...this.recipeDetail + ...this.recipeDetail, } ); - console.log("Sending changes") + console.log('Sending changes'); this._router.navigate(['/recipes']); }, }; diff --git a/client/src/app/features/recipes/recipes.component.html b/client/src/app/features/recipes/recipes.component.html index b8674be..fea70a7 100644 --- a/client/src/app/features/recipes/recipes.component.html +++ b/client/src/app/features/recipes/recipes.component.html @@ -27,11 +27,12 @@