add filter with material

This commit is contained in:
Kenta420 2023-10-27 16:13:04 +07:00
parent 5b01f1c431
commit 70cfd89fc4
9 changed files with 135 additions and 14 deletions

View file

@ -50,6 +50,16 @@ 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")
var materialIdsUint []uint64
for _, v := range strings.Split(materialIds, ",") {
materialIdUint, err := strconv.ParseUint(v, 10, 64)
if err != nil || materialIdUint == 0 {
continue
}
materialIdsUint = append(materialIdsUint, materialIdUint)
}
countryID, err := rr.data.GetCountryIDByName(country)
@ -72,6 +82,20 @@ func (rr *RecipeRouter) Route(r chi.Router) {
}
}
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]