forked from ebhomengo/niki
refactor service layer
This commit is contained in:
parent
d400991b13
commit
f5a2a31dd0
|
|
@ -35,3 +35,13 @@ const (
|
||||||
CartStatusExpired CartStatus = "expired"
|
CartStatusExpired CartStatus = "expired"
|
||||||
CartStatusCheckedOut CartStatus = "checked_out"
|
CartStatusCheckedOut CartStatus = "checked_out"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (c CartStatus) IsValid() bool {
|
||||||
|
switch c {
|
||||||
|
case CartStatusExpired, CartStatusActive, CartStatusCheckedOut:
|
||||||
|
return true
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,31 +3,34 @@ package service
|
||||||
import (
|
import (
|
||||||
"git.gocasts.ir/ebhomengo/niki/domain/shoppingbasket/entity"
|
"git.gocasts.ir/ebhomengo/niki/domain/shoppingbasket/entity"
|
||||||
"git.gocasts.ir/ebhomengo/niki/types"
|
"git.gocasts.ir/ebhomengo/niki/types"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddToCartRequest struct {
|
type AddToCartRequest struct {
|
||||||
UserID types.ID `json:"user_id"`
|
|
||||||
ProductID types.ID `json:"product_id"`
|
|
||||||
Quantity int `json:"quantity"`
|
|
||||||
Price types.Price `json:"price"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetCartResponse struct {
|
|
||||||
UserID types.ID `json:"user_id"`
|
|
||||||
Items []entity.Item `json:"items"`
|
|
||||||
TotalPrice types.Price `json:"total_price"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
ExpireAt int64 `json:"expire_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RemoveFromCartRequest struct {
|
|
||||||
UserID types.ID `json:"user_id"`
|
|
||||||
ProductID types.ID `json:"product_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdateQuantityRequest struct {
|
|
||||||
UserID types.ID `json:"user_id"`
|
UserID types.ID `json:"user_id"`
|
||||||
ProductID types.ID `json:"product_id"`
|
ProductID types.ID `json:"product_id"`
|
||||||
Quantity int `json:"quantity"`
|
Quantity int `json:"quantity"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetCartResponse struct {
|
||||||
|
ID types.ID `json:"id"`
|
||||||
|
UserID types.ID `json:"user_id"`
|
||||||
|
Items []entity.Item `json:"items"`
|
||||||
|
TotalPrice float64 `json:"total_price"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
ExpireAt time.Time `json:"expire_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RemoveFromCartRequest struct {
|
||||||
|
CartID types.ID `json:"cart_id"`
|
||||||
|
ItemID types.ID `json:"item_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateQuantityRequest struct {
|
||||||
|
CartID types.ID `json:"cart_id"`
|
||||||
|
ItemID types.ID `json:"item_id"`
|
||||||
|
Quantity int `json:"quantity"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ func (s Service) AddToBasket(ctx context.Context, req AddToCartRequest) error {
|
||||||
Quantity: req.Quantity,
|
Quantity: req.Quantity,
|
||||||
Price: req.Price,
|
Price: req.Price,
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
AddedAt: time.Now().UnixNano(),
|
AddedAt: time.Now().UTC(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,9 +58,11 @@ func (s Service) GetCart(ctx context.Context, userID types.ID) (GetCartResponse,
|
||||||
}
|
}
|
||||||
|
|
||||||
return GetCartResponse{
|
return GetCartResponse{
|
||||||
|
ID: res.ID,
|
||||||
UserID: res.UserID,
|
UserID: res.UserID,
|
||||||
Items: res.Items,
|
Items: res.Items,
|
||||||
TotalPrice: res.TotalPrice,
|
TotalPrice: res.TotalPrice,
|
||||||
|
Status: string(res.Status),
|
||||||
CreatedAt: res.CreatedAt,
|
CreatedAt: res.CreatedAt,
|
||||||
ExpireAt: res.ExpireAt,
|
ExpireAt: res.ExpireAt,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
@ -74,7 +76,7 @@ func (s Service) RemoveFromCart(ctx context.Context, req RemoveFromCartRequest)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.repo.DeleteItem(ctx, req.UserID, req.ProductID)
|
return s.repo.DeleteItem(ctx, req.CartID, req.ItemID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) UpdateQuantity(ctx context.Context, req UpdateQuantityRequest) error {
|
func (s Service) UpdateQuantity(ctx context.Context, req UpdateQuantityRequest) error {
|
||||||
|
|
@ -86,13 +88,13 @@ func (s Service) UpdateQuantity(ctx context.Context, req UpdateQuantityRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Quantity == 0 {
|
if req.Quantity == 0 {
|
||||||
return s.repo.DeleteItem(ctx, req.UserID, req.ProductID)
|
return s.repo.DeleteItem(ctx, req.CartID, req.ItemID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.repo.UpdateQuantity(ctx, req.UserID, req.ProductID, req.Quantity)
|
return s.repo.UpdateQuantity(ctx, req.CartID, req.ItemID, req.Quantity)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) ClearCart(ctx context.Context, userID types.ID) error {
|
func (s Service) ClearCart(ctx context.Context, cartID, userID types.ID) error {
|
||||||
const op = "shoppingbaskerapp.service.ClearCart"
|
const op = "shoppingbaskerapp.service.ClearCart"
|
||||||
|
|
||||||
if userID < 1 {
|
if userID < 1 {
|
||||||
|
|
@ -101,5 +103,14 @@ func (s Service) ClearCart(ctx context.Context, userID types.ID) error {
|
||||||
WithMessage("invalid user id")
|
WithMessage("invalid user id")
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.repo.DeleteCart(ctx, userID)
|
return s.repo.DeleteCart(ctx, cartID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Service) UpdateCartStatus(ctx context.Context, cartID types.ID, status entity.CartStatus) error {
|
||||||
|
const op = "shoppingbaskerapp.service.ClearCart"
|
||||||
|
if !status.IsValid() {
|
||||||
|
return richerror.New(op).WithKind(richerror.KindInvalid).WithMessage("invalid status")
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.repo.UpdateStatus(ctx, cartID, status)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue