add load from select save

This commit is contained in:
pakintada@gmail.com 2023-12-27 08:38:14 +07:00
parent f721517f25
commit 17030c72ce
10 changed files with 229 additions and 53 deletions

View file

@ -57,6 +57,8 @@ func (rr *RecipeRouter) Route(r chi.Router) {
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) {
@ -347,16 +349,16 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
// 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 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 {
if !testBool && err == nil {
menuMap[key] = changeMap[key]
}
}
@ -364,7 +366,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
// Apply changes
tempRecipe := models.Recipe01{}
tempRecipe = tempRecipe.FromMap(menuMap)
rr.data.SetValuesToRecipe(tempRecipe)
rr.data.SetValuesToRecipe(targetRecipe.Recipe01, tempRecipe)
rr.taoLogger.Log.Debug("ApplyChange", zap.Any("status", "passed"))
// check if changed
@ -385,7 +387,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
Id: commit_hash,
Msg: commit_msg,
Created_at: time.Now().Format("2006-01-02 15:04:05"),
Created_at: time.Now().Local().Format("2006-01-02 15:04:05"),
Editor: editor,
Change_file: temp_file_name,
}
@ -401,7 +403,11 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(rr.data.GetRecipe(countryID, filename))
// 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")))
@ -429,6 +435,8 @@ func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request)
commits, err := data.GetCommitLogOfFilename(countryID, file_version)
rr.taoLogger.Log.Debug("RecipeRouter.getSavedRecipes", zap.Any("commits", commits))
if err != nil {
return
@ -440,6 +448,94 @@ func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request)
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) {