update recipe detail and recipe detail list

This commit is contained in:
Kenta420 2023-11-24 17:47:44 +07:00
parent 8b45ed53ee
commit d52cad09fd
16 changed files with 947 additions and 458 deletions

View file

@ -7,11 +7,12 @@ import (
"net/http"
"os"
"path"
"recipe-manager/contracts"
"recipe-manager/data"
"recipe-manager/models"
"recipe-manager/services/logger"
"recipe-manager/services/recipe"
"recipe-manager/services/sheet"
"sort"
"strconv"
"strings"
@ -20,24 +21,45 @@ import (
)
type RecipeRouter struct {
data *data.Data
sheetService sheet.SheetService
data *data.Data
sheetService sheet.SheetService
recipeService recipe.RecipeService
}
var (
Log = logger.GetInstance()
)
func NewRecipeRouter(data *data.Data, sheetService sheet.SheetService) *RecipeRouter {
func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService) *RecipeRouter {
return &RecipeRouter{
data: data,
sheetService: sheetService,
data: data,
recipeService: recipeService,
sheetService: sheetService,
}
}
func (rr *RecipeRouter) Route(r chi.Router) {
r.Route("/recipes", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
r.Get("/dashboard", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
country := r.URL.Query().Get("country")
filename := r.URL.Query().Get("filename")
result, err := rr.recipeService.GetRecipeDashboard(&contracts.RecipeDashboardRequest{
Country: country,
Filename: filename,
})
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(result)
})
r.Get("/overview", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
var take, offset uint64 = 10, 0
if newOffset, err := strconv.ParseUint(r.URL.Query().Get("offset"), 10, 64); err == nil {
@ -50,111 +72,105 @@ func (rr *RecipeRouter) Route(r chi.Router) {
country := r.URL.Query().Get("country")
filename := r.URL.Query().Get("filename")
materialIds := r.URL.Query().Get("material_ids")
materialIds := r.URL.Query().Get("materialIds")
var materialIdsUint []uint64
var materialIdsUint []int
for _, v := range strings.Split(materialIds, ",") {
materialIdUint, err := strconv.ParseUint(v, 10, 64)
if err != nil || materialIdUint == 0 {
continue
}
materialIdsUint = append(materialIdsUint, materialIdUint)
materialIdsUint = append(materialIdsUint, int(materialIdUint))
}
countryID, err := rr.data.GetCountryIDByName(country)
result, err := rr.recipeService.GetRecipeOverview(&contracts.RecipeOverviewRequest{
Take: int(take),
Skip: int(offset),
Search: r.URL.Query().Get("search"),
Country: country,
Filename: filename,
MatIds: materialIdsUint,
})
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
recipe := rr.data.GetRecipe(countryID, filename)
searchQuery := r.URL.Query().Get("search")
if searchQuery != "" {
recipe.Recipe01 = []models.Recipe01{}
for _, v := range rr.data.GetRecipe01() {
if strings.Contains(strings.ToLower(v.ProductCode), strings.ToLower(searchQuery)) ||
strings.Contains(strings.ToLower(v.Name), strings.ToLower(searchQuery)) ||
strings.Contains(strings.ToLower(v.OtherName), strings.ToLower(searchQuery)) {
recipe.Recipe01 = append(recipe.Recipe01, v)
}
}
}
if len(materialIdsUint) > 0 {
resultFilter := []models.Recipe01{}
for _, v := range recipe.Recipe01 {
for _, matID := range materialIdsUint {
for _, recipe := range v.Recipes {
if recipe.IsUse && uint64(recipe.MaterialPathId) == matID {
resultFilter = append(resultFilter, v)
}
}
}
}
recipe.Recipe01 = resultFilter
}
isHasMore := len(recipe.Recipe01) >= int(take+offset)
if isHasMore {
recipe.Recipe01 = recipe.Recipe01[offset : take+offset]
sort.Slice(recipe.Recipe01, func(i, j int) bool {
return recipe.Recipe01[i].ID < recipe.Recipe01[j].ID
})
} else if len(recipe.Recipe01) > int(offset) {
recipe.Recipe01 = recipe.Recipe01[offset:]
} else {
recipe.Recipe01 = []models.Recipe01{}
}
json.NewEncoder(w).Encode(map[string]interface{}{
"fileName": rr.data.CurrentFile,
"recipes": recipe,
"hasMore": isHasMore,
})
json.NewEncoder(w).Encode(result)
})
r.Get("/{product_code}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
productCode := chi.URLParam(r, "product_code")
recipe := rr.data.GetRecipe01()
recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
// recipe := rr.data.GetRecipe01()
// recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
var recipeResult *models.Recipe01
recipeMetaDataResult := map[string]string{}
// var recipeResult *models.Recipe01
// recipeMetaDataResult := map[string]string{}
for _, v := range recipe {
if v.ProductCode == productCode {
recipeResult = &v
break
}
}
// for _, v := range recipe {
// if v.ProductCode == productCode {
// recipeResult = &v
// break
// }
// }
for _, v := range recipeMetaData {
if v[0].(string) == productCode {
recipeMetaDataResult = map[string]string{
"productCode": v[0].(string),
"name": v[1].(string),
"otherName": v[2].(string),
"description": v[3].(string),
"otherDescription": v[4].(string),
"picture": v[5].(string),
}
break
}
}
// for _, v := range recipeMetaData {
// if v[0].(string) == productCode {
// recipeMetaDataResult = map[string]string{
// "productCode": v[0].(string),
// "name": v[1].(string),
// "otherName": v[2].(string),
// "description": v[3].(string),
// "otherDescription": v[4].(string),
// "picture": v[5].(string),
// }
// break
// }
// }
if recipeResult == nil {
http.Error(w, "Not Found", http.StatusNotFound)
// if recipeResult == nil {
// http.Error(w, "Not Found", http.StatusNotFound)
// return
// }
// json.NewEncoder(w).Encode(map[string]interface{}{
// "recipe": recipeResult,
// "recipeMetaData": recipeMetaDataResult,
// })
result, err := rr.recipeService.GetRecipeDetail(&contracts.RecipeDetailRequest{
Filename: r.URL.Query().Get("filename"),
Country: r.URL.Query().Get("country"),
ProductCode: productCode,
})
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"recipe": recipeResult,
"recipeMetaData": recipeMetaDataResult,
json.NewEncoder(w).Encode(result)
})
r.Get("/{product_code}/mat", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
productCode := chi.URLParam(r, "product_code")
result, err := rr.recipeService.GetRecipeDetailMat(&contracts.RecipeDetailRequest{
Filename: r.URL.Query().Get("filename"),
Country: r.URL.Query().Get("country"),
ProductCode: productCode,
})
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(result)
})
r.Get("/{country}/{filename}/json", func(w http.ResponseWriter, r *http.Request) {
@ -242,7 +258,12 @@ func (rr *RecipeRouter) Route(r chi.Router) {
Log.Debug("Changes: ", zap.Any("changes", changes))
// TODO: find the matched pd
target_menu := rr.data.GetRecipe01ByProductCode(changes.ProductCode)
target_menu, err := rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode)
if err != nil {
Log.Error("Error when get recipe by product code", zap.Error(err))
return
}
menu_map := target_menu.ToMap()
change_map := changes.ToMap()