From f2ec0ed5fa3e9405b7386e8fa2f7469cace0763d Mon Sep 17 00:00:00 2001 From: "pakintada@gmail.com" Date: Wed, 6 Dec 2023 10:05:16 +0700 Subject: [PATCH] display commit at recipe --- .../src/app/core/services/recipe.service.ts | 418 ++++++----- .../features/recipes/recipes.component.html | 13 +- .../app/features/recipes/recipes.component.ts | 4 +- server/data/commit.go | 45 +- server/data/data.go | 682 +++++++++--------- server/routers/recipe.go | 33 +- 6 files changed, 640 insertions(+), 555 deletions(-) diff --git a/client/src/app/core/services/recipe.service.ts b/client/src/app/core/services/recipe.service.ts index 521354a..c00e6cb 100644 --- a/client/src/app/core/services/recipe.service.ts +++ b/client/src/app/core/services/recipe.service.ts @@ -1,194 +1,224 @@ -import { HttpClient } from '@angular/common/http'; -import { Injectable } from '@angular/core'; -import { Observable, tap } from 'rxjs'; -import { - Recipe, - Recipe01, - RecipeDetail, - RecipeDetailMat, - RecipeOverview, - RecipeOverviewList, - RecipesDashboard, -} from '../models/recipe.model'; -import { environment } from 'src/environments/environment'; -import { RecipeMetaData } from 'src/app/shared/types/recipe'; - -type RecipeOverviewParams = { - filename: string; - country: string; - materialIds: number[]; - offset: number; - take: number; - search: string; -}; - -type RecipeDashboardParams = { - filename: string; - country: string; -}; - -interface RecipeFiles { - [key: string]: string[]; -} - -@Injectable({ providedIn: 'root' }) -export class RecipeService { - private countries: string[] = []; - private recipeFiles: RecipeFiles = {}; - - constructor(private _httpClient: HttpClient) {} - - getRecipesDashboard( - params: RecipeDashboardParams = { - country: this.getCurrentCountry(), - filename: this.getCurrentFile(), - } - ): Observable { - return this._httpClient.get( - environment.api + '/recipes/dashboard', - { - params: { - country: params.country, - filename: params.filename, - }, - withCredentials: true, - responseType: 'json', - } - ); - } - - getRecipeOverview( - params: RecipeOverviewParams = { - country: this.getCurrentCountry(), - filename: this.getCurrentFile(), - materialIds: [], - offset: 0, - take: 20, - search: '', - } - ): Observable { - return this._httpClient.get( - environment.api + '/recipes/overview', - { - params: { - country: params.country, - filename: params.filename, - materialIds: params.materialIds.join(','), - offset: params.offset.toString(), - take: params.take.toString(), - search: params.search, - }, - withCredentials: true, - responseType: 'json', - } - ); - } - - getRecipeDetail(productCode: string): Observable { - return this._httpClient.get( - environment.api + '/recipes/' + productCode, - { - params: { - filename: this.getCurrentFile(), - country: this.getCurrentCountry(), - }, - withCredentials: true, - responseType: 'json', - } - ); - } - - getRecipeDetailMat( - productCode: string - ): Observable<{ result: RecipeDetailMat[] }> { - return this._httpClient.get<{ result: RecipeDetailMat[] }>( - environment.api + '/recipes/' + productCode + '/mat', - { - params: { - filename: this.getCurrentFile(), - country: this.getCurrentCountry(), - }, - withCredentials: true, - responseType: 'json', - } - ); - } - - 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; - }> { - return this._httpClient.get<{ - recipe: Recipe01; - recipeMetaData: RecipeMetaData; - }>(environment.api + '/recipes/' + id, { - withCredentials: true, - responseType: 'json', - }); - } - - getRecipeCountries(): Observable { - return this._httpClient - .get(environment.api + '/recipes/versions', { - withCredentials: true, - responseType: 'json', - }) - .pipe(tap((countries) => (this.countries = countries))); - } - - getRecipeFiles(country: string): Observable { - return this._httpClient - .get(environment.api + '/recipes/versions/' + country, { - withCredentials: true, - responseType: 'json', - }) - .pipe(tap((files) => (this.recipeFiles[country] = files))); - } - - getRecipeFileCountries(): string[] { - return this.countries; - } - - getRecipeFileNames(country: string): string[] | null { - return this.recipeFiles[country] ?? null; - } - - 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); - }, - }); - } -} +import { HttpClient } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Observable, tap } from 'rxjs'; +import { + Recipe, + Recipe01, + RecipeDetail, + RecipeDetailMat, + RecipeOverview, + RecipeOverviewList, + RecipesDashboard, +} from '../models/recipe.model'; +import { environment } from 'src/environments/environment'; +import { RecipeMetaData } from 'src/app/shared/types/recipe'; + +type RecipeOverviewParams = { + filename: string; + country: string; + materialIds: number[]; + offset: number; + take: number; + search: string; +}; + +type RecipeDashboardParams = { + filename: string; + country: string; +}; + +interface RecipeFiles { + [key: string]: string[]; +} + +@Injectable({ providedIn: 'root' }) +export class RecipeService { + private countries: string[] = []; + private recipeFiles: RecipeFiles = {}; + + private tmp_files: string[] = []; + + private get tmpfiles(): string[] { + return this.tmp_files; + } + + constructor(private _httpClient: HttpClient) {} + + getRecipesDashboard( + params: RecipeDashboardParams = { + country: this.getCurrentCountry(), + filename: this.getCurrentFile(), + } + ): Observable { + return this._httpClient.get( + environment.api + '/recipes/dashboard', + { + params: { + country: params.country, + filename: params.filename, + }, + withCredentials: true, + responseType: 'json', + } + ); + } + + getRecipeOverview( + params: RecipeOverviewParams = { + country: this.getCurrentCountry(), + filename: this.getCurrentFile(), + materialIds: [], + offset: 0, + take: 20, + search: '', + } + ): Observable { + return this._httpClient.get( + environment.api + '/recipes/overview', + { + params: { + country: params.country, + filename: params.filename, + materialIds: params.materialIds.join(','), + offset: params.offset.toString(), + take: params.take.toString(), + search: params.search, + }, + withCredentials: true, + responseType: 'json', + } + ); + } + + getRecipeDetail(productCode: string): Observable { + return this._httpClient.get( + environment.api + '/recipes/' + productCode, + { + params: { + filename: this.getCurrentFile(), + country: this.getCurrentCountry(), + }, + withCredentials: true, + responseType: 'json', + } + ); + } + + getRecipeDetailMat( + productCode: string + ): Observable<{ result: RecipeDetailMat[] }> { + return this._httpClient.get<{ result: RecipeDetailMat[] }>( + environment.api + '/recipes/' + productCode + '/mat', + { + params: { + filename: this.getCurrentFile(), + country: this.getCurrentCountry(), + }, + withCredentials: true, + responseType: 'json', + } + ); + } + + 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; + }> { + return this._httpClient.get<{ + recipe: Recipe01; + recipeMetaData: RecipeMetaData; + }>(environment.api + '/recipes/' + id, { + withCredentials: true, + responseType: 'json', + }); + } + + getRecipeCountries(): Observable { + return this._httpClient + .get(environment.api + '/recipes/versions', { + withCredentials: true, + responseType: 'json', + }) + .pipe(tap((countries) => (this.countries = countries))); + } + + getRecipeFiles(country: string): Observable { + return this._httpClient + .get(environment.api + '/recipes/versions/' + country, { + withCredentials: true, + responseType: 'json', + }) + .pipe(tap((files) => (this.recipeFiles[country] = files))); + } + + getRecipeFileCountries(): string[] { + return this.countries; + } + + getRecipeFileNames(country: string): string[] | null { + return this.recipeFiles[country] ?? null; + } + + editChanges(country: string, filename: string, change: any) { + console.log('target version = ', filename); + console.log('change in edit: ', change); + return this._httpClient + .post<{ + status: string; + }>( + environment.api + ('/recipes/edit/' + country + '/' + filename), + JSON.stringify(change), + { + withCredentials: true, + responseType: 'json', + } + ) + .subscribe({ + next(value) { + console.log( value); + }, + }); + } + + getSavedTmp(country: string, filename:string) { + console.log("loading saved .tmp* file", country, filename) + + // do split filename + filename = filename.split('_')[1]; + + + // this._user.getCurrentUser().subscribe((user) => { + // this.user = user.user; + // }) + return this._httpClient + .get( + environment.api + ("/recipes/saved/"+ country + "/" + filename), + { + withCredentials: true, + } + ); + // .subscribe({ + // next(value) { + // console.log( value); + // }, + // }); + } +} diff --git a/client/src/app/features/recipes/recipes.component.html b/client/src/app/features/recipes/recipes.component.html index 679e57f..e7bd8da 100644 --- a/client/src/app/features/recipes/recipes.component.html +++ b/client/src/app/features/recipes/recipes.component.html @@ -112,17 +112,20 @@ -