Fix cart
parent
d7b1f65805
commit
54b7fedcdb
|
@ -7,10 +7,9 @@ type handlers struct{}
|
||||||
type Handlers interface {
|
type Handlers interface {
|
||||||
GetCartItems(c *gin.Context)
|
GetCartItems(c *gin.Context)
|
||||||
CreateFUser(c *gin.Context)
|
CreateFUser(c *gin.Context)
|
||||||
//
|
CreateCartItem(c *gin.Context)
|
||||||
//CreateCartItem(c *gin.Context)
|
UpdateCartItem(c *gin.Context)
|
||||||
//UpdateCartItem(c *gin.Context)
|
DeleteCartItem(c *gin.Context)
|
||||||
//DeleteCartItem(c *gin.Context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHandlers() Handlers {
|
func GetHandlers() Handlers {
|
||||||
|
|
|
@ -1,5 +1,15 @@
|
||||||
package endpoints
|
package endpoints
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"relynolli-server/models"
|
||||||
|
"relynolli-server/status"
|
||||||
|
"relynolli-server/storage"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type createCartItemRequest struct {
|
type createCartItemRequest struct {
|
||||||
FuserId int `json:"fuserId"`
|
FuserId int `json:"fuserId"`
|
||||||
PriceTypeId int `json:"priceTypeId,omitempty"`
|
PriceTypeId int `json:"priceTypeId,omitempty"`
|
||||||
|
@ -18,6 +28,114 @@ type deleteCartRequest struct {
|
||||||
ProductId int `json:"productId"`
|
ProductId int `json:"productId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
//func (h *handlers) CreateCartItem(c *gin.Context) {
|
//func (h *handlers) CreateCartItem(c *gin.Context) {
|
||||||
// req := createCartItemRequest{}
|
// req := createCartItemRequest{}
|
||||||
// err := c.ShouldBindJSON(&req)
|
// err := c.ShouldBindJSON(&req)
|
||||||
|
|
|
@ -8,15 +8,14 @@ import (
|
||||||
func HandleRoutes(parent *gin.RouterGroup) {
|
func HandleRoutes(parent *gin.RouterGroup) {
|
||||||
h := endpoints.GetHandlers()
|
h := endpoints.GetHandlers()
|
||||||
cart := parent.Group("/cart")
|
cart := parent.Group("/cart")
|
||||||
//itemRouter := cart.Group("/item")
|
itemRouter := cart.Group("/item")
|
||||||
{
|
{
|
||||||
cart.GET("", h.GetCartItems)
|
cart.GET("", h.GetCartItems)
|
||||||
cart.POST("", h.CreateFUser)
|
cart.POST("", h.CreateFUser)
|
||||||
}
|
}
|
||||||
|
{
|
||||||
//{
|
itemRouter.POST("", h.CreateCartItem)
|
||||||
// itemRouter.POST("", h.CreateCartItem)
|
itemRouter.PATCH("", h.UpdateCartItem)
|
||||||
// itemRouter.PATCH("", h.UpdateCartItem)
|
itemRouter.DELETE("", h.DeleteCartItem)
|
||||||
// itemRouter.DELETE("", h.DeleteCartItem)
|
}
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package endpoints
|
package endpoints
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"relynolli-server/models"
|
"relynolli-server/models"
|
||||||
"relynolli-server/services"
|
"relynolli-server/status"
|
||||||
|
"relynolli-server/storage"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
|
@ -32,12 +34,27 @@ type makeOrderRequest struct {
|
||||||
func (h handlers) GetTotal(c *gin.Context) {
|
func (h handlers) GetTotal(c *gin.Context) {
|
||||||
req := getTotalRequest{}
|
req := getTotalRequest{}
|
||||||
err := c.ShouldBindJSON(&req)
|
err := c.ShouldBindJSON(&req)
|
||||||
|
meta := models.Meta{
|
||||||
|
RequestStarted: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
s := storage.NewStorageOrder()
|
||||||
|
resp := models.Response{
|
||||||
|
Status: status.STATUS_OK,
|
||||||
|
Meta: &meta,
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, models.Response{Status: http.StatusBadRequest, Info: "fuserId is not provided"})
|
meta.RequestFinished = time.Now().Unix()
|
||||||
|
resp.Status = status.STATUS_BAD_REQUEST
|
||||||
|
resp.Info = fmt.Sprintf("Error: %s", err.Error())
|
||||||
|
c.JSON(400, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
total := services.GetTotal(req.FuserId)
|
|
||||||
c.JSON(http.StatusOK, getTotalResponse{TotalProductPrice: total})
|
data, err := s.GetTotal(ctx, int64(req.FuserId))
|
||||||
|
resp.Data = data
|
||||||
|
c.JSON(200, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h handlers) MakeOrder(c *gin.Context) {
|
func (h handlers) MakeOrder(c *gin.Context) {
|
||||||
|
@ -46,26 +63,26 @@ func (h handlers) MakeOrder(c *gin.Context) {
|
||||||
req := makeOrderRequest{}
|
req := makeOrderRequest{}
|
||||||
err := c.ShouldBindJSON(&req)
|
err := c.ShouldBindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, models.Response{Status: http.StatusBadRequest, Info: fmt.Sprintf("ERROR: %s", err.Error())})
|
//c.JSON(400, models.Response{Status: http.StatusBadRequest, Info: fmt.Sprintf("ERROR: %s", err.Error())})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
validationErr := validate.Struct(req)
|
validationErr := validate.Struct(req)
|
||||||
|
|
||||||
if validationErr != nil {
|
if validationErr != nil {
|
||||||
responseErr := validationErr.(validator.ValidationErrors)[0]
|
//responseErr := validationErr.(validator.ValidationErrors)[0]
|
||||||
c.JSON(http.StatusBadRequest, models.Response{Status: http.StatusBadRequest, Info: fmt.Sprintf("Validation Error: Field %s should be %s", responseErr.Field(), responseErr.Tag())})
|
//c.JSON(http.StatusBadRequest, models.Response{Status: http.StatusBadRequest, Info: fmt.Sprintf("Validation Error: Field %s should be %s", responseErr.Field(), responseErr.Tag())})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
kassaResult, serviceErr := services.MakeOrder(req.FuserId, req.Email, req.FullName, req.PhoneNumber)
|
//kassaResult, serviceErr := services.MakeOrder(req.FuserId, req.Email, req.FullName, req.PhoneNumber)
|
||||||
|
|
||||||
if serviceErr != nil {
|
//if serviceErr != nil {
|
||||||
c.JSON(http.StatusInternalServerError, models.Response{Status: http.StatusInternalServerError, Info: fmt.Sprintf("Error: %s", serviceErr.Error())})
|
// c.JSON(http.StatusInternalServerError, models.Response{Status: http.StatusInternalServerError, Info: fmt.Sprintf("Error: %s", serviceErr.Error())})
|
||||||
return
|
// return
|
||||||
}
|
//}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, kassaResult)
|
//c.JSON(http.StatusOK, kassaResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handlers interface {
|
type Handlers interface {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"relynolli-server/handlers/cart"
|
"relynolli-server/handlers/cart"
|
||||||
"relynolli-server/handlers/news"
|
"relynolli-server/handlers/news"
|
||||||
|
"relynolli-server/handlers/order"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
@ -17,6 +18,5 @@ func InitializeRouter(router *gin.Engine) {
|
||||||
catalog.HandleRoutes(APIV1Router)
|
catalog.HandleRoutes(APIV1Router)
|
||||||
cart.HandleRoutes(APIV1Router)
|
cart.HandleRoutes(APIV1Router)
|
||||||
news.HandleRoutes(APIV1Router)
|
news.HandleRoutes(APIV1Router)
|
||||||
// order.HandleRoutes(APIV1Router)
|
order.HandleRoutes(APIV1Router)
|
||||||
// validate.HandleRoutes(APIV1Router)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,8 +101,7 @@ func (s *storage) AddItemToCart(ctx context.Context, fuserId, productId int64) e
|
||||||
|
|
||||||
func (s *storage) UpdateCartItem(ctx context.Context, fuserId, productId, quantity int64) error {
|
func (s *storage) UpdateCartItem(ctx context.Context, fuserId, productId, quantity int64) error {
|
||||||
if quantity <= 0 {
|
if quantity <= 0 {
|
||||||
// TODO Implement
|
return s.DeleteCartItem(ctx, fuserId, productId)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
item, _ := s.GetCartItem(ctx, fuserId, productId)
|
item, _ := s.GetCartItem(ctx, fuserId, productId)
|
||||||
|
|
|
@ -44,7 +44,7 @@ func (s *storage) GetCatalogItemByCode(ctx context.Context, code string) (*catal
|
||||||
func (s *storage) GetCatalogItem(ctx context.Context, id *int64) (*catalog.DBCatalog, error) {
|
func (s *storage) GetCatalogItem(ctx context.Context, id *int64) (*catalog.DBCatalog, error) {
|
||||||
model := new(catalog.DBCatalog)
|
model := new(catalog.DBCatalog)
|
||||||
|
|
||||||
err := s.db.NewSelect().Model(model).Where("id = ?", id).Where("available_quantity > 0").Where("is_available = 1").Scan(ctx)
|
err := s.db.NewSelect().Model(model).Where("id = ?", id).Where("available_quantity > 0").Where("is_active = 1").Scan(ctx)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"relynolli-server/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StorageOrder interface {
|
||||||
|
GetTotal(ctx context.Context, fuserId int64) (*TotalQuery, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStorageOrder() StorageOrder {
|
||||||
|
if instance == nil {
|
||||||
|
instance = &storage{
|
||||||
|
db: internal.InitDatabase().GetInstance(),
|
||||||
|
rdb: internal.InitRedis(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
type TotalQuery struct {
|
||||||
|
Total float64 `bun:"total" json:"total"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) GetTotal(ctx context.Context, fuserId int64) (*TotalQuery, error) {
|
||||||
|
model := new(TotalQuery)
|
||||||
|
stmt := `select sum(price.PRICE * cart.quantity) as total from api_cart cart left join b_catalog_price price on cart.product_id = price.PRODUCT_ID and cart.price_type_id = price.CATALOG_GROUP_ID where fuser_id = ?`
|
||||||
|
err := s.db.NewRaw(stmt, fuserId).Scan(ctx, model)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return model, nil
|
||||||
|
}
|
Loading…
Reference in New Issue