30 lines
810 B
Go
30 lines
810 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"git.espin.casa/albert/mercadona/internal/client"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|