product

package
v0.0.0-...-0044c33 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 10 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

This section is empty.

Variables

View Source
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 APIs 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

func (c Core) Create(ctx context.Context, np NewProduct, now time.Time) (Product, error)

Create adds a Product to the database. It returns the created Product with fields like ID and DateCreated populated.

func (Core) Delete

func (c Core) Delete(ctx context.Context, productID string) error

Delete removes the product identified by a given ID.

func (Core) Query

func (c Core) Query(ctx context.Context, pageNumber int, rowsPerPage int) ([]Product, error)

Query gets all Products from the database.

func (Core) QueryByID

func (c Core) QueryByID(ctx context.Context, productID string) (Product, error)

QueryByID finds the product identified by a given ID.

func (Core) QueryByUserID

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

QueryByUserID finds the products identified by a given User ID.

func (Core) Update

func (c Core) Update(ctx context.Context, productID string, up UpdateProduct, now time.Time) error

Update modifies data about a Product. It will error if the specified ID is invalid or does not reference an existing 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"`
	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.

Directories

Path Synopsis
Package db contains product related CRUD functionality.
Package db contains product related CRUD functionality.

Jump to

Keyboard shortcuts

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