basket

package
v0.0.0-...-f43f97b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound            = errors.New("Item not found")
	ErrCustomerCannotBeNil = errors.New("Customer cannot be nil")
)

Functions

func NewBasketHandler

func NewBasketHandler(r *gin.RouterGroup, service Service)

func NewBasketRepository

func NewBasketRepository(db *gorm.DB) *basketRepository

Types

type AddItemRequest

type AddItemRequest struct {
	BasketId string  `json:"basketId"  validate:"required"`
	Sku      string  `json:"sku"  validate:"required"`
	Quantity int     `json:"quantity" validate:"required,gte=0,lte=20"`
	Price    float64 `json:"price" validate:"required,gte=0"`
}

type Basket

type Basket struct {
	Id         string    `json:"id"`
	CustomerId string    `json:"CustomerId"`
	Items      []Item    `json:"items"`
	CreatedAt  time.Time `json:"createdAt"`
}

func Create

func Create(customer string) (*Basket, error)

func (*Basket) AddItem

func (b *Basket) AddItem(sku string, price float64, quantity int) (*Item, error)

func (*Basket) RemoveItem

func (b *Basket) RemoveItem(itemId string) (err error)

func (*Basket) SearchItem

func (b *Basket) SearchItem(itemId string) (int, *Item)

func (*Basket) SearchItemBySku

func (b *Basket) SearchItemBySku(sku string) (int, *Item)

func (*Basket) UpdateItem

func (b *Basket) UpdateItem(itemId string, quantity int) (err error)

func (*Basket) ValidateBasket

func (b *Basket) ValidateBasket() error

type CreateBasketRequest

type CreateBasketRequest struct {
	Buyer string `json:"buyer" validate:"required"`
}

type Item

type Item struct {
	Id        string  `json:"id"`
	Sku       string  `json:"sku"`
	UnitPrice float64 `json:"unitPrice"`
	Quantity  int     `json:"quantity"`
}

type Repository

type Repository interface {
	// Get returns the basket with the specified basket Id.
	Get(ctx context.Context, id string) *Basket
	// GetByCustomerId returns the basket with the specified customer Id.
	GetByCustomerId(ctx context.Context, customerId string) *Basket
	// Create saves a new basket in the storage.
	Create(ctx context.Context, basket *Basket) error
	// Update updates the basket with given Is in the storage.
	Update(ctx context.Context, basket Basket) error
	// Delete removes the basket with given Is from the storage.
	Delete(ctx context.Context, basket Basket) error
}

Repository encapsulates the logic to access basket from the data source.

type Service

type Service interface {
	Get(ctx context.Context, id string) (*Basket, error)
	GetByCustomerId(ctx context.Context, customerId string) (*Basket, error)
	Create(ctx context.Context, buyer string) (*Basket, error)
	Delete(ctx context.Context, id string) (*Basket, error)

	UpdateItem(ctx context.Context, basketId, itemId string, quantity int) error
	AddItem(ctx context.Context, basketId, sku string, quantity int, price float64) (string, error)
	DeleteItem(ctx context.Context, basketId, itemId string) error
}

func NewBasketService

func NewBasketService(repo Repository) Service

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL