types

package
v0.0.0-...-d62253c Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package types implements all types used in the Hoffman server

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTagNameEmpty     = errors.New("tag name is empty")
	ErrTagHasColor      = errors.New("tag has valid color")
	ErrTagInvalidCollor = errors.New("tag has invalid color")
)
View Source
var (
	ErrDuplicateUniqueIdentifier = errors.New("duplicate unique identifier")
)

Datasource global errors

View Source
var (
	ErrIDNotFound = errors.New("identifer not found")
)
View Source
var (
	ErrInvalidSearch = errors.New("invalid search")
)

Functions

This section is empty.

Types

type Connect

type Connect func(string) (Datasource, error)

Connect creates a new connection to the data source located at string. If the data source has not been initialized, it initializes it to be able to persist the data.

type Datasource

type Datasource interface {

	// Disconnect severs the connection to the data source.
	// After disconnection, the data source cannot be used to correctly
	// fulfil any actions requested.
	Disconnect() error

	// InsertProduct creates a new entry in the product segment of the
	// data source persistant storage. Products have a unique ID, set
	// by the data source, and a unique EAN code. Duplicates are
	// rejected. Product taggings are also saved.
	// InsertProduct also updates the ID of the product to match the
	// unique ID attributed.
	InsertProduct(*Product) error

	// SelectAllProducts returns all products from the product segment
	// of the data source persistant storage. If there are none, returns
	// an empty array of products and a nil error.
	SelectAllProducts() ([]*Product, error)

	// SelectProductById returns the only product with that ID from
	// the product segment of the data source persistant storage.
	// If the ID does not exist, SelectProductById returns an empty
	// product (without any values) and a nil error.
	SelectProductById(uint) (*Product, error)

	// SelectProductLike returns all possible products from the product
	// segment of the data source persistant storage matching the
	// search. If there are none, returns an empty array of products and
	// a nil error.
	SelectProductsLike(*ProductSearch) ([]*Product, error)

	// UpdateProduct updates the product in the product segment of the
	// data source persistant storage to match the passed product.
	// This means that taggings also have to be updated. The updated
	// product is then returned.
	// The ID can never be updated.
	UpdateProduct(*Product) (*Product, error)

	// DeleteProduct completely removes the product from the product
	// segment of the data source persistant storage. This means that
	// taggings are also deleted. It returns the deleted product in
	// full afterwards.
	DeleteProduct(uint) (*Product, error)

	// InsertProuctTag creates a new entry in the product tag segment
	// of the data source persistant storage. Product tags have a
	// unique ID, set by the data source, and name. Duplicates are
	// rejected.
	// InsertProductTag also updates the ID of the tag to match the
	// unique ID attributed.
	InsertProductTag(*Tag) error

	// SelectAllProductTags returns all possible product tags from the
	// product tag segment of the data source persistant storage. If
	// there are none, returns an empty array of produt tags.
	SelectAllProductTags() ([]*Tag, error)

	// SelectProductTagById returns the only product tag with that ID
	// from the product tag segment of the data source persistant
	// storage. If the ID does not exist, returns an emtpy product tag
	// and a nil error.
	SelectProductTagById(uint) (*Tag, error)

	// SelectProductTagByName returns the only product tag with that
	// name from the product tag segment of the data source persistant
	// storage. If none are found, returns an emtpy product tag and a
	// nil error.
	SelectProductTagByName(string) (*Tag, error)

	// SelectProductTagsLikeName returns all possible product tags
	// matching the name. If there are none, returns an empty array
	// of product tags and no error.
	SelectProductTagsLikeName(string) ([]*Tag, error)

	// UpdateProductTag updates the product tag in the product segment
	// of the data source peristant storage to match the passed product
	// tag.
	// The ID can never be updated.
	// Product tags have unique names. Duplicates are expected to be
	// rejected.
	UpdateProductTag(*Tag) (*Tag, error)

	// DeleteProductTag completely removes the product tag from the
	// product tag segment of the data source persistant storage.
	// This means that taggings are also deleted. After that, it
	// returns the deleted tag.
	DeleteProductTag(uint) (*Tag, error)
}

A Datasource is the connector to the real persistant data and the server.

Every function is expected to interact with the persistant storage, which is considered the single source of truth for the entire system. Therefor, it is primordial that it ensures ACID properties are always verifed. These functions are moslty straight forward low-level interactions, however some are more high-level, requiering transactions to keep ACID properties.

The Datasource MUST be able to perform full CRUD actions for all types in this types package.

type Location

type Location struct {
	Id      uint   `json:"id"`
	Name    string `json:"name"`
	Fridge  bool   `json:"fridge"`
	Freezer bool   `json:"freezer"`
	Tags    []Tag  `json:"tags"`
}

func (*Location) Valid

func (l *Location) Valid() bool

func (*Location) ValidKind

func (l *Location) ValidKind() bool

func (*Location) ValidName

func (l *Location) ValidName() bool

type LogicProvider

type LogicProvider interface {

	// CreateProduct creates a new product in the data source
	// using the passed product. The product ID is updated to
	// match the one assigned in the data source and the tags
	// are the full tags assigned.
	// TODO: Populate product with OFF data.
	CreateProduct(*Product) error

	// GetAllProducts returns all products saved in the data
	// source.
	GetAllProducts() ([]*Product, error)

	// GetProductByID returns the product saved in the data
	// source matching the ID. If no product matches,
	// GetProductByID returns an empty product and ErrIDNotFound.
	GetProductByID(uint) (*Product, error)

	// GetProductsLike returns all products saved in the data
	// source matching the requested search. If no product
	// matches, GetProductsLike returns an empty array of
	// products and a nil error.
	GetProductsLike(*ProductSearch) ([]*Product, error)

	// UpdateProduct checks if the product ID exists. If
	// it does, it updates the product with the ID to match
	// the data passed. Otherwise, it returns ErrIDNotFound.
	UpdateProduct(*Product) (*Product, error)

	// DeleteProduct checks if the product ID exists. If it
	// does, it requests deletion of the product to the data
	// source and returns the deleted product. Otherwise, it
	// returns an empty produt and ErrIDNotFound.
	DeleteProduct(uint) (*Product, error)

	// CreateProductTag creates a new product tag in the data
	// source using the passed tag. The product tag ID is
	// updated to match the one assigned in the data source.
	CreateProductTag(*Tag) error

	// GetAllProductTags returns all product tags saved in
	// the data source.
	GetAllProductTags() ([]*Tag, error)

	// GetProductTagByID and GetProductTagByName return the
	// product tag matching the ID and name respectively.
	// If no product tag matches, they both return an empty
	// product tag and ErrIDNotFound.
	GetProductTagByID(uint) (*Tag, error)
	GetProductTagByName(string) (*Tag, error)

	// GetProductTagsLikeName returns all product tags
	// matching the requested name. If no product tags match,
	// GetProductTagsLikeName returns an empty array of
	// product tags and a nil error.
	GetProductTagsLikeName(string) ([]*Tag, error)

	// UpdateProductTag checks if the product tag ID exists.
	// If it does, it updates the product tag with the ID to
	// match the data passed. Otherwise, it returns
	// ErrIDNotFound.
	UpdateProductTag(*Tag) (*Tag, error)

	// DeleteProductTag checks if the product tag ID exists.
	// If it does, it requests deletion of the product tag
	// to the data source and returns the deleted product tag.
	// Otherwise, it returns an empty product tag and
	// ErrIDNotFound.
	DeleteProductTag(uint) (*Tag, error)
}

A LogicProvidier is the part of the server that implements the logic of the application.

type Product

type Product struct {
	Id   uint   `json:"id"`
	EAN  uint   `json:"ean"`
	Name string `json:"name"`
	TTL  TTL    `json:"ttl"`
	Tags []Tag  `json:"tags"`
}

func (*Product) Valid

func (p *Product) Valid() bool

func (*Product) ValidName

func (p *Product) ValidName() bool

type ProductSearch

type ProductSearch struct {
	Ean      string
	Names    []string
	TagIds   []uint
	Oprators ProductSearchOperators
}

func (*ProductSearch) BuildNamesSearch

func (ps *ProductSearch) BuildNamesSearch() string

func (*ProductSearch) BuildSearch

func (ps *ProductSearch) BuildSearch() string

func (*ProductSearch) BuildTagSearch

func (ps *ProductSearch) BuildTagSearch() string

func (*ProductSearch) Valid

func (ps *ProductSearch) Valid() bool

type ProductSearchOperators

type ProductSearchOperators struct {
	Master string
	Names  string
	TagIds string
}

func (*ProductSearchOperators) FillDefaults

func (pso *ProductSearchOperators) FillDefaults()

func (*ProductSearchOperators) Valid

func (pso *ProductSearchOperators) Valid() bool

func (*ProductSearchOperators) ValidOperators

func (pso *ProductSearchOperators) ValidOperators() bool

type TTL

type TTL struct {
	Normal  uint `json:"normal"`
	Fridge  uint `json:"fridge"`
	Freezer uint `json:"freezer"`
}

func (*TTL) Valid

func (t *TTL) Valid() bool

func (*TTL) ValidTTL

func (t *TTL) ValidTTL() bool

type Tag

type Tag struct {
	Id    uint   `json:"id"`
	Name  string `json:"name"`
	Color uint   `json:"color"`
}

A Tag is a simple structure used to categorize products. Tag names are unique. Tag colors are base 10 RGB notation.

func (*Tag) GenerateColor

func (t *Tag) GenerateColor() error

GenerateColor generates a random tag color using the tag's name's SHA1.

func (*Tag) RGB

func (t *Tag) RGB() ([]uint, error)

RGB converts the base 10 RGB color in an array of color values.

func (*Tag) Valid

func (t *Tag) Valid() bool

Valid verifies that the tag is valid. A valid tag has a valid color and a name.

func (*Tag) ValidColor

func (t *Tag) ValidColor() bool

ValidColor verifies that the tag's color is valid.

func (*Tag) ValidName

func (t *Tag) ValidName() bool

ValidName verifies that the tag has a name. Uniqueness verification is the data source's job.

type UseDatasource

type UseDatasource func(*Datasource) *LogicProvider

UseDatasource creates a LogicProvider bound to the passed data source, used to persist data.

Jump to

Keyboard shortcuts

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