update getting file recipe

This commit is contained in:
Kenta420 2023-10-24 18:01:52 +07:00
parent ea506b8128
commit 3bfbbd778a
10 changed files with 243 additions and 152 deletions

View file

@ -3,9 +3,6 @@ package data
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"recipe-manager/helpers"
"recipe-manager/models"
"recipe-manager/services/logger"
@ -18,40 +15,18 @@ var (
Log = logger.GetInstance()
)
func readFile(version string) *models.Recipe {
path := filepath.Join("cofffeemachineConfig", version)
file, err := os.Open(path)
if err != nil {
log.Fatalf("Error when open file: %s", err)
return nil
}
defer file.Close()
var data *models.Recipe
err = json.NewDecoder(file).Decode(&data)
if err != nil {
log.Fatalf("Error when decode file: %s", err)
return nil
}
return data
}
type RecipeWithTimeStamps struct {
Recipe models.Recipe
TimeStamps int64
}
type Data struct {
CurrentVersion string
AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
CurrentFile string
CurrentCountryID string
AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
}
func NewData() *Data {
@ -70,15 +45,17 @@ func NewData() *Data {
allRecipeFiles := helpers.ScanRecipeFiles(countries)
defaultVersion := "coffeethai02_580.json"
defaultRecipe := readFile(defaultVersion)
defaultFile := "coffeethai02_580.json"
defaultCountry := "thai"
defaultRecipe := helpers.ReadRecipeFile(defaultCountry, defaultFile)
return &Data{
CurrentVersion: defaultVersion,
AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe,
CurrentFile: defaultFile,
CurrentCountryID: defaultCountry,
AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe,
recipeMap: map[string]RecipeWithTimeStamps{
defaultVersion: {
defaultFile: {
Recipe: *defaultRecipe,
TimeStamps: time.Now().Unix(),
},
@ -87,22 +64,26 @@ func NewData() *Data {
}
}
func (d *Data) GetRecipe(version string) models.Recipe {
func (d *Data) GetRecipe(countryID, filename string) models.Recipe {
if version == "" || version == d.CurrentVersion {
if countryID == "" {
return *d.currentRecipe
}
log.Println("Change recipe to version:", version)
if filename == "" || filename == d.CurrentFile {
return *d.currentRecipe
}
if recipe, ok := d.recipeMap[version]; ok {
d.CurrentVersion = version
if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename
d.CurrentCountryID = countryID
return recipe.Recipe
}
// change current version and read new recipe
d.CurrentVersion = version
d.currentRecipe = readFile(version)
d.CurrentFile = filename
d.CurrentCountryID = countryID
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -118,7 +99,7 @@ func (d *Data) GetRecipe(version string) models.Recipe {
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -151,22 +132,29 @@ func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
}
}
func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
result := make([]models.MaterialSetting, 0)
if version == "" || version == d.CurrentVersion {
if countryID == "" {
copy(result, d.currentRecipe.MaterialSetting)
return result
}
if recipe, ok := d.recipeMap[version]; ok {
copy(result, recipe.Recipe.MaterialSetting)
d.CurrentVersion = version
if filename == "" || filename == d.CurrentFile {
copy(result, d.currentRecipe.MaterialSetting)
return result
}
d.CurrentVersion = version
d.currentRecipe = readFile(version)
if recipe, ok := d.recipeMap[filename]; ok {
copy(result, recipe.Recipe.MaterialSetting)
d.CurrentFile = filename
d.CurrentCountryID = countryID
return result
}
d.CurrentFile = filename
d.CurrentCountryID = countryID
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -182,7 +170,7 @@ func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -191,17 +179,18 @@ func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
return result
}
func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCode {
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
var result []models.MaterialCode
if version == "" || version == d.CurrentVersion {
if filename == "" || filename == d.CurrentFile {
result = d.currentRecipe.MaterialCode
} else if recipe, ok := d.recipeMap[version]; ok {
d.CurrentVersion = version
} else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename
return recipe.Recipe.MaterialCode
} else {
d.CurrentVersion = version
d.currentRecipe = readFile(version)
d.CurrentFile = filename
d.CurrentCountryID = countryID
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -217,7 +206,7 @@ func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCo
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"os"
"path"
"path/filepath"
"recipe-manager/models"
"sort"
@ -28,23 +29,28 @@ func ReadFile(filePath string) (string, error) {
return string(file), nil
}
func ReadRecipeFile(filePath string) models.Recipe {
file, err := os.Open(filePath)
func ReadRecipeFile(countryID, filePath string) *models.Recipe {
if countryID == "thai" {
countryID = ""
}
file, err := os.Open(path.Join("cofffeemachineConfig", countryID, filePath))
if err != nil {
log.Fatalf("Error when open file: %s", err)
return models.Recipe{}
return nil
}
defer file.Close()
var data models.Recipe
var data *models.Recipe
err = json.NewDecoder(file).Decode(&data)
if err != nil {
log.Fatalf("Error when decode file: %s", err)
return models.Recipe{}
return nil
}
return data

View file

@ -57,7 +57,7 @@ type Recipe01 struct {
OnTOP bool `json:"OnTOP"`
LastChange string `json:"LastChange"`
MenuStatus int `json:"MenuStatus"`
RemainingCups string `json:"RemainingCups"`
RemainingCups int `json:"RemainingCups"`
StringParam string `json:"StringParam"`
TextForWarningBeforePay []string `json:"TextForWarningBeforePay"`
CashPrice int `json:"cashPrice"`

View file

@ -26,7 +26,8 @@ func (mr *MaterialRouter) Route(r chi.Router) {
r.Get("/code", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
version := r.URL.Query().Get("version")
filename := r.URL.Query().Get("filename")
country := r.URL.Query().Get("country")
matIDs := r.URL.Query().Get("mat_ids")
@ -41,7 +42,14 @@ func (mr *MaterialRouter) Route(r chi.Router) {
matIDsUint = append(matIDsUint, matIDUint)
}
material := mr.data.GetMaterialCode(matIDsUint, version)
countryID, err := mr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, "Country not found", http.StatusNotFound)
return
}
material := mr.data.GetMaterialCode(matIDsUint, countryID, filename)
json.NewEncoder(w).Encode(material)
})
@ -49,9 +57,17 @@ func (mr *MaterialRouter) Route(r chi.Router) {
r.Get("/setting/{mat_id}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
version := r.URL.Query().Get("version")
filename := r.URL.Query().Get("filename")
country := r.URL.Query().Get("country")
material := mr.data.GetMaterialSetting(version)
countryID, err := mr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, "Country not found", http.StatusNotFound)
return
}
material := mr.data.GetMaterialSetting(countryID, filename)
matID := chi.URLParam(r, "mat_id")

View file

@ -6,6 +6,7 @@ import (
"io/fs"
"net/http"
"os"
"path"
"recipe-manager/data"
"recipe-manager/models"
"recipe-manager/services/logger"
@ -47,9 +48,17 @@ func (rr *RecipeRouter) Route(r chi.Router) {
take = newTake
}
version := r.URL.Query().Get("version")
country := r.URL.Query().Get("country")
filename := r.URL.Query().Get("filename")
recipe := rr.data.GetRecipe(version)
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
recipe := rr.data.GetRecipe(countryID, filename)
searchQuery := r.URL.Query().Get("search")
if searchQuery != "" {
@ -76,7 +85,7 @@ func (rr *RecipeRouter) Route(r chi.Router) {
}
json.NewEncoder(w).Encode(map[string]interface{}{
"fileName": rr.data.CurrentVersion,
"fileName": rr.data.CurrentFile,
"recipes": recipe,
"hasMore": isHasMore,
})
@ -124,11 +133,17 @@ func (rr *RecipeRouter) Route(r chi.Router) {
})
})
r.Get("/{version}/json", func(w http.ResponseWriter, r *http.Request) {
version := chi.URLParam(r, "version")
r.Get("/{country}/{filename}/json", func(w http.ResponseWriter, r *http.Request) {
country := chi.URLParam(r, "country")
filename := chi.URLParam(r, "filename")
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(rr.data.GetRecipe(version))
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(rr.data.GetRecipe(countryID, filename))
})
r.Get("/versions", func(w http.ResponseWriter, r *http.Request) {
@ -179,16 +194,24 @@ func (rr *RecipeRouter) Route(r chi.Router) {
json.NewEncoder(w).Encode(mapResult)
})
r.Post("/edit/{version}", func(w http.ResponseWriter, r *http.Request) {
r.Post("/edit/{country}/{filename}", func(w http.ResponseWriter, r *http.Request) {
Log.Debug("Edit: ", zap.String("path", r.RequestURI))
version := chi.URLParam(r, "version")
target_recipe := rr.data.GetRecipe(version)
filename := chi.URLParam(r, "filename")
country := chi.URLParam(r, "country")
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
target_recipe := rr.data.GetRecipe(countryID, filename)
Log.Debug("Target => ", zap.Any("target", target_recipe.MachineSetting.ConfigNumber))
// Body
var changes models.Recipe01
err := json.NewDecoder(r.Body).Decode(&changes)
err = json.NewDecoder(r.Body).Decode(&changes)
if err != nil {
Log.Error("Decode in request failed: ", zap.Error(err))
}
@ -219,7 +242,7 @@ func (rr *RecipeRouter) Route(r chi.Router) {
return
}
os.WriteFile("./cofffeemachineConfig/coffeethai02_"+version+".json", temp_finalData, fs.FileMode(0666))
os.WriteFile(path.Join("cofffeemachineConfig", countryID, filename), temp_finalData, fs.FileMode(0666))
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{