update recipe endpoint
This commit is contained in:
parent
9e8efa4cfc
commit
e4fdf19c67
5 changed files with 76 additions and 14 deletions
|
|
@ -2,12 +2,12 @@ package routers
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/models"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
|
@ -35,8 +35,20 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
take = newTake
|
||||
}
|
||||
|
||||
// copy the strcut to avoid modifying the original
|
||||
recipe := rr.data.GetRecipe()
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isHasMore := len(recipe.Recipe01) >= int(take+offset)
|
||||
if isHasMore {
|
||||
recipe.Recipe01 = recipe.Recipe01[offset : take+offset]
|
||||
|
|
@ -44,7 +56,6 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
return recipe.Recipe01[i].ID < recipe.Recipe01[j].ID
|
||||
})
|
||||
} else if len(recipe.Recipe01) > int(offset) {
|
||||
log.Println("offset", offset, "len", len(recipe.Recipe01))
|
||||
recipe.Recipe01 = recipe.Recipe01[offset:]
|
||||
} else {
|
||||
recipe.Recipe01 = []models.Recipe01{}
|
||||
|
|
@ -55,5 +66,10 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
"hasMore": isHasMore,
|
||||
})
|
||||
})
|
||||
|
||||
r.Get("/json", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(rr.data.GetRecipe())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue