Add User route and Refactor code

This commit is contained in:
Kenta420 2023-12-06 20:21:25 +07:00
parent 519749fd3a
commit b311a41dc7
24 changed files with 902 additions and 489 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", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe
}
@ -144,7 +141,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", 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
@ -216,7 +213,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", 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
}
@ -260,7 +257,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", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe.MaterialCode
}
@ -326,13 +323,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
}