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" "time" "github.com/go-chi/chi/v5" "go.uber.org/zap" ) type RecipeRouter struct { data *data.Data sheetService sheet.SheetService recipeService recipe.RecipeService } var ( Log = logger.GetInstance() ) func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService) *RecipeRouter { return &RecipeRouter{ data: data, recipeService: recipeService, sheetService: sheetService, } } func (rr *RecipeRouter) Route(r chi.Router) { r.Route("/recipes", func(r chi.Router) { r.Get("/dashboard", func(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 } json.NewEncoder(w).Encode(result) }) r.Get("/overview", func(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 } json.NewEncoder(w).Encode(result) }) r.Get("/{product_code}", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") productCode := chi.URLParam(r, "product_code") // 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: r.URL.Query().Get("filename"), Country: r.URL.Query().Get("country"), ProductCode: productCode, }) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } json.NewEncoder(w).Encode(result) }) r.Get("/{product_code}/mat", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") productCode := chi.URLParam(r, "product_code") result, err := rr.recipeService.GetRecipeDetailMat(&contracts.RecipeDetailRequest{ Filename: r.URL.Query().Get("filename"), Country: r.URL.Query().Get("country"), ProductCode: productCode, }) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } json.NewEncoder(w).Encode(result) }) r.Get("/{country}/{filename}/json", func(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 { http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } json.NewEncoder(w).Encode(rr.data.GetRecipe(countryID, filename)) }) r.Get("/versions", func(w http.ResponseWriter, r *http.Request) { 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) { result := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE") 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), }) } json.NewEncoder(w).Encode(mapResult) }) r.Post("/edit/{country}/{filename}", func(w http.ResponseWriter, r *http.Request) { 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 { http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } target_recipe := rr.data.GetRecipe(countryID, filename) Log.Debug("Target => ", zap.Any("target", target_recipe.MachineSetting.ConfigNumber)) // check request structure // Body var ch_map map[string]interface{} var changes models.Recipe01 err = json.NewDecoder(r.Body).Decode(&ch_map) if err != nil { Log.Error("Decode in request failed: ", zap.Error(err)) } // commit request editor := ch_map["edit_by"].(string) Log.Debug("requester", zap.Any("editor", editor)) commit_msg := ch_map["commit_msg"].(string) Log.Debug("commit_msg", zap.Any("commit_msg", commit_msg)) changes = changes.FromMap(ch_map) Log.Debug("Changes: ", zap.Any("changes", changes)) // TODO: find the matched pd target_menu, err := rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode) if err != nil { Log.Error("Error when get recipe by product code", zap.Error(err)) return } menu_map := target_menu.ToMap() change_map := changes.ToMap() // Find changes for key, val := range menu_map { test_bool, err := helpers.DynamicCompare(val, change_map[key]) if err != nil { Log.Error("DynamicCompare in request failed: ", zap.Error(err)) } if !test_bool { menu_map[key] = change_map[key] } } // Apply changes tempRecipe := models.Recipe01{} tempRecipe = tempRecipe.FromMap(menu_map) rr.data.SetValuesToRecipe(tempRecipe) 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) if err != nil { Log.Error("Error when hash commit", zap.Error(err)) return } commit := data.CommitLog{ Id: commit_hash, Msg: commit_msg, Created_at: time.Now().Format("2006-01-02 15:04:05"), Editor: editor, Change_file: temp_file_name, } err = data.Insert(&commit) Log.Debug("Commit", zap.Any("attr", commit)) if err != nil { Log.Error("Error when insert commit log", zap.Error(err)) return } file, _ := os.Create(temp_file_name) if err != nil { Log.Error("Error when tried to create file", zap.Error(err)) return } encoder := json.NewEncoder(file) encoder.SetIndent("", " ") // err = encoder.Encode(rr.data.GetRecipe(countryID, temp_file_name)) err = encoder.Encode(rr.data.GetRecipe(countryID, filename)) if err != nil { Log.Error("Error when write file", zap.Error(err)) } w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "status": "OK", "commit_id": commit_hash, }) }) // get saved files r.Get("/saved/{country}/{filename_version_only}", func(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 } // recipe_root_path := "./cofffeemachineConfig/" // structure // full_file_name_targets := []string{} // files, err := os.ReadDir(recipe_root_path + countryID) // if err != nil { // Log.Error("Error when read directory", zap.Error(err)) // return // } // for _, file := range files { // Log.Debug("File: ", zap.Any("file", file.Name())) // if strings.Contains(file.Name(), file_version) && strings.Contains(file.Name(), ".tmp") { // full_file_name_targets = append(full_file_name_targets, file.Name()) // } // } commits, err := data.GetCommitLogOfFilename(countryID, file_version) if err != nil { Log.Error("Error when get commit log", zap.Error(err)) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]interface{}{"files": commits}) Log.Debug("Saved Files: ", zap.Any("files", commits)) }) }) }