add topping recipe display & fix bugs

This commit is contained in:
pakintada@gmail.com 2024-02-07 15:39:19 +07:00
parent 7d6988f581
commit ece4cef205
8 changed files with 421 additions and 33 deletions

View file

@ -499,6 +499,85 @@ func (d *Data) SetValuesToRecipe(base_recipe []models.Recipe01, recipe models.Re
}
}
func (d *Data) SetValuesToMaterialSetting(base_mat_setting []models.MaterialSetting, updated_mat_setting models.MaterialSetting) {
not_found := false
global_idx := 0
for index, v := range base_mat_setting {
// find matched id
if v.ID == updated_mat_setting.ID {
// change only changed values
for k, v := range updated_mat_setting.ToMap() {
if !reflect.DeepEqual(base_mat_setting[index].ToMap()[k], v) {
d.taoLogger.Log.Debug("SetValuesToMaterialSetting", zap.Any("key", k), zap.Any("old", base_mat_setting[index].ToMap()[k]), zap.Any("new", v))
base_mat_setting[index].ToMap()[k] = v
}
}
} else {
not_found = true
global_idx = index
}
}
// is new value
if not_found {
base_mat_setting[global_idx+1] = updated_mat_setting
}
}
func (d *Data) SetValuesToToppingList(base_topping_list []models.ToppingList, updated_topping_list models.ToppingList) {
not_found := false
global_idx := 0
for index, v := range base_topping_list {
// find matched id
if v.ID == updated_topping_list.ID {
// change only changed values
for k, v := range updated_topping_list.ToMap() {
if !reflect.DeepEqual(base_topping_list[index].ToMap()[k], v) {
d.taoLogger.Log.Debug("SetValuesToToppingList", zap.Any("key", k), zap.Any("old", base_topping_list[index].ToMap()[k]), zap.Any("new", v))
base_topping_list[index].ToMap()[k] = v
}
}
} else {
not_found = true
global_idx = index
}
}
// is new value
if not_found {
base_topping_list[global_idx+1] = updated_topping_list
}
}
func (d *Data) SetValuesToToppingGroupList(base_topping_group_list []models.ToppingGroup, updated_topping_group_list models.ToppingGroup) {
not_found := false
global_idx := 0
for index, v := range base_topping_group_list {
// find matched id
if v.GroupID == updated_topping_group_list.GroupID {
// change only changed values
for k, v := range updated_topping_group_list.ToMap() {
if !reflect.DeepEqual(base_topping_group_list[index].ToMap()[k], v) {
d.taoLogger.Log.Debug("SetValuesToToppingGroup", zap.Any("key", k), zap.Any("old", base_topping_group_list[index].ToMap()[k]), zap.Any("new", v))
base_topping_group_list[index].ToMap()[k] = v
}
}
} else {
not_found = true
global_idx = index
}
}
// is new value
if not_found {
base_topping_group_list[global_idx+1] = updated_topping_group_list
}
}
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
result := make([]models.MaterialSetting, 0)

View file

@ -57,6 +57,19 @@ type MaterialSetting struct {
RawMaterialUnit string `json:"RawMaterialUnit"`
}
func (r *MaterialSetting) ToMap() map[string]interface{} {
var m map[string]interface{}
recipeRecord, _ := json.Marshal(r)
json.Unmarshal(recipeRecord, &m)
return m
}
func (r *MaterialSetting) FromMap(m MaterialSetting) MaterialSetting {
recipeRecord, _ := json.Marshal(m)
json.Unmarshal(recipeRecord, &r)
return *r
}
type Recipe01 struct {
Description string `json:"Description"`
ExtendID int `json:"ExtendID"`
@ -140,6 +153,19 @@ type ToppingGroup struct {
OtherName string `json:"otherName"`
}
func (r *ToppingGroup) ToMap() map[string]interface{} {
var m map[string]interface{}
recipeRecord, _ := json.Marshal(r)
json.Unmarshal(recipeRecord, &m)
return m
}
func (r *ToppingGroup) FromMap(m map[string]interface{}) ToppingGroup {
recipeRecord, _ := json.Marshal(m)
json.Unmarshal(recipeRecord, &r)
return *r
}
type ToppingList struct {
ExtendID int `json:"ExtendID"`
OnTOP bool `json:"OnTOP"`
@ -163,3 +189,16 @@ type ToppingList struct {
UseGram bool `json:"useGram"`
Weight_float int `json:"weight_float"`
}
func (r *ToppingList) ToMap() map[string]interface{} {
var m map[string]interface{}
recipeRecord, _ := json.Marshal(r)
json.Unmarshal(recipeRecord, &m)
return m
}
func (r *ToppingList) FromMap(m map[string]interface{}) ToppingList {
recipeRecord, _ := json.Marshal(m)
json.Unmarshal(recipeRecord, &r)
return *r
}

View file

@ -504,6 +504,34 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
})
}
func (rr *RecipeRouter) updateMaterialSetting(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")
rr.taoLogger.Log.Debug("RecipeRouter.updateMaterialSetting", zap.Any("country", country), zap.Any("filename", filename))
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
updateMutex.Lock()
defer updateMutex.Unlock()
// get material setting
materialSetting := rr.data.GetMaterialSetting(countryID, filename)
if len(materialSetting) == 0 {
http.Error(w, fmt.Sprintf("Recipe country: %s file: %s found empty settings.", country, filename), http.StatusNotFound)
return
}
// TODO: create commit and set change
}
func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request) {
file_version := chi.URLParam(r, "filename_version_only")