76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package endpoints
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"relynolli-server/models"
|
|
"relynolli-server/services"
|
|
)
|
|
|
|
type createCartItemRequest struct {
|
|
FuserId int `json:"fuserId"`
|
|
PriceTypeId int `json:"priceTypeId,omitempty"`
|
|
ProductId int `json:"productId"`
|
|
Quantity int `json:"quantity,omitempty"`
|
|
}
|
|
|
|
type updateCartRequest struct {
|
|
FuserId int `json:"fuserId"`
|
|
ProductId int `json:"productId"`
|
|
Quantity int `json:"quantity"`
|
|
}
|
|
|
|
type deleteCartRequest struct {
|
|
FuserId int `json:"fuserId"`
|
|
ProductId int `json:"productId"`
|
|
}
|
|
|
|
func (h *handlers) CreateCartItem(c *gin.Context) {
|
|
req := createCartItemRequest{}
|
|
err := c.ShouldBindJSON(&req)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{Status: http.StatusBadRequest, Info: fmt.Sprintf("Bad request. Error info: %s", err.Error())})
|
|
return
|
|
}
|
|
services.AddItemToCart(req.FuserId, req.ProductId)
|
|
|
|
c.JSON(http.StatusCreated, models.Response{Status: http.StatusCreated, Info: fmt.Sprintf("Item %d has added to cart", req.ProductId)})
|
|
}
|
|
|
|
func (h *handlers) UpdateCartItem(c *gin.Context) {
|
|
req := updateCartRequest{}
|
|
err := c.ShouldBindJSON(&req)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{Status: http.StatusBadRequest, Info: fmt.Sprintf("Bad request. Error info: %s", err.Error())})
|
|
return
|
|
}
|
|
|
|
err = services.UpdateCartItem(req.FuserId, req.ProductId, req.Quantity)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{Status: http.StatusBadRequest, Info: fmt.Sprintf("Bad request. Error info: %s", err.Error())})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{Status: http.StatusOK})
|
|
|
|
}
|
|
|
|
func (h *handlers) DeleteCartItem(c *gin.Context) {
|
|
|
|
req := deleteCartRequest{}
|
|
err := c.ShouldBindJSON(&req)
|
|
|
|
if err != nil {
|
|
c.JSON(400, models.Response{Status: 400, Info: fmt.Sprintf("Bad request. Error info: %s", err.Error())})
|
|
return
|
|
}
|
|
|
|
services.DeleteCartItem(req.FuserId, req.ProductId)
|
|
|
|
c.JSON(http.StatusNoContent, models.Response{Status: http.StatusNoContent})
|
|
}
|