2024-03-15 21:27:45 +03:00
|
|
|
package endpoints
|
|
|
|
|
2024-03-28 18:20:57 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"relynolli-server/models"
|
|
|
|
"relynolli-server/status"
|
|
|
|
"relynolli-server/storage"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-03-15 21:27:45 +03:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2024-03-28 18:20:57 +03:00
|
|
|
func (h *handlers) CreateCartItem(c *gin.Context) {
|
|
|
|
meta := models.Meta{
|
|
|
|
RequestStarted: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
response := models.Response{
|
|
|
|
Status: status.STATUS_OK,
|
|
|
|
Meta: &meta,
|
|
|
|
}
|
|
|
|
s := storage.NewStorageCart()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
query := new(createCartItemRequest)
|
|
|
|
|
|
|
|
err := c.ShouldBindJSON(query)
|
|
|
|
if err != nil {
|
|
|
|
response.Status = status.STATUS_BAD_REQUEST
|
|
|
|
response.Info = fmt.Sprintf("Error: %s", err.Error())
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
c.JSON(400, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = s.AddItemToCart(ctx, int64(query.FuserId), int64(query.ProductId))
|
|
|
|
if err != nil {
|
|
|
|
response.Status = status.STATUS_BAD_REQUEST
|
|
|
|
response.Info = fmt.Sprintf("Error: %s", err.Error())
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
c.JSON(400, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
response.Info = fmt.Sprintf("Item has added to cart")
|
|
|
|
response.Status = status.STATUS_OK
|
|
|
|
c.JSON(201, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handlers) UpdateCartItem(c *gin.Context) {
|
|
|
|
meta := models.Meta{
|
|
|
|
RequestStarted: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
response := models.Response{
|
|
|
|
Status: status.STATUS_OK,
|
|
|
|
Meta: &meta,
|
|
|
|
}
|
|
|
|
s := storage.NewStorageCart()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
query := new(updateCartRequest)
|
|
|
|
|
|
|
|
err := c.ShouldBindJSON(query)
|
|
|
|
if err != nil {
|
|
|
|
response.Status = status.STATUS_BAD_REQUEST
|
|
|
|
response.Info = fmt.Sprintf("Error: %s", err.Error())
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
c.JSON(400, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = s.UpdateCartItem(ctx, int64(query.FuserId), int64(query.ProductId), int64(query.Quantity))
|
|
|
|
if err != nil {
|
|
|
|
response.Status = status.STATUS_BAD_REQUEST
|
|
|
|
response.Info = fmt.Sprintf("Error: %s", err.Error())
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
c.JSON(400, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
response.Info = fmt.Sprintf("Item has updated in cart")
|
|
|
|
response.Status = status.STATUS_OK
|
|
|
|
c.JSON(200, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handlers) DeleteCartItem(c *gin.Context) {
|
|
|
|
meta := models.Meta{
|
|
|
|
RequestStarted: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
response := models.Response{
|
|
|
|
Status: status.STATUS_OK,
|
|
|
|
Meta: &meta,
|
|
|
|
}
|
|
|
|
s := storage.NewStorageCart()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
query := new(deleteCartRequest)
|
|
|
|
|
|
|
|
err := c.ShouldBindJSON(query)
|
|
|
|
if err != nil {
|
|
|
|
response.Status = status.STATUS_BAD_REQUEST
|
|
|
|
response.Info = fmt.Sprintf("Error: %s", err.Error())
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
c.JSON(400, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = s.DeleteCartItem(ctx, int64(query.FuserId), int64(query.ProductId))
|
|
|
|
if err != nil {
|
|
|
|
response.Status = status.STATUS_BAD_REQUEST
|
|
|
|
response.Info = fmt.Sprintf("Error: %s", err.Error())
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
c.JSON(400, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.RequestFinished = time.Now().Unix()
|
|
|
|
response.Info = fmt.Sprintf("Item has dropped from cart")
|
|
|
|
response.Status = status.STATUS_OK
|
|
|
|
c.JSON(204, response)
|
|
|
|
}
|
|
|
|
|
2024-03-26 02:21:35 +03:00
|
|
|
//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})
|
|
|
|
//}
|