Add material code and settings
This commit is contained in:
parent
36be0426f6
commit
498bcf1c24
9 changed files with 279 additions and 60 deletions
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)
|
||||
})
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue