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 ¶
- Variables
- type Core
- func (c Core) Create(ctx context.Context, np NewProduct, now time.Time) (Product, error)
- func (c Core) Delete(ctx context.Context, productID string) error
- func (c Core) Query(ctx context.Context, pageNumber int, rowsPerPage int) ([]Product, error)
- func (c Core) QueryByID(ctx context.Context, productID string) (Product, error)
- func (c Core) QueryByUserID(ctx context.Context, userID string) ([]Product, error)
- func (c Core) Update(ctx context.Context, productID string, up UpdateProduct, now time.Time) error
- type NewProduct
- type Product
- type UpdateProduct
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("product not found") ErrInvalidID = errors.New("ID is not in its proper form") )
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 API's for product access.
func NewCore ¶
func NewCore(log *zap.SugaredLogger, sqlxDB *sqlx.DB) Core
NewCore constructs a core for product api access.
func (Core) Create ¶
Create adds a Product to the database. It returns the created Product with fields like ID and DateCreated populated.
func (Core) QueryByUserID ¶
QueryByUserID finds the products identified by a given User ID.
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"` UserID string `json:"user_id" validate:"required"` }
NewProduct is what we require from clients when adding a Product.
type Product ¶
type Product struct { ID string `json:"id"` // Unique identifier. Name string `json:"name"` // Display name of the product. Cost int `json:"cost"` // Price for one item in cents. Quantity int `json:"quantity"` // Original number of items available. Sold int `json:"sold"` // Aggregate field showing number of items sold. Revenue int `json:"revenue"` // Aggregate field showing total cost of sold items. UserID string `json:"user_id"` // ID of the user who created the product. DateCreated time.Time `json:"date_created"` // When the product was added. DateUpdated time.Time `json:"date_updated"` // When the product record was last modified. }
Product represents an individual product.
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.