product

package
v0.0.0-...-c64bf0e Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package product provides an example of a core business API. Right now these calls are just wrapping the data/store layer. But at some point you will want auditing or something that isn't specific to the data/store layer.

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 Core

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

Core manages the set of APIs for product access.

func NewCore

func NewCore(log *logger.Logger, usrCore *user.Core, delegate *delegate.Delegate, storer Storer) *Core

NewCore constructs a product core API for use.

func (*Core) Count

func (c *Core) Count(ctx context.Context, filter QueryFilter) (int, error)

Count returns the total number of products.

func (*Core) Create

func (c *Core) Create(ctx context.Context, np NewProduct) (Product, error)

Create adds a new product to the system.

func (*Core) Delete

func (c *Core) Delete(ctx context.Context, prd Product) error

Delete removes the specified product.

func (*Core) ExecuteUnderTransaction

func (c *Core) ExecuteUnderTransaction(tx transaction.Transaction) (*Core, error)

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

func (*Core) Query

func (c *Core) Query(ctx context.Context, filter QueryFilter, orderBy order.By, pageNumber int, rowsPerPage int) ([]Product, error)

Query retrieves a list of existing products.

func (*Core) QueryByID

func (c *Core) QueryByID(ctx context.Context, productID uuid.UUID) (Product, error)

QueryByID finds the product by the specified ID.

func (*Core) QueryByUserID

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

QueryByUserID finds the products by a specified User ID.

func (*Core) Update

func (c *Core) Update(ctx context.Context, prd Product, up UpdateProduct) (Product, error)

Update modifies information about a product.

type NewProduct

type NewProduct struct {
	UserID   uuid.UUID
	Name     string
	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        string
	Cost        float64
	Quantity    int
	DateCreated time.Time
	DateUpdated time.Time
}

Product represents an individual product.

func TestGenerateSeedProducts

func TestGenerateSeedProducts(n int, api *Core, userID uuid.UUID) ([]Product, error)

TestGenerateSeedProducts is a helper method for testing.

type QueryFilter

type QueryFilter struct {
	ID       *uuid.UUID
	Name     *string `validate:"omitempty,min=3"`
	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.

func (*QueryFilter) Validate

func (qf *QueryFilter) Validate() error

Validate can perform a check of the data against the validate tags.

func (*QueryFilter) WithCost

func (qf *QueryFilter) WithCost(cost float64)

WithCost sets the Cost field of the QueryFilter value.

func (*QueryFilter) WithName

func (qf *QueryFilter) WithName(name string)

WithName sets the Name field of the QueryFilter value.

func (*QueryFilter) WithProductID

func (qf *QueryFilter) WithProductID(productID uuid.UUID)

WithProductID sets the ID field of the QueryFilter value.

func (*QueryFilter) WithQuantity

func (qf *QueryFilter) WithQuantity(quantity int)

WithQuantity sets the Quantity field of the QueryFilter value.

type Storer

type Storer interface {
	ExecuteUnderTransaction(tx transaction.Transaction) (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, pageNumber int, rowsPerPage int) ([]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     *string
	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