update recipe detail and recipe detail list
This commit is contained in:
parent
8b45ed53ee
commit
d52cad09fd
16 changed files with 947 additions and 458 deletions
|
|
@ -69,20 +69,20 @@ func NewData() *Data {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe(countryID, filename string) models.Recipe {
|
||||
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
||||
|
||||
if countryID == "" {
|
||||
return *d.currentRecipe
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
return *d.currentRecipe
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return recipe.Recipe
|
||||
return &recipe.Recipe
|
||||
}
|
||||
|
||||
// change current version and read new recipe
|
||||
|
|
@ -92,7 +92,7 @@ func (d *Data) GetRecipe(countryID, filename string) models.Recipe {
|
|||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file", zap.Error(err))
|
||||
return *d.currentRecipe
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
|
@ -116,23 +116,70 @@ func (d *Data) GetRecipe(countryID, filename string) models.Recipe {
|
|||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
return *d.currentRecipe
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
return d.currentRecipe.Recipe01
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01ByProductCode(code string) models.Recipe01 {
|
||||
result := make([]models.Recipe01, 0)
|
||||
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
|
||||
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == code {
|
||||
result = append(result, v)
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
for _, v := range recipe.Recipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result[0]
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file", zap.Error(err))
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode)
|
||||
}
|
||||
|
||||
func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue