beans

package
v0.0.0-...-4a67540 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EFORBIDDEN     = "forbidden"
	EINTERNAL      = "internal"
	EINVALID       = "invalid"
	ENOTFOUND      = "not_found"
	EUNAUTHORIZED  = "unauthorized"
	EUNPROCESSABLE = "unprocessable"
)

Variables

View Source
var (
	ErrorForbidden     = &beansError{code: EFORBIDDEN, msg: "Forbidden"}
	ErrorInternal      = &beansError{code: EINTERNAL, msg: "Internal error"}
	ErrorInvalid       = &beansError{code: EINVALID, msg: "Invalid data provided"}
	ErrorNotFound      = &beansError{code: ENOTFOUND, msg: "Not found"}
	ErrorUnauthorized  = &beansError{code: EUNAUTHORIZED, msg: "Not authenticated"}
	ErrorUnprocessable = &beansError{code: EUNPROCESSABLE, msg: "Unprocessable request"}
)
View Source
var Arithmetic amountArithmetic

Functions

func ExecTx

func ExecTx[T any](ctx context.Context, m TxManager, callback func(tx Tx) (T, error)) (T, error)

func ExecTxNil

func ExecTxNil(ctx context.Context, m TxManager, callback func(tx Tx) error) error

func Field

func Field(name string, objects ...Validatable) validatableField

func Max

func Max(c Countable, max int, object string) valitableCountable

func MaxPrecision

func MaxPrecision(a Amount) validatableMaxPrecision

func NonZero

func NonZero(a Amount) validatableNonZero

func Positive

func Positive(a Amount) validatablePositive

func ValidateFields

func ValidateFields(objects ...validatableField) error

Types

type Account

type Account struct {
	ID   ID
	Name Name

	BudgetID ID

	// Must be loaded explicitly.
	Balance Amount
}

type AccountContract

type AccountContract interface {
	// Creates an account.
	Create(ctx context.Context, auth *BudgetAuthContext, name Name) (*Account, error)

	// Gets all accounts associated with the budget. Loads Balance.
	GetAll(ctx context.Context, auth *BudgetAuthContext) ([]*Account, error)
}

type AccountRepository

type AccountRepository interface {
	Create(ctx context.Context, id ID, name Name, budgetID ID) error
	Get(ctx context.Context, id ID) (*Account, error)
	// Loads Balance.
	GetForBudget(ctx context.Context, budgetID ID) ([]*Account, error)
}

type Amount

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

func NewAmount

func NewAmount(coefficient int64, exponent int32) Amount

func NewAmountWithBigInt

func NewAmountWithBigInt(coefficient *big.Int, exponent int32) Amount

func NewEmptyAmount

func NewEmptyAmount() Amount

func (*Amount) Coefficient

func (a *Amount) Coefficient() *big.Int

func (*Amount) Empty

func (a *Amount) Empty() bool

func (*Amount) Exponent

func (a *Amount) Exponent() int32

func (Amount) MarshalJSON

func (a Amount) MarshalJSON() ([]byte, error)

func (*Amount) OrZero

func (a *Amount) OrZero() Amount

If the amount is empty returns a new zero amount.

func (*Amount) String

func (a *Amount) String() string

func (*Amount) UnmarshalJSON

func (a *Amount) UnmarshalJSON(b []byte) error

type AuthContext

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

func NewAuthContext

func NewAuthContext(userID ID, sessionID SessionID) *AuthContext

func (*AuthContext) SessionID

func (c *AuthContext) SessionID() SessionID

func (*AuthContext) UserID

func (c *AuthContext) UserID() ID

type Budget

type Budget struct {
	ID   ID
	Name Name

	// Must be explicitly loaded.
	UserIDs []ID
}

func (*Budget) UserHasAccess

func (b *Budget) UserHasAccess(userID ID) bool

type BudgetAuthContext

type BudgetAuthContext struct {
	*AuthContext
	// contains filtered or unexported fields
}

func NewBudgetAuthContext

func NewBudgetAuthContext(auth *AuthContext, budget *Budget) (*BudgetAuthContext, error)

func (*BudgetAuthContext) BudgetID

func (c *BudgetAuthContext) BudgetID() ID

type BudgetContract

type BudgetContract interface {
	// Creates a budget.
	Create(ctx context.Context, auth *AuthContext, name Name) (*Budget, error)

	// Gets a budget by the budget ID.
	// Ensures the user has access to the budget.
	Get(ctx context.Context, auth *AuthContext, id ID) (*Budget, error)

	// Gets all budgets accessible to the user.
	GetAll(ctx context.Context, auth *AuthContext) ([]*Budget, error)
}

type BudgetRepository

type BudgetRepository interface {
	// Creates a budget and assigns user to the budget.
	Create(ctx context.Context, tx Tx, id ID, name Name, userID ID) error
	// Gets budget by ID. Attaches UserIDs field.
	Get(ctx context.Context, id ID) (*Budget, error)
	GetBudgetsForUser(ctx context.Context, userID ID) ([]*Budget, error)
}

type Category

type Category struct {
	ID       ID
	BudgetID ID
	GroupID  ID
	Name     Name
}

type CategoryContract

type CategoryContract interface {
	// Creates a category.
	CreateCategory(ctx context.Context, auth *BudgetAuthContext, groupID ID, name Name) (*Category, error)

	// Creates a category group.
	CreateGroup(ctx context.Context, auth *BudgetAuthContext, name Name) (*CategoryGroup, error)

	// Gets all categories and groups for a budget.
	GetAll(ctx context.Context, auth *BudgetAuthContext) ([]*CategoryGroup, []*Category, error)
}

type CategoryGroup

type CategoryGroup struct {
	ID       ID
	BudgetID ID
	Name     Name
	IsIncome bool
}

type CategoryRepository

type CategoryRepository interface {
	Create(ctx context.Context, tx Tx, category *Category) error
	GetSingleForBudget(ctx context.Context, id ID, budgetID ID) (*Category, error)
	GetForBudget(ctx context.Context, budgetID ID) ([]*Category, error)
	CreateGroup(ctx context.Context, tx Tx, categoryGroup *CategoryGroup) error
	GetGroupsForBudget(ctx context.Context, budgetID ID) ([]*CategoryGroup, error)
	GroupExists(ctx context.Context, budgetID ID, id ID) (bool, error)
}

type Countable

type Countable interface {
	Length() int
}

type Date

type Date struct {
	time.Time
	// contains filtered or unexported fields
}

func NewDate

func NewDate(date time.Time) Date

func (Date) Empty

func (d Date) Empty() bool

func (Date) String

func (d Date) String() string

func (*Date) UnmarshalJSON

func (t *Date) UnmarshalJSON(b []byte) error

type Emptiable

type Emptiable interface {
	Empty() bool
}

type Error

type Error interface {
	error
	BeansError() (string, string)
}

func NewError

func NewError(code string, msg string) Error

func WrapError

func WrapError(err error, parent Error) Error

type ID

type ID ksuid.KSUID

func BeansIDFromString

func BeansIDFromString(id string) (ID, error)

func NewBeansID

func NewBeansID() ID

func (ID) Empty

func (id ID) Empty() bool

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

func (ID) String

func (id ID) String() string

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(b []byte) error

type Month

type Month struct {
	ID        ID
	BudgetID  ID
	Date      MonthDate
	Carryover Amount

	// Must be explicitly loaded.
	CarriedOver Amount
	// Must be explicitly loaded.
	Income Amount
	// Must be explicitly loaded.
	Assigned Amount
}

func (Month) String

func (m Month) String() string

type MonthCategory

type MonthCategory struct {
	ID         ID
	MonthID    ID
	CategoryID ID
	Amount     Amount

	// Must be explicitly loaded.
	Activity Amount
	// Must be explicitly loaded.
	Available Amount
}

type MonthCategoryRepository

type MonthCategoryRepository interface {
	Create(ctx context.Context, tx Tx, monthCategory *MonthCategory) error
	UpdateAmount(ctx context.Context, monthCategoryID ID, amount Amount) error
	// Gets categories by month. Attaches Activity, Available fields.
	GetForMonth(ctx context.Context, month *Month) ([]*MonthCategory, error)
	// Gets the month category, or creates it if it does not exist.
	GetOrCreate(ctx context.Context, tx Tx, monthID ID, categoryID ID) (*MonthCategory, error)

	// Gets the amount assigned in a month.
	GetAssignedInMonth(ctx context.Context, monthID ID) (Amount, error)
}

type MonthContract

type MonthContract interface {
	// Gets a month, its categories, and budgetable amount.
	// If the month does not exist it is created.
	//
	// Attaches the fields: CarriedOver, Income, Assigned.
	GetOrCreate(ctx context.Context, auth *BudgetAuthContext, date MonthDate) (*Month, []*MonthCategory, Amount, error)

	// Updates the given month.
	Update(ctx context.Context, auth *BudgetAuthContext, monthID ID, carryover Amount) error

	// Sets the assigned amount on a category for a month.
	SetCategoryAmount(ctx context.Context, auth *BudgetAuthContext, monthID ID, categoryID ID, amount Amount) error
}

type MonthDate

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

func NewMonthDate

func NewMonthDate(date Date) MonthDate

Creates a new MonthDate and normalizes the date.

func (MonthDate) FirstDay

func (d MonthDate) FirstDay() Date

func (MonthDate) LastDay

func (d MonthDate) LastDay() Date

func (MonthDate) Previous

func (d MonthDate) Previous() MonthDate

func (MonthDate) String

func (d MonthDate) String() string

func (MonthDate) Time

func (d MonthDate) Time() time.Time

type MonthRepository

type MonthRepository interface {
	Create(ctx context.Context, tx Tx, month *Month) error
	Get(ctx context.Context, id ID) (*Month, error)
	// Only updates the Carryover field.
	Update(ctx context.Context, month *Month) error
	GetOrCreate(ctx context.Context, tx Tx, budgetID ID, date MonthDate) (*Month, error)
	GetForBudget(ctx context.Context, budgetID ID) ([]*Month, error)
}

type Name

type Name ValidatableString

func (Name) Validate

func (n Name) Validate() error

type NullString

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

func NewNullString

func NewNullString(string string) NullString

func NullStringFromSQL

func NullStringFromSQL(s sql.NullString) NullString

func (NullString) Empty

func (s NullString) Empty() bool

func (NullString) Length

func (s NullString) Length() int

func (NullString) MarshalJSON

func (s NullString) MarshalJSON() ([]byte, error)

func (NullString) SQLNullString

func (s NullString) SQLNullString() sql.NullString

func (NullString) String

func (s NullString) String() string

func (*NullString) UnmarshalJSON

func (s *NullString) UnmarshalJSON(b []byte) error

type Password

type Password string

func (Password) Empty

func (p Password) Empty() bool

func (Password) Length

func (p Password) Length() int

func (Password) ValidatableField

func (p Password) ValidatableField() validatableField

type PasswordHash

type PasswordHash string

type Payee

type Payee struct {
	ID       ID
	BudgetID ID
	Name     Name
}

type PayeeContract

type PayeeContract interface {
	// Creates a payee.
	CreatePayee(ctx context.Context, auth *BudgetAuthContext, name Name) (*Payee, error)

	// Gets all payees for a budget.
	GetAll(ctx context.Context, auth *BudgetAuthContext) ([]*Payee, error)
}

type PayeeRepository

type PayeeRepository interface {
	Create(ctx context.Context, payee *Payee) error
	Get(ctx context.Context, id ID) (*Payee, error)
	GetForBudget(ctx context.Context, budgetID ID) ([]*Payee, error)
}

type Session

type Session struct {
	ID        SessionID
	UserID    ID
	CreatedAt time.Time
}

type SessionID

type SessionID string

type SessionRepository

type SessionRepository interface {
	Create(userID ID) (*Session, error)
	Get(id SessionID) (*Session, error)
	Delete(id SessionID) error
}

type Transaction

type Transaction struct {
	ID ID

	AccountID  ID
	CategoryID ID
	PayeeID    ID

	Amount Amount
	Date   Date
	Notes  TransactionNotes

	// Must be explicitly loaded.
	Account *Account
	// Must be explicitly loaded.
	CategoryName NullString
	// Must be explicitly loaded.
	PayeeName NullString
}

type TransactionContract

type TransactionContract interface {
	// Creates a transaction. Attaches Account field.
	Create(ctx context.Context, auth *BudgetAuthContext, params TransactionCreateParams) (*Transaction, error)

	// Gets all transactions for budget. Attaches Account, CategoryName, PayeeName fields.
	GetAll(ctx context.Context, auth *BudgetAuthContext) ([]*Transaction, error)

	// Edits a transaction.
	Update(ctx context.Context, auth *BudgetAuthContext, params TransactionUpdateParams) error
}

type TransactionCreateParams

type TransactionCreateParams struct {
	TransactionParams
}

type TransactionNotes

type TransactionNotes struct{ NullString }

func NewTransactionNotes

func NewTransactionNotes(string string) TransactionNotes

type TransactionParams

type TransactionParams struct {
	AccountID  ID
	CategoryID ID
	PayeeID    ID
	Amount     Amount
	Date       Date
	Notes      TransactionNotes
}

func (TransactionParams) ValidateAll

func (t TransactionParams) ValidateAll() error

type TransactionRepository

type TransactionRepository interface {
	Create(ctx context.Context, transaction *Transaction) error

	Update(ctx context.Context, transaction *Transaction) error

	// Attaches Account, CategoryName, PayeeName fields to Transactions.
	GetForBudget(ctx context.Context, budgetID ID) ([]*Transaction, error)

	// Get transaction. Attaches Account field to Transaction.
	Get(ctx context.Context, id ID) (*Transaction, error)

	// Gets sum of all income transactions between the dates.
	GetIncomeBetween(ctx context.Context, budgetID ID, begin Date, end Date) (Amount, error)
}

type TransactionUpdateParams

type TransactionUpdateParams struct {
	ID ID
	TransactionParams
}

func (TransactionUpdateParams) ValidateAll

func (t TransactionUpdateParams) ValidateAll() error

type Tx

type Tx interface {
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type TxManager

type TxManager interface {
	Create(ctx context.Context) (Tx, error)
}

type User

type User struct {
	ID           ID
	Username     Username
	PasswordHash PasswordHash
}

type UserContract

type UserContract interface {
	// Creates a new account
	Register(ctx context.Context, username Username, password Password) error

	// Logs in and returns a session
	Login(ctx context.Context, username Username, password Password) (*Session, error)

	// Logs out and deletes the active session
	Logout(ctx context.Context, auth *AuthContext) error

	// Gets the currently authenticated user
	GetMe(ctx context.Context, auth *AuthContext) (*User, error)
}

type UserRepository

type UserRepository interface {
	Create(ctx context.Context, id ID, username Username, passwordHash PasswordHash) error
	Exists(ctx context.Context, username Username) (bool, error)
	Get(ctx context.Context, id ID) (*User, error)
	GetByUsername(ctx context.Context, username Username) (*User, error)
}

type Username

type Username string

func (Username) Empty

func (u Username) Empty() bool

func (Username) Length

func (u Username) Length() int

func (Username) ValidatableField

func (u Username) ValidatableField() validatableField

type Validatable

type Validatable interface {
	Validate() error
}

func Required

func Required(e Emptiable) Validatable

type ValidatableString

type ValidatableString string

func (ValidatableString) Empty

func (s ValidatableString) Empty() bool

func (ValidatableString) Length

func (s ValidatableString) Length() int

Jump to

Keyboard shortcuts

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