This commit is contained in:
pakintada@gmail.com 2023-12-07 11:09:20 +07:00
commit 96f341aa28
26 changed files with 1068 additions and 664 deletions

View file

@ -1,7 +1,6 @@
package data
import (
"encoding/json"
"fmt"
"log"
"recipe-manager/helpers"
@ -12,10 +11,6 @@ import (
"go.uber.org/zap"
)
var (
Log = logger.GetInstance()
)
type RecipeWithTimeStamps struct {
Recipe models.Recipe
TimeStamps int64
@ -28,9 +23,10 @@ type Data struct {
currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
taoLogger *logger.TaoLogger
}
func NewData() *Data {
func NewData(taoLogger *logger.TaoLogger) *Data {
countries := []helpers.CountryName{{
CountryID: "tha",
@ -66,6 +62,7 @@ func NewData() *Data {
},
},
Countries: countries,
taoLogger: taoLogger,
}
}
@ -91,7 +88,7 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetRecipe]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe
}
@ -148,7 +145,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetRecipe01ByProductCode]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
for _, v := range d.currentRecipe.Recipe01 {
if v.ProductCode == productCode {
return v, nil
@ -222,7 +219,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetMaterialSetting]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
copy(result, d.currentRecipe.MaterialSetting)
return result
}
@ -266,7 +263,7 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetMaterialCode]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe.MaterialCode
}
@ -332,13 +329,3 @@ func (d *Data) GetCountryIDByName(countryName string) (string, error) {
}
return "", fmt.Errorf("country name: %s not found", countryName)
}
func (d *Data) ExportToJSON() []byte {
b_recipe, err := json.Marshal(d.currentRecipe)
if err != nil {
Log.Error("Error when marshal recipe", zap.Error(err))
return nil
}
return b_recipe
}