storage

package
v0.0.0-...-d5587fd Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const StorageOperationTimeout = 15 * time.Second

Variables

View Source
var (
	// Food
	ErrFoodNotFound  = errors.New("food not found")
	ErrFoodInvalid   = errors.New("invalid food")
	ErrFoodEmptyList = errors.New("empty food list")
	ErrFoodIsUsed    = errors.New("food is used")

	// Bundle
	ErrBundleInvalid           = errors.New("invalid bundle")
	ErrBundleNotFound          = errors.New("bundle not found")
	ErrBundleEmptyList         = errors.New("empty bundle list")
	ErrBundleDepBundleNotFound = errors.New("dependent bundle not found")
	ErrBundleDepFoodNotFound   = errors.New("dependent food not found")
	ErrBundleDepRecursive      = errors.New("dependent recursive bundle not allowed")
	ErrBundleIsUsed            = errors.New("bundle is used")

	// Journal
	ErrJournalInvalid         = errors.New("journal invalid")
	ErrJournalMealReportEmpty = errors.New("empty journal meal report")
	ErrJournalReportEmpty     = errors.New("empty journal report")
	ErrJournalStatsEmpty      = errors.New("empty journal stats")
	ErrJournalInvalidFood     = errors.New("journal invalid food")
	ErrCopyToNotEmpty         = errors.New("copy destination not empty")

	// Weight
	ErrWeightNotFound  = errors.New("weight not found")
	ErrWeightInvalid   = errors.New("invalid weight")
	ErrWeightEmptyList = errors.New("empty weight list")

	// UserSettings
	ErrUserSettingsNotFound = errors.New("user settings not found")
	ErrUserSettingsInvalid  = errors.New("invalid user settings")

	// Activity
	ErrActivityNotFound  = errors.New("activity not found")
	ErrActivityInvalid   = errors.New("activity invalid")
	ErrActivityEmptyList = errors.New("activity empty list")
)

Functions

func WithDebug

func WithDebug() func(*StorageSQLite)

Types

type Activity

type Activity struct {
	Timestamp time.Time
	ActiveCal float64
}

func (*Activity) Validate

func (r *Activity) Validate() bool

type Backup

type Backup struct {
	Timestamp    int64                `json:"timestamp"`
	Weight       []WeightBackup       `json:"weight"`
	Food         []FoodBackup         `json:"food"`
	Journal      []JournalBackup      `json:"journal"`
	Bundle       []BundleBackup       `json:"bundle"`
	UserSettings []UserSettingsBackup `json:"user_settings"`
}

type Bundle

type Bundle struct {
	Key string
	// Map of bundle data
	// Variants:
	// if food: food_key -> weight > 0
	// if bundle: bundle_key -> 0
	Data map[string]float64
}

func (*Bundle) Validate

func (r *Bundle) Validate() bool

type BundleBackup

type BundleBackup struct {
	UserID int64              `json:"user_id"`
	Key    string             `json:"key"`
	Data   map[string]float64 `json:"data"`
}

type Food

type Food struct {
	Key     string
	Name    string
	Brand   string
	Cal100  float64
	Prot100 float64
	Fat100  float64
	Carb100 float64
	Comment string
}

func (*Food) Validate

func (r *Food) Validate() bool

type FoodBackup

type FoodBackup struct {
	Key     string  `json:"key"`
	Name    string  `json:"name"`
	Brand   string  `json:"brand"`
	Cal100  float64 `json:"cal100"`
	Prot100 float64 `json:"prot100"`
	Fat100  float64 `json:"fat100"`
	Carb100 float64 `json:"carb100"`
	Comment string  `json:"comment"`
}

type Journal

type Journal struct {
	Timestamp  time.Time
	Meal       Meal
	FoodKey    string
	FoodWeight float64
}

func (*Journal) Validate

func (r *Journal) Validate() bool

type JournalBackup

type JournalBackup struct {
	UserID     int64   `json:"user_id"`
	Timestamp  int64   `json:"timestamp"`
	Meal       int64   `json:"meal"`
	FoodKey    string  `json:"food_key"`
	FoodWeight float64 `json:"food_weight"`
}

type JournalMealItem

type JournalMealItem struct {
	Timestamp  time.Time
	FoodKey    string
	FoodName   string
	FoodBrand  string
	FoodWeight float64
	Cal        float64
}

type JournalMealReport

type JournalMealReport struct {
	ConsumedDayCal  float64
	ConsumedMealCal float64
	Items           []JournalMealItem
}

type JournalReport

type JournalReport struct {
	Timestamp  time.Time
	Meal       Meal
	FoodKey    string
	FoodName   string
	FoodBrand  string
	FoodWeight float64
	Cal        float64
	Prot       float64
	Fat        float64
	Carb       float64
}

type JournalStats

type JournalStats struct {
	Timestamp time.Time
	TotalCal  float64
	TotalProt float64
	TotalFat  float64
	TotalCarb float64
}

type Meal

type Meal int64

func NewMealFromString

func NewMealFromString(m string) Meal

func (Meal) ToString

func (r Meal) ToString() string

type Storage

type Storage interface {
	// Food
	GetFood(ctx context.Context, key string) (*Food, error)
	SetFood(ctx context.Context, food *Food) error
	SetFoodComment(ctx context.Context, key, comment string) error
	GetFoodList(ctx context.Context) ([]Food, error)
	FindFood(ctx context.Context, pattern string) ([]Food, error)
	DeleteFood(ctx context.Context, key string) error

	// Bundle
	SetBundle(ctx context.Context, userID int64, bndl *Bundle) error
	GetBundle(ctx context.Context, userID int64, key string) (*Bundle, error)
	GetBundleList(ctx context.Context, userID int64) ([]Bundle, error)
	DeleteBundle(ctx context.Context, userID int64, key string) error

	// Weight
	GetWeightList(ctx context.Context, userID int64, from, to time.Time) ([]Weight, error)
	SetWeight(ctx context.Context, userID int64, weight *Weight) error
	DeleteWeight(ctx context.Context, userID int64, timestamp time.Time) error

	// Journal
	SetJournal(ctx context.Context, userID int64, journal *Journal) error
	SetJournalBundle(ctx context.Context, userID int64, timestamp time.Time, meal Meal, bndlKey string) error
	DeleteJournal(ctx context.Context, userID int64, timestamp time.Time, meal Meal, foodkey string) error
	DeleteJournalMeal(ctx context.Context, userID int64, timestamp time.Time, meal Meal) error
	GetJournalMealReport(ctx context.Context, userID int64, timestamp time.Time, meal Meal) (*JournalMealReport, error)
	GetJournalReport(ctx context.Context, userID int64, from, to time.Time) ([]JournalReport, error)
	GetJournalStats(ctx context.Context, userID int64, from, to time.Time) ([]JournalStats, error)
	CopyJournal(ctx context.Context, userID int64, from time.Time, mealFrom Meal, to time.Time, mealTo Meal) (int, error)
	GetJournalFoodAvgWeight(ctx context.Context, userID int64, from, to time.Time, foodkey string) (float64, error)

	// Activity
	GetActivityList(ctx context.Context, userID int64, from, to time.Time) ([]Activity, error)
	GetActivity(ctx context.Context, userID int64, timestamp time.Time) (*Activity, error)
	SetActivity(ctx context.Context, userID int64, activity *Activity) error
	DeleteActivity(ctx context.Context, userID int64, timestamp time.Time) error

	// UserSettings
	GetUserSettings(ctx context.Context, userID int64) (*UserSettings, error)
	SetUserSettings(ctx context.Context, userID int64, settings *UserSettings) error

	// Backup
	Backup(ctx context.Context) (*Backup, error)

	Close() error
}

type StorageSQLite

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

func NewStorageSQLite

func NewStorageSQLite(dbFilePath string, opts ...func(*StorageSQLite)) (*StorageSQLite, error)

func (*StorageSQLite) Backup

func (r *StorageSQLite) Backup(ctx context.Context) (*Backup, error)

func (*StorageSQLite) Close

func (r *StorageSQLite) Close() error

func (*StorageSQLite) CopyJournal

func (r *StorageSQLite) CopyJournal(ctx context.Context, userID int64, from time.Time, mealFrom Meal, to time.Time, mealTo Meal) (int, error)

func (*StorageSQLite) DeleteActivity

func (r *StorageSQLite) DeleteActivity(ctx context.Context, userID int64, timestamp time.Time) error

func (*StorageSQLite) DeleteBundle

func (r *StorageSQLite) DeleteBundle(ctx context.Context, userID int64, key string) error

func (*StorageSQLite) DeleteFood

func (r *StorageSQLite) DeleteFood(ctx context.Context, key string) error

func (*StorageSQLite) DeleteJournal

func (r *StorageSQLite) DeleteJournal(ctx context.Context, userID int64, timestamp time.Time, meal Meal, foodkey string) error

func (*StorageSQLite) DeleteJournalMeal

func (r *StorageSQLite) DeleteJournalMeal(ctx context.Context, userID int64, timestamp time.Time, meal Meal) error

func (*StorageSQLite) DeleteWeight

func (r *StorageSQLite) DeleteWeight(ctx context.Context, userID int64, timestamp time.Time) error

func (*StorageSQLite) FindFood

func (r *StorageSQLite) FindFood(ctx context.Context, pattern string) ([]Food, error)

func (*StorageSQLite) GetActivity

func (r *StorageSQLite) GetActivity(ctx context.Context, userID int64, timestamp time.Time) (*Activity, error)

func (*StorageSQLite) GetActivityList

func (r *StorageSQLite) GetActivityList(ctx context.Context, userID int64, from, to time.Time) ([]Activity, error)

func (*StorageSQLite) GetBundle

func (r *StorageSQLite) GetBundle(ctx context.Context, userID int64, key string) (*Bundle, error)

func (*StorageSQLite) GetBundleList

func (r *StorageSQLite) GetBundleList(ctx context.Context, userID int64) ([]Bundle, error)

func (*StorageSQLite) GetFood

func (r *StorageSQLite) GetFood(ctx context.Context, key string) (*Food, error)

func (*StorageSQLite) GetFoodList

func (r *StorageSQLite) GetFoodList(ctx context.Context) ([]Food, error)

func (*StorageSQLite) GetJournalFoodAvgWeight

func (r *StorageSQLite) GetJournalFoodAvgWeight(ctx context.Context, userID int64, from, to time.Time, foodkey string) (float64, error)

func (*StorageSQLite) GetJournalMealReport

func (r *StorageSQLite) GetJournalMealReport(ctx context.Context, userID int64, timestamp time.Time, meal Meal) (*JournalMealReport, error)

func (*StorageSQLite) GetJournalReport

func (r *StorageSQLite) GetJournalReport(ctx context.Context, userID int64, from, to time.Time) ([]JournalReport, error)

func (*StorageSQLite) GetJournalStats

func (r *StorageSQLite) GetJournalStats(ctx context.Context, userID int64, from, to time.Time) ([]JournalStats, error)

func (*StorageSQLite) GetUserSettings

func (r *StorageSQLite) GetUserSettings(ctx context.Context, userID int64) (*UserSettings, error)

func (*StorageSQLite) GetWeightList

func (r *StorageSQLite) GetWeightList(ctx context.Context, userID int64, from, to time.Time) ([]Weight, error)

func (*StorageSQLite) SetActivity

func (r *StorageSQLite) SetActivity(ctx context.Context, userID int64, activity *Activity) error

func (*StorageSQLite) SetBundle

func (r *StorageSQLite) SetBundle(ctx context.Context, userID int64, bndl *Bundle) error

func (*StorageSQLite) SetFood

func (r *StorageSQLite) SetFood(ctx context.Context, food *Food) error

func (*StorageSQLite) SetFoodComment

func (r *StorageSQLite) SetFoodComment(ctx context.Context, key, comment string) error

func (*StorageSQLite) SetJournal

func (r *StorageSQLite) SetJournal(ctx context.Context, userID int64, journal *Journal) error

func (*StorageSQLite) SetJournalBundle

func (r *StorageSQLite) SetJournalBundle(ctx context.Context, userID int64, timestamp time.Time, meal Meal, bndlKey string) error

func (*StorageSQLite) SetUserSettings

func (r *StorageSQLite) SetUserSettings(ctx context.Context, userID int64, settings *UserSettings) error

func (*StorageSQLite) SetWeight

func (r *StorageSQLite) SetWeight(ctx context.Context, userID int64, weight *Weight) error

type TxFn

type TxFn func(ctx context.Context, tx *ent.Tx) (any, error)

type UserSettings

type UserSettings struct {
	CalLimit         float64
	DefaultActiveCal float64
}

func (*UserSettings) Validate

func (r *UserSettings) Validate() bool

type UserSettingsBackup

type UserSettingsBackup struct {
	UserID   int64   `json:"user_id"`
	CalLimit float64 `json:"cal_limit"`
}

type Weight

type Weight struct {
	Timestamp time.Time
	Value     float64
}

func (*Weight) Validate

func (r *Weight) Validate() bool

type WeightBackup

type WeightBackup struct {
	UserID    int64   `json:"user_id"`
	Timestamp int64   `json:"timestamp"`
	Value     float64 `json:"value"`
}

Directories

Path Synopsis
ent

Jump to

Keyboard shortcuts

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