Add material code and settings
This commit is contained in:
parent
36be0426f6
commit
498bcf1c24
9 changed files with 279 additions and 60 deletions
|
|
@ -89,3 +89,49 @@ func (d *Data) GetRecipe(version string) models.Recipe {
|
|||
func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
return d.recipe.Recipe01
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
|
||||
result := make([]models.MaterialSetting, 0)
|
||||
|
||||
if version == "" || version == d.CurrentVersion {
|
||||
copy(result, d.recipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
d.CurrentVersion = version
|
||||
d.recipe = readFile(version)
|
||||
copy(result, d.recipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCode {
|
||||
var result []models.MaterialCode
|
||||
|
||||
if version == "" || version == d.CurrentVersion {
|
||||
result = d.recipe.MaterialCode
|
||||
} else {
|
||||
d.CurrentVersion = version
|
||||
d.recipe = readFile(version)
|
||||
result = d.recipe.MaterialCode
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
resultFilter := make([]models.MaterialCode, len(ids))
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, m := range result {
|
||||
if m.MaterialID == id {
|
||||
resultFilter = append(resultFilter, m)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resultFilter
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
package models
|
||||
|
||||
type Recipe struct {
|
||||
Timestamp string `json:"Timestamp"`
|
||||
MachineSetting MatchineSetting `json:"MachineSetting"`
|
||||
Recipe01 []Recipe01 `json:"Recipe01"`
|
||||
Topping Topping `json:"Topping"`
|
||||
MaterailCode []MaterailCode `json:"MaterailCode"`
|
||||
Timestamp string `json:"Timestamp"`
|
||||
MachineSetting MachineSetting `json:"MachineSetting"`
|
||||
Recipe01 []Recipe01 `json:"Recipe01"`
|
||||
Topping Topping `json:"Topping"`
|
||||
MaterialCode []MaterialCode `json:"MaterialCode"`
|
||||
MaterialSetting []MaterialSetting `json:"MaterialSetting"`
|
||||
}
|
||||
|
||||
type MatchineSetting struct {
|
||||
type MachineSetting struct {
|
||||
RecipeTag string `json:"RecipeTag"`
|
||||
StrTextShowError []string `json:"strTextShowError"`
|
||||
ConfigNumber int `json:"configNumber"`
|
||||
|
|
@ -16,10 +17,10 @@ type MatchineSetting struct {
|
|||
TemperatureMin int `json:"temperatureMin"`
|
||||
}
|
||||
|
||||
type MaterailCode struct {
|
||||
type MaterialCode struct {
|
||||
PackageDescription string `json:"PackageDescription"`
|
||||
RefillValuePerStep int `json:"RefillValuePerStep"`
|
||||
MaterialID int `json:"materialID"`
|
||||
MaterialID uint64 `json:"materialID"`
|
||||
MaterialCode string `json:"materialCode"`
|
||||
}
|
||||
|
||||
|
|
@ -40,7 +41,7 @@ type MaterialSetting struct {
|
|||
SodaChannel bool `json:"SodaChannel"`
|
||||
StockAdjust int `json:"StockAdjust"`
|
||||
SyrupChannel bool `json:"SyrupChannel"`
|
||||
ID int `json:"id"`
|
||||
ID uint64 `json:"id"`
|
||||
IDAlternate int `json:"idAlternate"`
|
||||
IsUse bool `json:"isUse"`
|
||||
PayRettryMaxCount int `json:"pay_rettry_max_count"`
|
||||
|
|
|
|||
76
server/routers/material.go
Normal file
76
server/routers/material.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type MaterialRouter struct {
|
||||
data *data.Data
|
||||
}
|
||||
|
||||
func NewMaterialRouter(data *data.Data) *MaterialRouter {
|
||||
return &MaterialRouter{
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (mr *MaterialRouter) Route(r chi.Router) {
|
||||
r.Route("/materials", func(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")
|
||||
|
||||
matIDs := r.URL.Query().Get("mat_ids")
|
||||
|
||||
var matIDsUint []uint64
|
||||
for _, v := range strings.Split(matIDs, ",") {
|
||||
matIDUint, err := strconv.ParseUint(v, 10, 64)
|
||||
|
||||
if err != nil || matIDUint == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
matIDsUint = append(matIDsUint, matIDUint)
|
||||
}
|
||||
|
||||
material := mr.data.GetMaterialCode(matIDsUint, version)
|
||||
|
||||
json.NewEncoder(w).Encode(material)
|
||||
})
|
||||
|
||||
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")
|
||||
|
||||
material := mr.data.GetMaterialSetting(version)
|
||||
|
||||
matID := chi.URLParam(r, "mat_id")
|
||||
|
||||
matIDuint, err := strconv.ParseUint(matID, 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid material id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var matSetting models.MaterialSetting
|
||||
|
||||
for _, mat := range material {
|
||||
if mat.ID == matIDuint {
|
||||
matSetting = mat
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(matSetting)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
@ -437,7 +437,6 @@ func (s *Server) createHandler() {
|
|||
Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI))
|
||||
})
|
||||
|
||||
// Recipe Router
|
||||
sheetService, err := sheet.NewSheetService(context.Background(), s.cfg)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -445,9 +444,14 @@ func (s *Server) createHandler() {
|
|||
return
|
||||
}
|
||||
|
||||
// Recipe Router
|
||||
rr := routers.NewRecipeRouter(database, sheetService)
|
||||
rr.Route(r)
|
||||
|
||||
// Material Router
|
||||
mr := routers.NewMaterialRouter(database)
|
||||
mr.Route(r)
|
||||
|
||||
})
|
||||
|
||||
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue