Documentation ¶
Index ¶
- Constants
- Variables
- type Core
- func (c *Core) Count(ctx context.Context, filter QueryFilter) (int, error)
- func (c *Core) Create(ctx context.Context, np NewProduct) (Product, error)
- func (c *Core) Delete(ctx context.Context, p Product) error
- func (c *Core) Query(ctx context.Context, filter QueryFilter, orderBy order.By, pageNumber int, ...) ([]Product, error)
- func (c *Core) QueryByID(ctx context.Context, productID uuid.UUID) (Product, error)
- func (c *Core) QueryByUserID(ctx context.Context, userID uuid.UUID) ([]Product, error)
- func (c *Core) Update(ctx context.Context, p Product, up UpdateProduct) (Product, error)
- type NewProduct
- type Product
- type QueryFilter
- type Storer
- type UpdateProduct
Constants ¶
const ( OrderByID = "productid" OrderByName = "name" OrderByCost = "cost" OrderByQuantity = "quantity" OrderBySold = "sold" OrderByRevenue = "revenue" OrderByUserID = "userid" )
Set of fields that the results can be ordered by. These are the names that should be used by the application layer.
Variables ¶
DefaultOrderBy represents the default way we sort.
var (
ErrNotFound = errors.New("product not found")
)
Set of error variables for CRUD operations.
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 (*Core) Create ¶
Create adds a Product to the database. It returns the crated Product with fields like ID and DateCreated populated.
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 from the database.
func (*Core) QueryByUserID ¶
QueryByUserID finds the products for a given user.
type NewProduct ¶
NewProduct is what we require from clients when adding a Product.
type Product ¶
type Product struct { ID uuid.UUID Name string Cost float64 Quantity int Sold int Revenue int UserID uuid.UUID DateCreated time.Time DateUpdated time.Time }
Product represents an individual product.
type QueryFilter ¶
type QueryFilter struct { ID *uuid.UUID `validate:"omitempty"` Name *string `validate:"omitempty,min=3"` Cost *float64 `validate:"omitempty,numeric"` Quantity *int `validate:"omitempty,numeric"` }
QueryFilter holds the available fields a query can be filtered on.
func (*QueryFilter) Validate ¶
func (qf *QueryFilter) Validate() error
Validate checks the data in the model is considered clean.
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 { Create(ctx context.Context, p Product) error Update(ctx context.Context, p Product) error Delete(ctx context.Context, p 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 persist and retrieve data.
type UpdateProduct ¶
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 taht was not provided and a field that was provided as explicitly blank. Otherwise we wouldn't want to use pointers to basic types but we make exceptions around marshalling/unmarshalling.