]> localhost Git - tq-wave11-pf.git/commitdiff
feat: delete ingredient
authorJansen <[email protected]>
Thu, 7 Aug 2025 20:12:10 +0000 (16:12 -0400)
committerJansen <[email protected]>
Thu, 7 Aug 2025 20:12:10 +0000 (16:12 -0400)
persistence/inmemory/ingredients.go
pkg/core/ingrendient.go
pkg/http/ingredient.go
pkg/services/ingredient.go
pkg/util/collections.go [new file with mode: 0644]

index 6cb91c9c128d85317b362cf2f75714b1f078e9b9..4f699224c5d833123cf70c81135bd0de14ad77bd 100644 (file)
@@ -5,6 +5,7 @@ import (
        "time"
 
        "jsdaj.tq/pf/pkg/core"
+       "jsdaj.tq/pf/pkg/util"
 )
 
 func (p *InmemoryPersistence) IngredientCreate(in core.Ingredient) (core.Ingredient, error) {
@@ -54,6 +55,13 @@ func (p *InmemoryPersistence) IngredientUpdate(in core.Ingredient) error {
 }
 
 func (p *InmemoryPersistence) IngredientDeactivate(id int) error {
+       if id < 1 || id > len(p.ingredients) {
+               return nil
+       }
+
+       now := time.Now()
+       p.ingredients[id-1].DeletedAt = &now 
+
        return nil
 }
 
@@ -61,12 +69,18 @@ func (p *InmemoryPersistence) IngredientGet(id int) (*core.Ingredient, error) {
        if id < 1 || id > len(p.ingredients) {
                return nil, nil
        }
-       return &p.ingredients[id-1], nil
+
+       ingredient := p.ingredients[id-1]
+       if ingredient.DeletedAt != nil {
+               return nil, nil
+       }
+
+       return &ingredient, nil
 }
 
 func (p *InmemoryPersistence) IngredientGetByName(name string) (*core.Ingredient, error) {
        for _, in := range p.ingredients {
-               if in.Name == name {
+               if in.Name == name && in.DeletedAt == nil {
                        return &in, nil
                }
        }
@@ -74,5 +88,7 @@ func (p *InmemoryPersistence) IngredientGetByName(name string) (*core.Ingredient
 }
 
 func (p *InmemoryPersistence) IngredientList() ([]core.Ingredient, error) {
-       return p.ingredients, nil
+       filtered := util.Filter(p.ingredients, func(i core.Ingredient) bool { return i.DeletedAt == nil })
+
+       return filtered, nil
 }
index 26dc172892ecb7cd83572b9d016e31a0068edea0..aa2deb88973ff2efddb303f96acd9fdd58ba8100 100644 (file)
@@ -11,7 +11,8 @@ type Ingredient struct {
        Name        string     `json:"name"`
        Quantity    int        `json:"quantity"`
        Description string     `json:"description"`
-       CreatedAt   *time.Time `json:"createdAt,omitempty"`
+       CreatedAt   *time.Time `json:"-"`
+       DeletedAt   *time.Time `json:"-"`
 }
 
 func (ingredient Ingredient) IsValid() error {
index 671f486f88606024346cd6dd1894d3fc5ae72cbc..78167cc426460fefedf318d719cbcde89affe84e 100644 (file)
@@ -52,7 +52,21 @@ func (s *Server) ingredientHandleUpdate(w shttp.ResponseWriter, r *shttp.Request
        util.ParseAndWriteResponse(w, ingredient, shttp.StatusOK)
 }
 
-func (s *Server) ingredientHandleDelete(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) ingredientHandleDelete(w shttp.ResponseWriter, r *shttp.Request) {
+       id, err := strconv.Atoi(r.PathValue("id"))
+       if err != nil {
+               util.HandleServiceError(w, errors.MapError(serrors.New("bad_id")))
+               return
+       }
+
+       err = s.ingredients.Delete(id)
+       if err != nil {
+               util.HandleServiceError(w, err)
+               return
+       }
+
+       util.ParseAndWriteResponse(w, struct{}{}, shttp.StatusNoContent)
+}
 
 func (s *Server) ingredientHandleGet(w shttp.ResponseWriter, r *shttp.Request) {}
 
index 48e08143b95dc50833e797ad3fa9d6a31cc97eba..663801156d44b738d3ec38d8939a499ce59def70 100644 (file)
@@ -39,11 +39,25 @@ func (s *Ingredient) Update(ingredient core.Ingredient) error {
 func (s *Ingredient) List() ([]core.Ingredient, error) {
        ingredients, err := s.repository.IngredientList()
        if err != nil {
-               return []core.Ingredient{}, err
+               return []core.Ingredient{}, errors.MapError(err)
        }
        return ingredients, nil
 }
 
+func (s *Ingredient) Delete(id int) error {
+       in, err := s.repository.IngredientGet(id)
+       if err != nil {
+               return errors.MapError(err)
+       }
+
+       if in == nil {
+               return errors.Error{Code: errors.NotFoundError, Message: "not_found"}
+       }
+
+       err = s.repository.IngredientDeactivate(id)
+       return err
+}
+
 func InitIngredientService(repository persistence.IngredientRepository) Ingredient {
        return Ingredient{
                repository,
diff --git a/pkg/util/collections.go b/pkg/util/collections.go
new file mode 100644 (file)
index 0000000..5bfea8c
--- /dev/null
@@ -0,0 +1,15 @@
+package util
+
+type FilterFunc[T any] func(T) bool
+
+func Filter[T any](slic []T, f FilterFunc[T]) []T {
+       result := []T{}
+
+       for _, s := range slic {
+               if f(s) {
+                       result = append(result, s)
+               }
+       }
+
+       return result
+}