data

package
v0.0.0-...-783f409 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeAuthentication = "authentication"
)

Variables

View Source
var (
	ErrRecordNotFound             = errors.New("record not found")
	ErrInvalidCredentials         = errors.New("credentials were not correct")
	ErrEditConflict               = errors.New("edit conflict")
	ErrReferencedUserDoesNotExist = errors.New("user doesn't exist")
	ErrParentRecipeDoesNotExist   = errors.New("parent recipe doesn't exist")
	ErrPantryItemDoesNotExist     = errors.New("pantry item does not exist")
	ErrChildRecipeExists          = errors.New("child recipe exists")
	ErrRecipeDoesNotExist         = errors.New("recipe does not exists")
)
View Source
var AnonymousUser = &User{}
View Source
var (
	ErrDuplicateEmail = errors.New("duplicate email")
)
View Source
var (
	ValidMeasurementUnits = []MeasurementUnit{
		"g",
		"ml",
	}
)

Functions

func GetDefaultTimeoutContext

func GetDefaultTimeoutContext() (context.Context, context.CancelFunc)

func Max

func Max[T Number](a, b T) T

func ValidateComponentConsumableList

func ValidateComponentConsumableList(v *validator.Validator, recipeID int64, recipeComponents []*RecipeComponent, pantryItems []*PantryItem, consumables []*Consumable)

func ValidateConsumable

func ValidateConsumable(v *validator.Validator, consumable *Consumable)

func ValidateConsumed

func ValidateConsumed(v *validator.Validator, consumed *Consumed)

func ValidateEmail

func ValidateEmail(v *validator.Validator, email string)

func ValidateFullRecipe

func ValidateFullRecipe(v *validator.Validator, fullRecipe *FullRecipe)

func ValidateMacroNutrients

func ValidateMacroNutrients(v *validator.Validator, macros Macronutrients)

func ValidateMeasurementUnit

func ValidateMeasurementUnit(v *validator.Validator, consumable *Consumable)

func ValidateMetadataFilters

func ValidateMetadataFilters(v *validator.Validator, f MetadataFilters)

func ValidatePantryItem

func ValidatePantryItem(v *validator.Validator, pantryItem *PantryItem)

func ValidatePasswordPlaintext

func ValidatePasswordPlaintext(v *validator.Validator, password string)

func ValidateRecipe

func ValidateRecipe(v *validator.Validator, recipe *Recipe)

func ValidateRecipeComponent

func ValidateRecipeComponent(v *validator.Validator, recipeComponent *RecipeComponent)

func ValidateTokenPlaintext

func ValidateTokenPlaintext(v *validator.Validator, tokenPlaintext string)

func ValidateUser

func ValidateUser(v *validator.Validator, user *User)

Types

type Consumable

type Consumable struct {
	ID        int64           `json:"id"`
	CreatorID int64           `json:"creator_id"`
	CreatedAt time.Time       `json:"created_at"`
	Name      string          `json:"name"`
	BrandName string          `json:"brand_name"`
	Size      float64         `json:"size"`
	Units     MeasurementUnit `json:"units"`
	Macros    Macronutrients  `json:"macros"`
}

type ConsumableFilters

type ConsumableFilters struct {
	Metadata                     MetadataFilters
	NameSearch                   string
	BrandNameSearch              string
	RequireNameAndBrandNameMatch bool
}

func (ConsumableFilters) GetWhereClauseDelimiter

func (options ConsumableFilters) GetWhereClauseDelimiter() string

type ConsumableModel

type ConsumableModel struct {
	DB *pgxpool.Pool
}

func (ConsumableModel) Delete

func (m ConsumableModel) Delete(ID int64) error

func (ConsumableModel) GetByCreatorID

func (m ConsumableModel) GetByCreatorID(ID int64, filters ConsumableFilters) ([]*Consumable, Metadata, error)

func (ConsumableModel) GetByID

func (m ConsumableModel) GetByID(ID int64) (*Consumable, error)

func (ConsumableModel) Insert

func (m ConsumableModel) Insert(consumable *Consumable) error

func (ConsumableModel) Search

func (m ConsumableModel) Search(filters ConsumableFilters) ([]*Consumable, Metadata, error)

func (ConsumableModel) Update

func (m ConsumableModel) Update(consumable *Consumable) error

type Consumed

type Consumed struct {
	ID           int64          `json:"id"`
	UserID       int64          `json:"user_id"`
	RecipeID     int64          `json:"recipe_id"`
	Quantity     float64        `json:"quantity"`
	Macros       Macronutrients `json:"macros"`
	ConsumedAt   time.Time      `json:"consumed_at"`
	CreatedAt    time.Time      `json:"created_at"`
	LastEditedAt time.Time      `json:"last_edited_at"`
	Notes        string         `json:"notes"`
}

type ConsumedModel

type ConsumedModel struct {
	DB *pgxpool.Pool
}

func (ConsumedModel) Delete

func (m ConsumedModel) Delete(ID int64, userID int64) error

func (ConsumedModel) GetAllByUserID

func (m ConsumedModel) GetAllByUserID(userID int64) ([]*Consumed, error)

func (ConsumedModel) GetAllByUserIDAndDate

func (m ConsumedModel) GetAllByUserIDAndDate(userID int64, from time.Time, to time.Time) ([]*Consumed, error)

func (ConsumedModel) GetByConsumedID

func (m ConsumedModel) GetByConsumedID(ConsumedID int64) (*Consumed, error)

func (ConsumedModel) Insert

func (m ConsumedModel) Insert(consumed *Consumed) error

func (ConsumedModel) Update

func (m ConsumedModel) Update(consumed *Consumed) error

type FullRecipe

type FullRecipe struct {
	Recipe           Recipe
	RecipeComponents []*RecipeComponent
	PantryItems      []*PantryItem
	Consumables      []*Consumable
}

type IConsumableModel

type IConsumableModel interface {
	GetByID(int64) (*Consumable, error)
	GetByCreatorID(int64, ConsumableFilters) ([]*Consumable, Metadata, error)
	Search(ConsumableFilters) ([]*Consumable, Metadata, error)
	Insert(*Consumable) error
	Update(*Consumable) error
	Delete(int64) error
}

type IConsumedModel

type IConsumedModel interface {
	GetByConsumedID(int64) (*Consumed, error)
	GetAllByUserID(int64) ([]*Consumed, error)
	GetAllByUserIDAndDate(int64, time.Time, time.Time) ([]*Consumed, error)
	Insert(*Consumed) error
	Update(*Consumed) error
	Delete(int64, int64) error
}

type IPantryItemModel

type IPantryItemModel interface {
	GetAllByUserID(int64) ([]*PantryItem, error)
	Get(int64) (*PantryItem, error)
	Create(*PantryItem) error
	Update(*PantryItem) error
	Delete(int64, int64) error
}

type IRecipeComponentModel

type IRecipeComponentModel interface {
	Get(int64) (*RecipeComponent, error)
	Insert(*RecipeComponent) error
	Update(*RecipeComponent, int64) error
}

type IRecipeModel

type IRecipeModel interface {
	Get(int64) (*Recipe, error)
	GetByCreatorID(int64, RecipeFilters) ([]*Recipe, Metadata, error)
	GetLatestByCreatorID(int64, RecipeFilters) ([]*Recipe, Metadata, error)
	GetFullRecipe(int64, int64) (*FullRecipe, error)
	Insert(*Recipe) error
	InsertFullRecipe(*FullRecipe) error
	Update(*Recipe) error
	UpdateFullRecipe(*FullRecipe) error
	Delete(int64) error
	GetParentRecipe(*Recipe) (*Recipe, error)
	GetAllAncestors(*Recipe, RecipeFilters) ([]*Recipe, Metadata, error)
}

type ITokenModel

type ITokenModel interface {
	New(int64, time.Duration, string) (*Token, error)
	Insert(*Token) error
	DeleteAllForUser(string, int64) error
}

type IUserModel

type IUserModel interface {
	Insert(*User) error
	Exists(int) (bool, error)
	GetByEmail(string) (*User, error)
	Update(*User) error
	GetForToken(string, string) (*User, error)
}

type Macronutrients

type Macronutrients struct {
	Carbs    float64 `json:"carbs"`
	Fats     float64 `json:"fats"`
	Proteins float64 `json:"proteins"`
	Alcohol  float64 `json:"alcohol"`
}

func (*Macronutrients) CalculateKJ

func (macros *Macronutrients) CalculateKJ() float64

type MeasurementUnit

type MeasurementUnit string

type Metadata

type Metadata struct {
	CurrentPage  int `json:"current_page,omitempty"`
	PageSize     int `json:"page_size,omitempty"`
	FirstPage    int `json:"first_page,omitempty"`
	LastPage     int `json:"last_page,omitempty"`
	TotalRecords int `json:"total_records,omitempty"`
}

type MetadataFilters

type MetadataFilters struct {
	Page         int
	PageSize     int
	Sort         string
	SortSafeList []string
}

type Models

type Models struct {
	Users            IUserModel
	Tokens           ITokenModel
	Consumed         IConsumedModel
	Consumables      IConsumableModel
	Recipes          IRecipeModel
	RecipeComponents IRecipeComponentModel
	PantryItems      IPantryItemModel
}

func NewModel

func NewModel(db *pgxpool.Pool) Models

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

type PantryItem

type PantryItem struct {
	ID           int64     `json:"id"`
	UserID       int64     `json:"user_id"`
	ConsumableId int64     `json:"consumable_id"`
	Name         string    `json:"name"`
	CreatedAt    time.Time `json:"created_at"`
	LastEditedAt time.Time `json:"last_edited_at"`
}

type PantryItemModel

type PantryItemModel struct {
	DB *pgxpool.Pool
}

func (PantryItemModel) Create

func (m PantryItemModel) Create(pantryItem *PantryItem) error

func (PantryItemModel) Delete

func (m PantryItemModel) Delete(ID int64, userID int64) error

func (PantryItemModel) Get

func (m PantryItemModel) Get(ID int64) (*PantryItem, error)

func (PantryItemModel) GetAllByUserID

func (m PantryItemModel) GetAllByUserID(userID int64) ([]*PantryItem, error)

func (PantryItemModel) Update

func (m PantryItemModel) Update(pantryItem *PantryItem) error

type Recipe

type Recipe struct {
	ID             int64     `json:"id"`
	Name           string    `json:"recipe_name"`
	CreatorID      int64     `json:"creator_id"`
	CreatedAt      time.Time `json:"created_at"`
	LastEditedAt   time.Time `json:"last_edited_at"`
	Notes          string    `json:"notes"`
	ParentRecipeID int64     `json:"parent_recipe_id"`
	IsLatest       bool      `json:"is_latest"`
}

type RecipeComponent

type RecipeComponent struct {
	ID              int64     `json:"id"`
	RecipeID        int64     `json:"recipe_id"`
	PantryItemID    int64     `json:"pantry_item_id"`
	CreatedAt       time.Time `json:"created_at"`
	Quantity        int64     `json:"quantity"`
	StepNo          int64     `json:"step_no"`
	StepDescription string    `json:"step_description"`
}

type RecipeComponentModel

type RecipeComponentModel struct {
	DB *pgxpool.Pool
}

func (RecipeComponentModel) Get

func (RecipeComponentModel) Insert

func (m RecipeComponentModel) Insert(recipeComponent *RecipeComponent) error

func (RecipeComponentModel) Update

func (m RecipeComponentModel) Update(recipeComponent *RecipeComponent, userID int64) error

type RecipeFilters

type RecipeFilters struct {
	Metadata   MetadataFilters
	NameSearch string
}

type RecipeModel

type RecipeModel struct {
	DB *pgxpool.Pool
}

func (RecipeModel) Delete

func (m RecipeModel) Delete(ID int64) error

func (RecipeModel) Get

func (m RecipeModel) Get(ID int64) (*Recipe, error)

func (RecipeModel) GetAllAncestors

func (m RecipeModel) GetAllAncestors(childRecipe *Recipe, filters RecipeFilters) ([]*Recipe, Metadata, error)

func (RecipeModel) GetByCreatorID

func (m RecipeModel) GetByCreatorID(ID int64, filters RecipeFilters) ([]*Recipe, Metadata, error)

func (RecipeModel) GetFullRecipe

func (m RecipeModel) GetFullRecipe(ID int64, userID int64) (*FullRecipe, error)

func (RecipeModel) GetLatestByCreatorID

func (m RecipeModel) GetLatestByCreatorID(ID int64, filters RecipeFilters) ([]*Recipe, Metadata, error)

func (RecipeModel) GetParentRecipe

func (m RecipeModel) GetParentRecipe(childRecipe *Recipe) (*Recipe, error)

func (RecipeModel) Insert

func (m RecipeModel) Insert(recipe *Recipe) error

func (RecipeModel) InsertFullRecipe

func (m RecipeModel) InsertFullRecipe(fullRecipe *FullRecipe) error

func (RecipeModel) Update

func (m RecipeModel) Update(recipe *Recipe) error

func (RecipeModel) UpdateFullRecipe

func (m RecipeModel) UpdateFullRecipe(fullRecipe *FullRecipe) error

not good name, maybe createChildOfFullRecipe()

type Token

type Token struct {
	Plaintext string    `json:"token"`
	Hash      []byte    `json:"-"`
	UserID    int64     `json:"-"`
	Expiry    time.Time `json:"expiry"`
	Scope     string    `json:"-"`
}

type TokenModel

type TokenModel struct {
	DB *pgxpool.Pool
}

func (TokenModel) DeleteAllForUser

func (m TokenModel) DeleteAllForUser(scope string, userID int64) error

func (TokenModel) Insert

func (m TokenModel) Insert(token *Token) error

func (TokenModel) New

func (m TokenModel) New(userID int64, ttl time.Duration, scope string) (*Token, error)

type User

type User struct {
	ID        int64     `json:"id"`
	CreatedAt time.Time `json:"created_at"`
	Username  string    `json:"username"`
	Email     string    `json:"email"`
	Password  password  `json:"-"`
	Version   int       `json:"-"`
}

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

type UserModel

type UserModel struct {
	DB *pgxpool.Pool
}

func (UserModel) Authenticate

func (m UserModel) Authenticate(email string, password string) (*User, error)

func (UserModel) Exists

func (m UserModel) Exists(id int) (bool, error)

func (UserModel) GetByEmail

func (m UserModel) GetByEmail(email string) (*User, error)

func (UserModel) GetForToken

func (m UserModel) GetForToken(tokenScope string, tokenPlaintext string) (*User, error)

func (UserModel) Insert

func (m UserModel) Insert(user *User) error

func (UserModel) Update

func (m UserModel) Update(user *User) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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