diff --git a/client/src/app/core/services/recipe.service.ts b/client/src/app/core/services/recipe.service.ts index 46fe787..1b291ea 100644 --- a/client/src/app/core/services/recipe.service.ts +++ b/client/src/app/core/services/recipe.service.ts @@ -12,8 +12,15 @@ interface RecipeParams { search: string; } +interface RecipeFiles { + [key: string]: string[]; +} + @Injectable({ providedIn: 'root' }) export class RecipeService { + private countries: string[] = []; + private recipeFiles: RecipeFiles = {}; + constructor(private _httpClient: HttpClient) {} getRecipes( @@ -74,10 +81,29 @@ export class RecipeService { }); } - getRecipeVersions(): Observable { - return this._httpClient.get( - environment.api + '/recipes/versions', - { 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[] { + return this.recipeFiles[country]; } } 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 ad1f039..6932258 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 @@ -109,6 +109,10 @@ export class RecipeDetailsComponent implements OnInit { (recipe) => recipe.materialPathId ); + if (originalRecipeDetail.recipe.recipes != null) { + return; + } + this._materialService.getMaterialCodes(ids).subscribe((data) => { this.originalRecipeDetail.next({ ...originalRecipeDetail, diff --git a/client/src/app/features/recipes/recipes.component.html b/client/src/app/features/recipes/recipes.component.html index 5318882..c116391 100644 --- a/client/src/app/features/recipes/recipes.component.html +++ b/client/src/app/features/recipes/recipes.component.html @@ -5,7 +5,7 @@
-
+
Recipe Version {{ recipes?.MachineSetting?.configNumber }} | @@ -13,7 +13,73 @@ >
-
= new BehaviorSubject(''); - recipeVersion$ = this.recipeVersion.asObservable(); + recipeFileFilter: BehaviorSubject = + new BehaviorSubject({ + country: null, + fileName: null, + }); + recipeFileFilter$ = this.recipeFileFilter.asObservable(); isLoaded: boolean = false; isLoadMore: boolean = false; @@ -111,11 +120,16 @@ export class DashboardComponent implements OnInit { this.isHasMore = hasMore; }); - this.recipeVersion$.subscribe((version) => { - if (version) - this.recipeVersions = lodash.filter(this.recipeVersionData, (v) => - v.includes(version) - ); + this.recipeFileFilter$.subscribe((version) => { + this.recipeFileCountries = lodash.filter( + this._recipeService.getRecipeFileCountries(), + (v) => { + if (version.country) { + return v.includes(version.country); + } + return true; + } + ); }); } @@ -189,9 +203,9 @@ export class DashboardComponent implements OnInit { getRecipeVersions() { if (this.recipeVersionData.length > 0) return; - this._recipeService.getRecipeVersions().subscribe((versions) => { + this._recipeService.getRecipeCountries().subscribe((versions) => { this.recipeVersionData = versions; - this.recipeVersions = versions; + this.recipeFileCountries = versions; }); } diff --git a/client/src/environments/environment.development.ts b/client/src/environments/environment.development.ts index 53fafeb..dd9d9ef 100644 --- a/client/src/environments/environment.development.ts +++ b/client/src/environments/environment.development.ts @@ -1,5 +1,4 @@ export const environment = { production: false, api: 'http://localhost:8080', - wsapi: 'ws://localhost:8080' }; diff --git a/server/cofffeemachineConfig b/server/cofffeemachineConfig index bb7e2e1..5adec80 160000 --- a/server/cofffeemachineConfig +++ b/server/cofffeemachineConfig @@ -1 +1 @@ -Subproject commit bb7e2e1d3ce239ac877ed0ecdad934eef4f2513d +Subproject commit 5adec80644b88c732a06331bfa002427bf0a84da diff --git a/server/data/data.go b/server/data/data.go index a461406..ed663dd 100644 --- a/server/data/data.go +++ b/server/data/data.go @@ -2,11 +2,12 @@ package data import ( "encoding/json" + "fmt" "log" "os" "path/filepath" + "recipe-manager/helpers" "recipe-manager/models" - "sort" "time" ) @@ -40,44 +41,34 @@ type RecipeWithTimeStamps struct { type Data struct { CurrentVersion string - AllVersions []string + AllRecipeFiles map[string][]helpers.RecipePath currentRecipe *models.Recipe recipeMap map[string]RecipeWithTimeStamps + Countries []helpers.CountryName } func NewData() *Data { - files, err := filepath.Glob("cofffeemachineConfig/coffeethai02_*.json") - if err != nil { - log.Panic("Error when scan recipe files:", err) + countries := []helpers.CountryName{{ + CountryID: "thai", + CountryName: "Thailand", + }, { + CountryID: "mys", + CountryName: "Malaysia", + }, { + CountryID: "aus", + CountryName: "Australia", + }, } - sort.Slice(files, func(i, j int) bool { - file1, err := os.Stat(files[i]) - - if err != nil { - log.Panic("Error when get file info:", err) - } - - file2, err := os.Stat(files[j]) - - if err != nil { - log.Panic("Error when get file info:", err) - } - - return file1.ModTime().After(file2.ModTime()) - }) - - for i := 0; i < len(files); i++ { - files[i] = filepath.Base(files[i]) - } + allRecipeFiles := helpers.ScanRecipeFiles(countries) defaultVersion := "coffeethai02_580.json" defaultRecipe := readFile(defaultVersion) return &Data{ CurrentVersion: defaultVersion, - AllVersions: files, + AllRecipeFiles: allRecipeFiles, currentRecipe: defaultRecipe, recipeMap: map[string]RecipeWithTimeStamps{ defaultVersion: { @@ -85,6 +76,7 @@ func NewData() *Data { TimeStamps: time.Now().Unix(), }, }, + Countries: countries, } } @@ -225,3 +217,21 @@ func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCo return resultFilter } + +func (d *Data) GetCountryNameByID(countryID string) (string, error) { + for _, country := range d.Countries { + if country.CountryID == countryID { + return country.CountryName, nil + } + } + return "", fmt.Errorf("country ID: %s not found", countryID) +} + +func (d *Data) GetCountryIDByName(countryName string) (string, error) { + for _, country := range d.Countries { + if country.CountryName == countryName { + return country.CountryID, nil + } + } + return "", fmt.Errorf("country name: %s not found", countryName) +} diff --git a/server/helpers/filereader.go b/server/helpers/filereader.go new file mode 100644 index 0000000..974ae1b --- /dev/null +++ b/server/helpers/filereader.go @@ -0,0 +1,101 @@ +package helpers + +import ( + "encoding/json" + "log" + "os" + "path/filepath" + "recipe-manager/models" + "sort" +) + +func ListFile(rootPath string) []string { + files, err := filepath.Glob(rootPath) + if err != nil { + log.Panic("Error when scan recipe files:", err) + } + return files +} + +func ReadFile(filePath string) (string, error) { + file, err := os.ReadFile(filePath) + + if err != nil { + log.Fatalf("Error when open file: %s", err) + return "", err + } + + return string(file), nil +} + +func ReadRecipeFile(filePath string) models.Recipe { + file, err := os.Open(filePath) + + if err != nil { + log.Fatalf("Error when open file: %s", err) + return models.Recipe{} + } + + defer file.Close() + + var data models.Recipe + + err = json.NewDecoder(file).Decode(&data) + + if err != nil { + log.Fatalf("Error when decode file: %s", err) + return models.Recipe{} + } + + return data +} + +type RecipePath struct { + Name string `json:"name"` + Path string `json:"path"` +} + +type CountryName struct { + CountryID string `json:"countryId"` + CountryName string `json:"countryName"` +} + +func ScanRecipeFiles(countries []CountryName) map[string][]RecipePath { + recipeFiles := map[string][]RecipePath{} + + for _, country := range countries { + var files []string + if country.CountryID == "thai" { + files = ListFile("cofffeemachineConfig/coffeethai02_*.json") + } else { + files = ListFile("cofffeemachineConfig/" + country.CountryID + "/coffeethai02_*.json") + } + sort.Slice(files, func(i, j int) bool { + file1, err := os.Stat(files[i]) + + if err != nil { + log.Panic("Error when get file info:", err) + } + + file2, err := os.Stat(files[j]) + + if err != nil { + log.Panic("Error when get file info:", err) + } + + return file1.ModTime().After(file2.ModTime()) + }) + + for i := 0; i < len(files); i++ { + if _, ok := recipeFiles[country.CountryID]; !ok { + recipeFiles[country.CountryID] = []RecipePath{} + } + + recipeFiles[country.CountryID] = append(recipeFiles[country.CountryID], RecipePath{ + Name: filepath.Base(files[i]), + Path: files[i], + }) + } + } + return recipeFiles +} diff --git a/server/routers/recipe.go b/server/routers/recipe.go index 0ab7133..fcea72d 100644 --- a/server/routers/recipe.go +++ b/server/routers/recipe.go @@ -2,6 +2,7 @@ package routers import ( "encoding/json" + "fmt" "net/http" "recipe-manager/data" "recipe-manager/models" @@ -123,7 +124,33 @@ func (rr *RecipeRouter) Route(r chi.Router) { }) r.Get("/versions", func(w http.ResponseWriter, r *http.Request) { - json.NewEncoder(w).Encode(rr.data.AllVersions) + w.Header().Add("Content-Type", "application/json") + // get key from map + keys := []string{} + for k := range rr.data.AllRecipeFiles { + countryName, err := rr.data.GetCountryNameByID(k) + if err != nil { + continue + } + keys = append(keys, countryName) + } + json.NewEncoder(w).Encode(keys) + }) + + r.Get("/versions/{country}", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + + countryName := chi.URLParam(r, "country") + countryID, err := rr.data.GetCountryIDByName(countryName) + if err != nil { + http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", countryName), http.StatusNotFound) + return + } + files := []string{} + for _, v := range rr.data.AllRecipeFiles[countryID] { + files = append(files, v.Name) + } + json.NewEncoder(w).Encode(files) }) r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) {