add topping WIP
This commit is contained in:
parent
cabfcdee15
commit
16e0e4f9d8
11 changed files with 460 additions and 26 deletions
|
|
@ -267,10 +267,10 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
// , d.CurrentFile, countryID, "result by country id", len(d.currentRecipe[countryID].Recipe01)
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadCurrent::filename", filename)
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadCurrent::countryID", countryID)
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile)
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
|
||||
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::filename", filename)
|
||||
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::countryID", countryID)
|
||||
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile)
|
||||
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
|
||||
|
||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
|
|
@ -283,9 +283,9 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("No result in current recipe", countryID)
|
||||
// fmt.Println("No result in current recipe", countryID)
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile, recipe.Recipe[countryID], "countryID=", countryID)
|
||||
// fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile, recipe.Recipe[countryID], "countryID=", countryID)
|
||||
for _, v := range recipe.Recipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
|
||||
|
|
@ -499,6 +499,100 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
return recipe.MaterialSetting
|
||||
}
|
||||
|
||||
func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingGroup {
|
||||
if countryID == "" {
|
||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
||||
}
|
||||
|
||||
if !strings.Contains(filename, ".tmp") {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
||||
}
|
||||
if _, ok := d.recipeMap[countryID]; ok {
|
||||
d.CurrentFile[countryID] = filename
|
||||
|
||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
||||
}
|
||||
}
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
if err != nil {
|
||||
return d.currentRecipe[countryID].Topping.ToppingGroup
|
||||
}
|
||||
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
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(),
|
||||
}
|
||||
|
||||
return recipe.Topping.ToppingGroup
|
||||
}
|
||||
|
||||
func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList {
|
||||
// do return default
|
||||
if countryID == "" {
|
||||
return d.currentRecipe[countryID].Topping.ToppingList
|
||||
}
|
||||
|
||||
// handle temporary file
|
||||
if !strings.Contains(filename, ".tmp") {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe[countryID].Topping.ToppingList
|
||||
}
|
||||
if _, ok := d.recipeMap[countryID]; ok {
|
||||
d.CurrentFile[countryID] = filename
|
||||
return d.currentRecipe[countryID].Topping.ToppingList
|
||||
}
|
||||
}
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
if err != nil {
|
||||
return d.currentRecipe[countryID].Topping.ToppingList
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
|
||||
return recipe.Topping.ToppingList
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
|
||||
var result []models.MaterialCode
|
||||
|
||||
|
|
|
|||
69
server/routers/topping.go
Normal file
69
server/routers/topping.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/services/logger"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// this does different from normal topping fetcher in recipe
|
||||
|
||||
type ToppingRouter struct {
|
||||
data *data.Data
|
||||
taoLogger *logger.TaoLogger
|
||||
}
|
||||
|
||||
func NewToppingRouter(data *data.Data, taoLogger *logger.TaoLogger) *ToppingRouter {
|
||||
return &ToppingRouter{
|
||||
data: data,
|
||||
taoLogger: taoLogger,
|
||||
}
|
||||
}
|
||||
|
||||
func (tr *ToppingRouter) Route(r chi.Router) {
|
||||
r.Route("/toppings", func(r chi.Router) {
|
||||
r.Get("/groups/{country}/{filename}", tr.GetToppingGroups)
|
||||
r.Get("/lists/{country}/{filename}", tr.GetToppingLists)
|
||||
})
|
||||
}
|
||||
|
||||
// get all topping group
|
||||
func (tr *ToppingRouter) GetToppingGroups(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
// just in case: params country and filename
|
||||
country := chi.URLParam(r, "country")
|
||||
filename := chi.URLParam(r, "filename")
|
||||
|
||||
// get topping groups
|
||||
toppingGroups := tr.data.GetAllToppingGroups(country, filename)
|
||||
|
||||
// send without new mapping
|
||||
|
||||
if err := json.NewEncoder(w).Encode(toppingGroups); err != nil {
|
||||
tr.taoLogger.Log.Error("ToppingRouter.GetToppingGroups", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (tr *ToppingRouter) GetToppingLists(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
country := chi.URLParam(r, "country")
|
||||
filename := chi.URLParam(r, "filename")
|
||||
|
||||
// get toppping list
|
||||
toppingLists := tr.data.GetToppingsList(country, filename)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(toppingLists); err != nil {
|
||||
tr.taoLogger.Log.Debug("ToppingRouter.GetToppingLists", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -151,6 +151,10 @@ func (s *Server) createHandler() {
|
|||
ur := routers.NewUserRouter(s.taoLogger, userService)
|
||||
ur.Route(r)
|
||||
|
||||
// Topping Router
|
||||
tr := routers.NewToppingRouter(s.data, s.taoLogger)
|
||||
tr.Route(r)
|
||||
|
||||
})
|
||||
|
||||
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -197,6 +197,8 @@ func (rs *recipeService) GetRecipeDashboard(request *contracts.RecipeDashboardRe
|
|||
}
|
||||
}
|
||||
|
||||
rs.taoLogger.Log.Debug("RecipeDashboard", zap.String("Timestamp", recipe.Timestamp))
|
||||
|
||||
result := contracts.RecipeDashboardResponse{
|
||||
ConfigNumber: recipe.MachineSetting.ConfigNumber,
|
||||
LastUpdated: recipe.Timestamp,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue