174 lines
4.9 KiB
Go
174 lines
4.9 KiB
Go
package routers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"recipe-manager/data"
|
|
"recipe-manager/models"
|
|
"recipe-manager/services/sheet"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type RecipeRouter struct {
|
|
data *data.Data
|
|
sheetService sheet.SheetService
|
|
}
|
|
|
|
func NewRecipeRouter(data *data.Data, sheetService sheet.SheetService) *RecipeRouter {
|
|
return &RecipeRouter{
|
|
data: data,
|
|
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) {
|
|
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 {
|
|
offset = newOffset
|
|
}
|
|
|
|
if newTake, err := strconv.ParseUint(r.URL.Query().Get("take"), 10, 64); err == nil {
|
|
take = newTake
|
|
}
|
|
|
|
version := r.URL.Query().Get("version")
|
|
|
|
recipe := rr.data.GetRecipe(version)
|
|
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]
|
|
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.CurrentVersion,
|
|
"recipes": recipe,
|
|
"hasMore": isHasMore,
|
|
})
|
|
})
|
|
|
|
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")
|
|
|
|
var recipeResult *models.Recipe01
|
|
recipeMetaDataResult := map[string]string{}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
if recipeResult == nil {
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"recipe": recipeResult,
|
|
"recipeMetaData": recipeMetaDataResult,
|
|
})
|
|
})
|
|
|
|
r.Get("/{version}/json", func(w http.ResponseWriter, r *http.Request) {
|
|
version := chi.URLParam(r, "version")
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(rr.data.GetRecipe(version))
|
|
})
|
|
|
|
r.Get("/versions", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Add("Content-Type", "application/json")
|
|
// get key from map
|
|
keys := []string{}
|
|
for k := range rr.data.AllRecipeFiles {
|
|
countryName, err := rr.data.GetCountryNameByID(k)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
keys = append(keys, countryName)
|
|
}
|
|
json.NewEncoder(w).Encode(keys)
|
|
})
|
|
|
|
r.Get("/versions/{country}", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
countryName := chi.URLParam(r, "country")
|
|
countryID, err := rr.data.GetCountryIDByName(countryName)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", countryName), http.StatusNotFound)
|
|
return
|
|
}
|
|
files := []string{}
|
|
for _, v := range rr.data.AllRecipeFiles[countryID] {
|
|
files = append(files, v.Name)
|
|
}
|
|
json.NewEncoder(w).Encode(files)
|
|
})
|
|
|
|
r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) {
|
|
result := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
|
|
|
|
mapResult := []map[string]string{}
|
|
|
|
for _, v := range result {
|
|
mapResult = append(mapResult, 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),
|
|
})
|
|
}
|
|
json.NewEncoder(w).Encode(mapResult)
|
|
})
|
|
})
|
|
}
|