diff --git a/pkg/abstract/cart/cart.go b/pkg/abstract/cart/cart.go index 932921d..0ee66ba 100644 --- a/pkg/abstract/cart/cart.go +++ b/pkg/abstract/cart/cart.go @@ -13,4 +13,5 @@ type Cart interface { type CartItems interface { ListItems(ctx context.Context, cartId int64) (*[]cart.CartItems, error) + DeleteItem(ctx context.Context, cartItemId int64) (bool, error) } diff --git a/pkg/handler/cart/cart.go b/pkg/handler/cart/cart.go index d403b89..1f25b4c 100644 --- a/pkg/handler/cart/cart.go +++ b/pkg/handler/cart/cart.go @@ -5,6 +5,7 @@ import ( "context" "github.com/gin-gonic/gin" "net/http" + "strconv" "time" ) @@ -78,3 +79,20 @@ func (s *CartHandler) getItems(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"items": items}) return } + +func (s *CartHandler) deleteItemFromCart(c *gin.Context) { + ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second) + defer cancel() + + cartItemId := c.Param("id") + cartItemID, _ := strconv.ParseInt(cartItemId, 10, 64) + + _, err := s.service.DeleteItem(ctx, cartItemID) + if err != nil { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()}) + return + } + + c.Status(http.StatusNoContent) + +} diff --git a/pkg/handler/cart/routes.go b/pkg/handler/cart/routes.go index ce58b1c..68e5c16 100644 --- a/pkg/handler/cart/routes.go +++ b/pkg/handler/cart/routes.go @@ -12,5 +12,7 @@ func (s *CartHandler) registerGroup(middleware ...gin.HandlerFunc) gin.IRoutes { func (s *CartHandler) routes() http.Handler { s.router.POST("/add", s.addToCart) s.router.GET("/list", s.getItems) + s.router.DELETE("/remove/:id", s.deleteItemFromCart) + return s.serve.Gin } diff --git a/pkg/service/cart/delete_item_from_cart.go b/pkg/service/cart/delete_item_from_cart.go new file mode 100644 index 0000000..7b1c6ca --- /dev/null +++ b/pkg/service/cart/delete_item_from_cart.go @@ -0,0 +1,16 @@ +package cart + +import ( + "Go_Food_Delivery/pkg/database" + "context" +) + +func (cartSrv *CartService) DeleteItem(ctx context.Context, cartItemId int64) (bool, error) { + filter := database.Filter{"cart_item_id": cartItemId} + + _, err := cartSrv.db.Delete(ctx, "cart_items", filter) + if err != nil { + return false, err + } + return true, nil +}