Documentation ¶
Overview ¶
Package product contains product related CRUD functionality.
Index ¶
- Variables
- type Info
- type NewProduct
- type Product
- func (p Product) Create(ctx context.Context, traceID string, claims auth.Claims, np NewProduct, ...) (Info, error)
- func (p Product) Delete(ctx context.Context, traceID string, productID string) error
- func (p Product) Query(ctx context.Context, traceID string, pageNumber int, rowsPerPage int) ([]Info, error)
- func (p Product) QueryByID(ctx context.Context, traceID string, productID string) (Info, error)
- func (p Product) Update(ctx context.Context, traceID string, claims auth.Claims, productID string, ...) error
- type UpdateProduct
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is used when a specific Product is requested but does not exist. ErrNotFound = errors.New("not found") // ErrInvalidID occurs when an ID is not in a valid form. ErrInvalidID = errors.New("ID is not in its proper form") // ErrForbidden occurs when a user tries to do something that is forbidden to them according to our access control policies. ErrForbidden = errors.New("attempted action is not allowed") )
Functions ¶
This section is empty.
Types ¶
type Info ¶
type Info struct { ID string `db:"product_id" json:"id"` // Unique identifier. Name string `db:"name" json:"name"` // Display name of the product. Cost int `db:"cost" json:"cost"` // Price for one item in cents. Quantity int `db:"quantity" json:"quantity"` // Original number of items available. Sold int `db:"sold" json:"sold"` // Aggregate field showing number of items sold. Revenue int `db:"revenue" json:"revenue"` // Aggregate field showing total cost of sold items. UserID string `db:"user_id" json:"user_id"` // ID of the user who created the product. DateCreated time.Time `db:"date_created" json:"date_created"` // When the product was added. DateUpdated time.Time `db:"date_updated" json:"date_updated"` // When the product record was last modified. }
Info represents an individual product.
type NewProduct ¶
type NewProduct struct { Name string `json:"name" validate:"required"` Cost int `json:"cost" validate:"required,gte=0"` Quantity int `json:"quantity" validate:"gte=1"` }
NewProduct is what we require from clients when adding a Product.
type Product ¶
type Product struct {
// contains filtered or unexported fields
}
Product manages the set of API's for product access.
func (Product) Create ¶
func (p Product) Create(ctx context.Context, traceID string, claims auth.Claims, np NewProduct, now time.Time) (Info, error)
Create adds a Product to the database. It returns the created Product with fields like ID and DateCreated populated.
func (Product) Query ¶
func (p Product) Query(ctx context.Context, traceID string, pageNumber int, rowsPerPage int) ([]Info, error)
Query gets all Products from the database.
type UpdateProduct ¶
type UpdateProduct struct { Name *string `json:"name"` Cost *int `json:"cost" validate:"omitempty,gte=0"` Quantity *int `json:"quantity" validate:"omitempty,gte=1"` }
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.