diff --git a/internal/handlers/product.go b/internal/handlers/product.go index 44db980..fc12f01 100644 --- a/internal/handlers/product.go +++ b/internal/handlers/product.go @@ -1,6 +1,7 @@ package handlers import ( + "encoding/json" "net/http" "git.espin.casa/albert/mercadona/internal/client" @@ -9,6 +10,20 @@ import ( func GetMercadonaProduct(client client.IMercadona) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - + id := p.ByName("id") + product, err := client.GetMercadonaProduct(r.Context(), id) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + // marshal product + resp, err := json.Marshal(&product) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + w.WriteHeader(http.StatusOK) + w.Header().Add("application/type", "application/json") + if _, err = w.Write(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } } }