db

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: 6 Imported by: 0

Documentation

Overview

Package db contains product related CRUD functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Product

type Product struct {
	ID          string    `db:"product_id"`   // Unique identifier.
	Name        string    `db:"name"`         // Display name of the product.
	Cost        int       `db:"cost"`         // Price for one item in cents.
	Quantity    int       `db:"quantity"`     // Original number of items available.
	Sold        int       `db:"sold"`         // Aggregate field showing number of items sold.
	Revenue     int       `db:"revenue"`      // Aggregate field showing total cost of sold items.
	UserID      string    `db:"user_id"`      // ID of the user who created the product.
	DateCreated time.Time `db:"date_created"` // When the product was added.
	DateUpdated time.Time `db:"date_updated"` // When the product record was last modified.
}

Product represents an individual product.

type Store

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

Store manages the set of APIs for user access.

func NewStore

func NewStore(log *zap.SugaredLogger, db *sqlx.DB) Store

NewStore constructs a data for api access.

func (Store) Create

func (s Store) Create(ctx context.Context, prd Product) error

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

func (Store) Delete

func (s Store) Delete(ctx context.Context, productID string) error

Delete removes the product identified by a given ID.

func (Store) Query

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

Query gets all Products from the database.

func (Store) QueryByID

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

QueryByID finds the product identified by a given ID.

func (Store) QueryByUserID

func (s Store) QueryByUserID(ctx context.Context, userID string) ([]Product, error)

QueryByUserID finds the product identified by a given User ID.

func (Store) Tran

func (s Store) Tran(tx sqlx.ExtContext) Store

Tran return new Store with transaction in it.

func (Store) Update

func (s Store) Update(ctx context.Context, prd Product) error

Update modifies data about a Product. It will error if the specified ID is invalid or does not reference an existing Product.

func (Store) WithinTran

func (s Store) WithinTran(ctx context.Context, fn func(sqlx.ExtContext) error) error

WithinTran runs passed function and do commit/rollback at the end.

Jump to

Keyboard shortcuts

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