fix edit file, savable now, req fix bug

This commit is contained in:
pakintada@gmail.com 2023-11-22 11:22:54 +07:00
parent c4895d22c7
commit 20ee437e94

View file

@ -3,11 +3,11 @@ package routers
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/fs"
"net/http" "net/http"
"os" "os"
"path" "path"
"recipe-manager/data" "recipe-manager/data"
"recipe-manager/helpers"
"recipe-manager/models" "recipe-manager/models"
"recipe-manager/services/logger" "recipe-manager/services/logger"
"recipe-manager/services/sheet" "recipe-manager/services/sheet"
@ -233,6 +233,18 @@ func (rr *RecipeRouter) Route(r chi.Router) {
Log.Debug("Target => ", zap.Any("target", target_recipe.MachineSetting.ConfigNumber)) Log.Debug("Target => ", zap.Any("target", target_recipe.MachineSetting.ConfigNumber))
// check request structure
// FIXME: Request structure bug. Case-sensitive, likely bug at client
// uncomment the below code to view the bug
// var change_request map[string]interface{}
// err = json.NewDecoder(r.Body).Decode(&change_request)
// if err != nil {
// Log.Error("Decode in request failed: ", zap.Error(err))
// }
// Log.Debug("Request => ", zap.Any("request", change_request))
// Body // Body
var changes models.Recipe01 var changes models.Recipe01
err = json.NewDecoder(r.Body).Decode(&changes) err = json.NewDecoder(r.Body).Decode(&changes)
@ -249,7 +261,14 @@ func (rr *RecipeRouter) Route(r chi.Router) {
// Find changes // Find changes
for key, val := range menu_map { for key, val := range menu_map {
if val != change_map[key] {
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] menu_map[key] = change_map[key]
} }
} }
@ -258,15 +277,24 @@ func (rr *RecipeRouter) Route(r chi.Router) {
tempRecipe := models.Recipe01{} tempRecipe := models.Recipe01{}
tempRecipe = tempRecipe.FromMap(menu_map) tempRecipe = tempRecipe.FromMap(menu_map)
rr.data.SetValuesToRecipe(tempRecipe) rr.data.SetValuesToRecipe(tempRecipe)
Log.Debug("ApplyChange", zap.Any("status", "passed"))
finalData := rr.data.ExportToJSON() // check if changed
temp_finalData, err := json.MarshalIndent(finalData, "", " ") // Log.Debug("Check if changed", zap.Any("result", rr.data.GetRecipe01ByProductCode(changes.ProductCode)))
file, _ := os.Create(path.Join("./cofffeemachineConfig", countryID[:3], filename))
if err != nil { if err != nil {
Log.Error("Error when indent marshal recipe", zap.Error(err)) Log.Error("Error when tried to create file", zap.Error(err))
return return
} }
os.WriteFile(path.Join("cofffeemachineConfig", countryID, filename), temp_finalData, fs.FileMode(0666)) encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(rr.data.GetCurrentRecipe())
if err != nil {
Log.Error("Error when write file", zap.Error(err))
}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{ json.NewEncoder(w).Encode(map[string]interface{}{