Taobin-Recipe-Manager/server/routers/material.go

172 lines
4.8 KiB
Go
Raw Normal View History

2023-10-06 15:33:10 +07:00
package routers
import (
"encoding/json"
"net/http"
"recipe-manager/data"
"recipe-manager/models"
2023-12-07 09:37:18 +07:00
"recipe-manager/services/logger"
2023-10-06 15:33:10 +07:00
"strconv"
"strings"
2023-12-29 16:10:57 +07:00
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
2023-10-06 15:33:10 +07:00
)
type MaterialRouter struct {
2023-12-07 09:37:18 +07:00
data *data.Data
taoLogger *logger.TaoLogger
2023-10-06 15:33:10 +07:00
}
2023-12-07 09:37:18 +07:00
func NewMaterialRouter(data *data.Data, taoLogger *logger.TaoLogger) *MaterialRouter {
2023-10-06 15:33:10 +07:00
return &MaterialRouter{
2023-12-07 09:37:18 +07:00
data: data,
taoLogger: taoLogger,
2023-10-06 15:33:10 +07:00
}
}
func (mr *MaterialRouter) Route(r chi.Router) {
r.Route("/materials", func(r chi.Router) {
2023-12-07 09:37:18 +07:00
r.Get("/code", mr.getMaterialCode)
r.Get("/setting/{mat_id}", mr.getMaterialSettingByMatID)
2023-12-29 16:10:57 +07:00
r.Get("/full/{country}/{filename}", mr.GetFullMaterialDetail)
2023-12-07 09:37:18 +07:00
})
}
2023-10-06 15:33:10 +07:00
2023-12-29 16:10:57 +07:00
func (mr *MaterialRouter) GetFullMaterialDetail(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
country := chi.URLParam(r, "country")
filename := chi.URLParam(r, "filename")
mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("country", country), zap.Any("filename", filename))
// get material setting and code
matSettings := mr.data.GetMaterialSetting(country, filename)
matCodes := mr.data.GetRecipe(country, filename).MaterialCode
2024-02-05 17:07:04 +07:00
if len(matSettings) == 0 {
// mr.taoLogger.Log.Error("MaterialRouter.GetFullMaterialDetail", zap.Error(err))
http.Error(w, "Material not found", http.StatusNotFound)
return
}
2023-12-29 16:10:57 +07:00
// combine
materialDetails := []map[string]interface{}{}
for _, matSetting := range matSettings {
// if material name exist
mat_name := ""
if matSetting.MaterialName != "" {
mat_name = matSetting.MaterialName
// mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("mat_name", mat_name))
} else {
for _, matCode := range matCodes {
if matCode.MaterialID == matSetting.ID {
mat_name = matCode.PackageDescription
break
}
}
}
materialDetails = append(materialDetails, map[string]interface{}{
"materialId": matSetting.ID,
"name": mat_name,
2024-01-23 13:46:37 +07:00
"nameEN": matSetting.MaterialOtherName,
"type": "powder:" + strconv.FormatBool(matSetting.PowderChannel) + ",syrup:" + strconv.FormatBool(matSetting.SyrupChannel) + ",bean:" + strconv.FormatBool(matSetting.BeanChannel) + ",equipment:" + strconv.FormatBool(matSetting.IsEquipment) + ",soda:" + strconv.FormatBool(matSetting.SodaChannel) + ",icecream:" + strconv.FormatBool(matSetting.IceScreamBingsuChannel),
2023-12-29 16:10:57 +07:00
})
}
2024-01-18 16:59:06 +07:00
mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("materialDetails", materialDetails[0]))
2023-12-29 16:10:57 +07:00
// send result
if err := json.NewEncoder(w).Encode(materialDetails); err != nil {
mr.taoLogger.Log.Error("MaterialRouter.GetFullMaterialDetail", zap.Error(err))
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
}
2023-12-07 09:37:18 +07:00
func (mr *MaterialRouter) getMaterialCode(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
filename := r.URL.Query().Get("filename")
country := r.URL.Query().Get("country")
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
matIDs := r.URL.Query().Get("mat_ids")
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
var matIDsUint []uint64
for _, v := range strings.Split(matIDs, ",") {
matIDUint, err := strconv.ParseUint(v, 10, 64)
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
if err != nil || matIDUint == 0 {
continue
}
matIDsUint = append(matIDsUint, matIDUint)
}
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
countryID, err := mr.data.GetCountryIDByName(country)
2023-10-24 18:01:52 +07:00
2023-12-07 09:37:18 +07:00
if err != nil {
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialCode", zap.Error(err))
http.Error(w, "Country not found", http.StatusNotFound)
return
}
2023-10-24 18:01:52 +07:00
2023-12-07 09:37:18 +07:00
material := mr.data.GetMaterialCode(matIDsUint, countryID, filename)
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
if err := json.NewEncoder(w).Encode(material); err != nil {
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialCode", zap.Error(err))
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
}
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
func (mr *MaterialRouter) getMaterialSettingByMatID(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
filename := r.URL.Query().Get("filename")
country := r.URL.Query().Get("country")
2023-10-24 18:01:52 +07:00
2023-12-07 09:37:18 +07:00
countryID, err := mr.data.GetCountryIDByName(country)
2023-10-24 18:01:52 +07:00
2023-12-07 09:37:18 +07:00
if err != nil {
2024-01-19 14:59:21 +07:00
// mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
2023-12-07 09:37:18 +07:00
http.Error(w, "Country not found", http.StatusNotFound)
return
}
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
material := mr.data.GetMaterialSetting(countryID, filename)
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
matID := chi.URLParam(r, "mat_id")
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
matIDuint, err := strconv.ParseUint(matID, 10, 64)
if err != nil {
2024-01-19 14:59:21 +07:00
// mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
2023-12-07 09:37:18 +07:00
http.Error(w, "Invalid material id", http.StatusBadRequest)
return
}
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
var matSetting models.MaterialSetting
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
for _, mat := range material {
if mat.ID == matIDuint {
matSetting = mat
break
}
}
2023-10-06 15:33:10 +07:00
2023-12-07 09:37:18 +07:00
if err := json.NewEncoder(w).Encode(matSetting); err != nil {
2024-01-19 14:59:21 +07:00
// mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
2023-12-07 09:37:18 +07:00
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
2023-10-06 15:33:10 +07:00
}