productbus

package
v0.0.0-...-8ad27d5 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package productbus provides business access to product domain.

Index

Constants

View Source
const (
	OrderByProductID = "product_id"
	OrderByUserID    = "user_id"
	OrderByName      = "name"
	OrderByCost      = "cost"
	OrderByQuantity  = "quantity"
)

Set of fields that the results can be ordered by.

Variables

View Source
var (
	ErrNotFound     = errors.New("product not found")
	ErrUserDisabled = errors.New("user disabled")
	ErrInvalidCost  = errors.New("cost not valid")
)

Set of error variables for CRUD operations.

View Source
var DefaultOrderBy = order.NewBy(OrderByProductID, order.ASC)

DefaultOrderBy represents the default way we sort.

Functions

This section is empty.

Types

type Business

type Business struct {
	// contains filtered or unexported fields
}

Business manages the set of APIs for product access.

func NewBusiness

func NewBusiness(log *logger.Logger, userBus *userbus.Business, delegate *delegate.Delegate, storer Storer) *Business

NewBusiness constructs a product business API for use.

func (*Business) Count

func (b *Business) Count(ctx context.Context, filter QueryFilter) (int, error)

Count returns the total number of products.

func (*Business) Create

func (b *Business) Create(ctx context.Context, np NewProduct) (Product, error)

Create adds a new product to the system.

func (*Business) Delete

func (b *Business) Delete(ctx context.Context, prd Product) error

Delete removes the specified product.

func (*Business) NewWithTx

func (b *Business) NewWithTx(tx sqldb.CommitRollbacker) (*Business, error)

NewWithTx constructs a new business value that will use the specified transaction in any store related calls.

func (*Business) Query

func (b *Business) Query(ctx context.Context, filter QueryFilter, orderBy order.By, page page.Page) ([]Product, error)

Query retrieves a list of existing products.

func (*Business) QueryByID

func (b *Business) QueryByID(ctx context.Context, productID uuid.UUID) (Product, error)

QueryByID finds the product by the specified Ib.

func (*Business) QueryByUserID

func (b *Business) QueryByUserID(ctx context.Context, userID uuid.UUID) ([]Product, error)

QueryByUserID finds the products by a specified User Ib.

func (*Business) Update

func (b *Business) Update(ctx context.Context, prd Product, up UpdateProduct) (Product, error)

Update modifies information about a product.

type Name

type Name struct {
	// contains filtered or unexported fields
}

Name represents a name in the system.

func MustParseName

func MustParseName(value string) Name

MustParseName parses the string value and returns a name if the value complies with the rules for a name. If an error occurs the function panics.

func ParseName

func ParseName(value string) (Name, error)

ParseName parses the string value and returns a name if the value complies with the rules for a name.

func (Name) Equal

func (n Name) Equal(n2 Name) bool

Equal provides support for the go-cmp package and testing.

func (Name) String

func (n Name) String() string

String returns the value of the name.

type NewProduct

type NewProduct struct {
	UserID   uuid.UUID
	Name     Name
	Cost     float64
	Quantity int
}

NewProduct is what we require from clients when adding a Product.

func TestGenerateNewProducts

func TestGenerateNewProducts(n int, userID uuid.UUID) []NewProduct

TestGenerateNewProducts is a helper method for testing.

type Product

type Product struct {
	ID          uuid.UUID
	UserID      uuid.UUID
	Name        Name
	Cost        float64
	Quantity    int
	DateCreated time.Time
	DateUpdated time.Time
}

Product represents an individual product.

func TestGenerateSeedProducts

func TestGenerateSeedProducts(ctx context.Context, n int, api *Business, userID uuid.UUID) ([]Product, error)

TestGenerateSeedProducts is a helper method for testing.

type QueryFilter

type QueryFilter struct {
	ID       *uuid.UUID
	Name     *Name
	Cost     *float64
	Quantity *int
}

QueryFilter holds the available fields a query can be filtered on. We are using pointer semantics because the With API mutates the value.

type Storer

type Storer interface {
	NewWithTx(tx sqldb.CommitRollbacker) (Storer, error)
	Create(ctx context.Context, prd Product) error
	Update(ctx context.Context, prd Product) error
	Delete(ctx context.Context, prd Product) error
	Query(ctx context.Context, filter QueryFilter, orderBy order.By, page page.Page) ([]Product, error)
	Count(ctx context.Context, filter QueryFilter) (int, error)
	QueryByID(ctx context.Context, productID uuid.UUID) (Product, error)
	QueryByUserID(ctx context.Context, userID uuid.UUID) ([]Product, error)
}

Storer interface declares the behavior this package needs to perists and retrieve data.

type UpdateProduct

type UpdateProduct struct {
	Name     *Name
	Cost     *float64
	Quantity *int
}

UpdateProduct defines what information may be provided to modify an existing Product. All fields are optional so clients can send just the fields they want changed. It uses pointer fields so we can differentiate between a field that was not provided and a field that was provided as explicitly blank. Normally we do not want to use pointers to basic types but we make exceptions around marshalling/unmarshalling.

Directories

Path Synopsis
stores
productdb
Package productdb contains product related CRUD functionality.
Package productdb contains product related CRUD functionality.

Jump to

Keyboard shortcuts

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