package routers import ( "encoding/json" "fmt" "net/http" "os" "path" "recipe-manager/contracts" "recipe-manager/data" "recipe-manager/helpers" "recipe-manager/models" "recipe-manager/services/logger" "recipe-manager/services/recipe" "recipe-manager/services/sheet" "strconv" "strings" "sync" "time" "github.com/go-chi/chi/v5" "github.com/pkg/errors" "go.uber.org/zap" ) type RecipeRouter struct { data *data.Data sheetService sheet.SheetService recipeService recipe.RecipeService taoLogger *logger.TaoLogger } var ( binaryApiLock sync.Mutex ) func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService, taoLogger *logger.TaoLogger) *RecipeRouter { return &RecipeRouter{ data, sheetService, recipeService, taoLogger, } } func (rr *RecipeRouter) Route(r chi.Router) { r.Route("/recipes", func(r chi.Router) { r.Get("/dashboard", rr.dashBoard) r.Get("/overview", rr.overview) r.Get("/{product_code}", rr.getRecipeByProductCode) r.Get("/{product_code}/mat", rr.getRecipeMatByProductCode) r.Get("/{country}/{filename}/json", rr.getRecipeJson) r.Post("/edit/{country}/{filename}", rr.updateRecipe) r.Post("/upgrade/{country}/{filename}", rr.ApplyTmpChanges) r.Get("/saved/{country}/{filename_version_only}", rr.getSavedRecipes) r.Get("/countries", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") // get key from map var keys []string for k := range rr.data.AllRecipeFiles { countryName, err := rr.data.GetCountryNameByID(k) if err != nil { continue } keys = append(keys, countryName) } if err := json.NewEncoder(w).Encode(keys); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetCountryRecipe", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) r.Post("/merge", rr.doMergeJson) r.Get("/{country}/versions", 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 } var files []string for _, v := range rr.data.AllRecipeFiles[countryID] { files = append(files, v.Name) } if err := json.NewEncoder(w).Encode(files); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetVersionsCountryRecipe", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) { result := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE") var mapResult []map[string]string for _, v := range result { mapResult = append(mapResult, map[string]string{ "productCode": v[0].(string), "name": v[1].(string), "otherName": v[2].(string), "description": v[3].(string), "otherDescription": v[4].(string), "picture": v[5].(string), }) } if err := json.NewEncoder(w).Encode(mapResult); err != nil { rr.taoLogger.Log.Error("RecipeRouter.TestSheet", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) }) } // ====================== Handler ================================= func (rr *RecipeRouter) dashBoard(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") country := r.URL.Query().Get("country") filename := r.URL.Query().Get("filename") result, err := rr.recipeService.GetRecipeDashboard(&contracts.RecipeDashboardRequest{ Country: country, Filename: filename, }) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.Dashboard", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) overview(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") var take, offset uint64 = 10, 0 if newOffset, err := strconv.ParseUint(r.URL.Query().Get("offset"), 10, 64); err == nil { offset = newOffset } if newTake, err := strconv.ParseUint(r.URL.Query().Get("take"), 10, 64); err == nil { take = newTake } country := r.URL.Query().Get("country") filename := r.URL.Query().Get("filename") materialIds := r.URL.Query().Get("materialIds") var materialIdsUint []int for _, v := range strings.Split(materialIds, ",") { materialIdUint, err := strconv.ParseUint(v, 10, 64) if err != nil || materialIdUint == 0 { continue } materialIdsUint = append(materialIdsUint, int(materialIdUint)) } result, err := rr.recipeService.GetRecipeOverview(&contracts.RecipeOverviewRequest{ Take: int(take), Skip: int(offset), Search: r.URL.Query().Get("search"), Country: country, Filename: filename, MatIds: materialIdsUint, }) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.Overview", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) getRecipeByProductCode(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") productCode := chi.URLParam(r, "product_code") fileName := r.URL.Query().Get("filename") country := r.URL.Query().Get("country") // recipe := rr.data.GetRecipe01() // recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE") // var recipeResult *models.Recipe01 // recipeMetaDataResult := map[string]string{} // for _, v := range recipe { // if v.ProductCode == productCode { // recipeResult = &v // break // } // } // for _, v := range recipeMetaData { // if v[0].(string) == productCode { // recipeMetaDataResult = map[string]string{ // "productCode": v[0].(string), // "name": v[1].(string), // "otherName": v[2].(string), // "description": v[3].(string), // "otherDescription": v[4].(string), // "picture": v[5].(string), // } // break // } // } // if recipeResult == nil { // http.Error(w, "Not Found", http.StatusNotFound) // return // } // json.NewEncoder(w).Encode(map[string]interface{}{ // "recipe": recipeResult, // "recipeMetaData": recipeMetaDataResult, // }) result, err := rr.recipeService.GetRecipeDetail(&contracts.RecipeDetailRequest{ Filename: fileName, Country: country, ProductCode: productCode, }) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeByProductCode", zap.Error(err)) http.Error(w, fmt.Sprintf("Recipe file: %s with productCode: %s not found", fileName, productCode), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeByProductCode", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) getRecipeMatByProductCode(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") productCode := chi.URLParam(r, "product_code") fileName := r.URL.Query().Get("filename") country := r.URL.Query().Get("country") result, err := rr.recipeService.GetRecipeDetailMat(&contracts.RecipeDetailRequest{ Filename: fileName, Country: country, ProductCode: productCode, }) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeMatByProductCode", zap.Error(err)) http.Error(w, fmt.Sprintf("Material Recipe file: %s with productCode: %s not found", fileName, productCode), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeMatByProductCode", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) getRecipeJson(w http.ResponseWriter, r *http.Request) { country := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") w.Header().Add("Content-Type", "application/json") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeJson", zap.Error(err)) http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(rr.data.GetRecipe(countryID, filename)); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeJson", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) { rr.taoLogger.Log.Debug("Edit: ", zap.String("path", r.RequestURI)) filename := chi.URLParam(r, "filename") country := chi.URLParam(r, "country") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(err)) http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } targetRecipe := rr.data.GetRecipe(countryID, filename) rr.taoLogger.Log.Debug("Target => ", zap.Any("target", targetRecipe.MachineSetting.ConfigNumber)) // Body var ch_map map[string]interface{} var changes models.Recipe01 err = json.NewDecoder(r.Body).Decode(&ch_map) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Decode in request failed"))) http.Error(w, "Can't Decode Recipe request body.", http.StatusBadRequest) return } // commit request editor := ch_map["edit_by"].(string) commit_msg := ch_map["commit_msg"].(string) changes = changes.FromMap(ch_map) rr.taoLogger.Log.Debug("Changes: ", zap.Any("changes", changes)) // TODO: find the matched pd targetMenu, err := rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when get recipe by product code"))) http.Error(w, fmt.Sprintf("Recipe country: %s file: %s productCode: %s not found.", country, filename, changes.ProductCode), http.StatusNotFound) return } menuMap := targetMenu.ToMap() changeMap := changes.ToMap() // Find changes for key, val := range menuMap { // rr.taoLogger.Log.Debug("..DynamicCompare.key", zap.Any("key", key)) testBool, err := helpers.DynamicCompare(val, changeMap[key]) // if err != nil { // rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "DynamicCompare in request failed"))) // http.Error(w, "Internal Error", http.StatusInternalServerError) // return // } if !testBool && err == nil { menuMap[key] = changeMap[key] } } // Apply changes tempRecipe := models.Recipe01{} tempRecipe = tempRecipe.FromMap(menuMap) rr.data.SetValuesToRecipe(targetRecipe.Recipe01, tempRecipe) rr.taoLogger.Log.Debug("ApplyChange", zap.Any("status", "passed")) // check if changed // Log.Debug("Check if changed", zap.Any("result", rr.data.GetRecipe01ByProductCode(changes.ProductCode))) // target saved filename saved_filename := path.Join("./cofffeemachineConfig", countryID, filename) // store @ temporary file temp_file_name := helpers.GetTempFile(saved_filename, editor, 0) // TODO: push this change, editor, commit_msg into db // gen hash commit_hash, err := data.HashCommit(8) commit := data.CommitLog{ Id: commit_hash, Msg: commit_msg, Created_at: time.Now().Local().Format("2006-01-02 15:04:05"), Editor: editor, Change_file: temp_file_name, } err = data.Insert(&commit) file, _ := os.Create(temp_file_name) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when tried to create file"))) http.Error(w, "Internal Error", http.StatusInternalServerError) return } encoder := json.NewEncoder(file) encoder.SetIndent("", " ") // full err = encoder.Encode(targetRecipe) // partial // err = encoder.Encode(changes) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when write file"))) http.Error(w, "Internal Error", http.StatusInternalServerError) return } w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "status": "OK", "commit_id": commit_hash, }) } func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request) { file_version := chi.URLParam(r, "filename_version_only") country := chi.URLParam(r, "country") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } commits, err := data.GetCommitLogOfFilename(countryID, file_version) rr.taoLogger.Log.Debug("RecipeRouter.getSavedRecipes", zap.Any("commits", commits)) if err != nil { return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]interface{}{"files": commits}) } func (rr *RecipeRouter) ApplyTmpChanges(w http.ResponseWriter, r *http.Request) { filename := chi.URLParam(r, "filename") country := chi.URLParam(r, "country") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(err)) http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } var legit_changes map[string]interface{} target_recipe := rr.data.GetRecipe(countryID, filename) new_file_version := target_recipe.MachineSetting.ConfigNumber + 1 show_to_current := legit_changes["no_upgrade"].(bool) if show_to_current { new_file_version = target_recipe.MachineSetting.ConfigNumber } err = json.NewDecoder(r.Body).Decode(&legit_changes) if err != nil { rr.taoLogger.Log.Error("ApplyErr", zap.Any("err", err)) return } user_selected_tmp := legit_changes["selected_files"].([]string) for select_tmp := range user_selected_tmp { // open selected current_temp_file, err := os.ReadFile(user_selected_tmp[select_tmp]) if err != nil { rr.taoLogger.Log.Error("TmpFile", zap.Any("Open", "tried to open but failed => "+user_selected_tmp[select_tmp])) continue } // load to model temp_recipe := models.Recipe01{} json.Unmarshal(current_temp_file, &temp_recipe) // apply set value rr.data.SetValuesToRecipe(target_recipe.Recipe01, temp_recipe) rr.taoLogger.Log.Debug("ApplyTmpChanges", zap.Any("Update|Push", string(rune(temp_recipe.ID))+":"+temp_recipe.ProductCode)) } // export exported_filename := "coffeethai02_" + string(rune(new_file_version)) if countryID != "tha" { exported_filename += "_" + countryID } exported_filename += ".json" full_exported_path := "cofffeemachineConfig/" + countryID + "/" + exported_filename outfile, _ := os.Create(full_exported_path) target_recipe.MachineSetting.ConfigNumber = new_file_version encoder := json.NewEncoder(outfile) encoder.SetIndent("", " ") err = encoder.Encode(target_recipe) if err != nil { rr.taoLogger.Log.Error("UpgradeToFullRecipeFailed", zap.Any("File", err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "status": "OK", "version": new_file_version, "path": full_exported_path, }) delete_files := legit_changes["del_after_upgrade"].(bool) if delete_files && !show_to_current { for seleted_file := range user_selected_tmp { os.Remove(user_selected_tmp[seleted_file]) } } } func (rr *RecipeRouter) doMergeJson(w http.ResponseWriter, r *http.Request) { // TODO: v2, change to binary instead if !binaryAPIhandler(w, r) { rr.taoLogger.Log.Warn("RecipeRouter.doMergeJson", zap.Error(errors.New("API is busy"))) return } else { rr.taoLogger.Log.Debug("RecipeRouter.doMergeJson", zap.Any("status", "ready")) } // TODO: add binary command here } func binaryAPIhandler(w http.ResponseWriter, r *http.Request) bool { timeout := 10 * time.Second if !lockThenTimeout(&binaryApiLock, timeout) { http.Error(w, "API is busy", http.StatusServiceUnavailable) return false } defer binaryApiLock.Unlock() return true } func lockThenTimeout(mutex *sync.Mutex, timeout time.Duration) bool { ch := make(chan struct{}) go func() { mutex.Lock() close(ch) }() select { case <-ch: return true case <-time.After(timeout): return false } }