update test get recipe metadatas

This commit is contained in:
Kenta420 2023-10-05 09:39:25 +07:00
parent 41812591b7
commit 7450796a6b
4 changed files with 95 additions and 37 deletions

View file

@ -73,22 +73,42 @@ func (rr *RecipeRouter) Route(r chi.Router) {
})
})
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
r.Get("/{product_code}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64)
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
}
productCode := chi.URLParam(r, "product_code")
recipe := rr.data.GetRecipe01()
recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
recipeResult := models.Recipe01{}
recipeMetaDataResult := map[string]string{}
for _, v := range recipe {
if uint64(v.ID) == id {
json.NewEncoder(w).Encode(v)
return
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
}
}
json.NewEncoder(w).Encode(map[string]interface{}{
"recipe": recipeResult,
"recipeMetaData": recipeMetaDataResult,
})
http.Error(w, "Recipe not found", http.StatusNotFound)
})
@ -110,12 +130,12 @@ func (rr *RecipeRouter) Route(r chi.Router) {
for _, v := range result {
mapResult = append(mapResult, map[string]string{
"product_code": v[0].(string),
"name": v[1].(string),
"other_name": v[2].(string),
"description": v[3].(string),
"other_description": v[4].(string),
"image": v[5].(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)