data

package
v0.0.0-...-e26ef75 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: MIT Imports: 5 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrProductNotFound = fmt.Errorf("Product not found")

ErrProductNotFound is an error raised when a product can not be found in the database

Functions

func AddProduct

func AddProduct(p Product)

AddProduct adds a new product to the database

func FromJSON

func FromJSON(i interface{}, r io.Reader) error

FromJSON deserializes the object from JSON string in an io.Reader to the given interface

func ToJSON

func ToJSON(i interface{}, w io.Writer) error

ToJSON serializes the given interface into a string based JSON format

func UpdateProduct

func UpdateProduct(p Product) error

UpdateProduct replaces a product in the database with the given item. If a product with the given id does not exist in the database this function returns a ProductNotFound error

Types

type Product

type Product struct {
	ID          int     `json:"id"`
	Name        string  `json:"name" validate:"required"`
	Description string  `json:"description"`
	Price       float32 `json:"price" validate:"required,gt=0.1,lt=10"`
	SKU         string  `json:"sku" validate:"sku"`
}

Product defines the structure for an API product

func GetProductByID

func GetProductByID(id int) (*Product, error)

GetProductByID returns a single product which matches the id from the database. If a product is not found this function returns a ProductNotFound error

func (*Product) Validate

func (p *Product) Validate() error

Validate the product

type Products

type Products []*Product

Products defines a slice of Product

func GetProducts

func GetProducts() Products

GetProducts returns all products from the database

Jump to

Keyboard shortcuts

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