]> localhost Git - tq-wave11-pf.git/commitdiff
feat: get ingredient by id
authorJansen <[email protected]>
Thu, 7 Aug 2025 20:19:22 +0000 (16:19 -0400)
committerJansen <[email protected]>
Thu, 7 Aug 2025 20:19:22 +0000 (16:19 -0400)
pkg/http/ingredient.go
pkg/http/util/parsers.go
pkg/services/ingredient.go

index 78167cc426460fefedf318d719cbcde89affe84e..49d744ecdb7540519d898c48584c41da7cae4940 100644 (file)
@@ -1,11 +1,8 @@
 package http
 
 import (
-       serrors "errors"
        shttp "net/http"
-       "strconv"
 
-       "jsdaj.tq/pf/internal/errors"
        "jsdaj.tq/pf/pkg/core"
        "jsdaj.tq/pf/pkg/http/util"
 )
@@ -28,9 +25,8 @@ func (s *Server) ingredientHandleCreate(w shttp.ResponseWriter, r *shttp.Request
 }
 
 func (s *Server) ingredientHandleUpdate(w shttp.ResponseWriter, r *shttp.Request) {
-       id, err := strconv.Atoi(r.PathValue("id"))
+       id, err := util.ParseParamIdAndErr(w, r)
        if err != nil {
-               util.HandleServiceError(w, errors.MapError(serrors.New("bad_id")))
                return
        }
 
@@ -53,9 +49,8 @@ func (s *Server) ingredientHandleUpdate(w shttp.ResponseWriter, r *shttp.Request
 }
 
 func (s *Server) ingredientHandleDelete(w shttp.ResponseWriter, r *shttp.Request) {
-       id, err := strconv.Atoi(r.PathValue("id"))
+       id, err := util.ParseParamIdAndErr(w, r)
        if err != nil {
-               util.HandleServiceError(w, errors.MapError(serrors.New("bad_id")))
                return
        }
 
@@ -68,7 +63,20 @@ func (s *Server) ingredientHandleDelete(w shttp.ResponseWriter, r *shttp.Request
        util.ParseAndWriteResponse(w, struct{}{}, shttp.StatusNoContent)
 }
 
-func (s *Server) ingredientHandleGet(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) ingredientHandleGet(w shttp.ResponseWriter, r *shttp.Request) {
+       id, err := util.ParseParamIdAndErr(w, r)
+       if err != nil {
+               return
+       }
+
+       in, err := s.ingredients.Get(id)
+       if err != nil {
+               util.HandleServiceError(w, err)
+               return
+       }
+
+       util.ParseAndWriteResponse(w, in, shttp.StatusOK)
+}
 
 func (s *Server) ingredientHandleList(w shttp.ResponseWriter, r *shttp.Request) {
        ingredients, err := s.ingredients.List()
index cee58befc7575079043747c0770aa00e17250047..9cbad9ab0ef4e2bcfc8134a504a56c4e3fa8117b 100644 (file)
@@ -4,8 +4,21 @@ import (
        "encoding/json"
        "log"
        "net/http"
+       "strconv"
+       serrors "errors"
+
+       "jsdaj.tq/pf/internal/errors"
 )
 
+func ParseParamIdAndErr(w http.ResponseWriter, r *http.Request) (int, error) {
+       id, err := strconv.Atoi(r.PathValue("id"))
+       if err != nil {
+               HandleServiceError(w, errors.MapError(serrors.New("bad_id")))
+               return 0, err
+       }
+       return id, nil
+}
+
 func ParseRequestAndErr[T any](w http.ResponseWriter, r *http.Request, input T) error {
        err := json.NewDecoder(r.Body).Decode(input)
        if err != nil {
index 663801156d44b738d3ec38d8939a499ce59def70..01742fdab8853395877df62ea6a93d6218646bb3 100644 (file)
@@ -58,6 +58,19 @@ func (s *Ingredient) Delete(id int) error {
        return err
 }
 
+func (s *Ingredient) Get(id int) (*core.Ingredient, error) {
+       in, err := s.repository.IngredientGet(id)
+       if err != nil {
+               return nil, errors.MapError(err)
+       }
+
+       if in == nil {
+               return nil, errors.Error{Code: errors.NotFoundError, Message: "not_found"}
+       }
+
+       return in, nil
+}
+
 func InitIngredientService(repository persistence.IngredientRepository) Ingredient {
        return Ingredient{
                repository,