product

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package product contains product related CRUD functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

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 Store added in v0.3.3

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

Store manages the set of API's for product access.

func NewStore added in v0.3.3

func NewStore(log *log.Logger, db *database.DB) Store

NewStore constructs a Store for api access.

func (Store) Create added in v0.3.3

func (s Store) 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 (Store) Delete added in v0.3.3

func (s Store) Delete(ctx context.Context, traceID string, claims auth.Claims, productID string) error

Delete removes the product identified by a given ID.

func (Store) Query added in v0.3.3

func (s Store) Query(ctx context.Context, traceID string, pageNumber int, rowsPerPage int) ([]Info, error)

Query gets all Products from the database.

func (Store) QueryByID added in v0.3.3

func (s Store) QueryByID(ctx context.Context, traceID string, productID string) (Info, error)

QueryByID finds the product identified by a given ID.

func (Store) Update added in v0.3.3

func (s Store) Update(ctx context.Context, traceID string, claims auth.Claims, 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 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.

Jump to

Keyboard shortcuts

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