fix edit file, savable now, req fix bug
This commit is contained in:
parent
c4895d22c7
commit
20ee437e94
1 changed files with 34 additions and 6 deletions
|
|
@ -3,11 +3,11 @@ package routers
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/helpers"
|
||||
"recipe-manager/models"
|
||||
"recipe-manager/services/logger"
|
||||
"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))
|
||||
|
||||
// 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
|
||||
var changes models.Recipe01
|
||||
err = json.NewDecoder(r.Body).Decode(&changes)
|
||||
|
|
@ -249,7 +261,14 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
|
||||
// Find changes
|
||||
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]
|
||||
}
|
||||
}
|
||||
|
|
@ -258,15 +277,24 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
tempRecipe := models.Recipe01{}
|
||||
tempRecipe = tempRecipe.FromMap(menu_map)
|
||||
rr.data.SetValuesToRecipe(tempRecipe)
|
||||
Log.Debug("ApplyChange", zap.Any("status", "passed"))
|
||||
|
||||
finalData := rr.data.ExportToJSON()
|
||||
temp_finalData, err := json.MarshalIndent(finalData, "", " ")
|
||||
// check if changed
|
||||
// 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 {
|
||||
Log.Error("Error when indent marshal recipe", zap.Error(err))
|
||||
Log.Error("Error when tried to create file", zap.Error(err))
|
||||
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")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue