database

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package database has a database client to access the transactions and account database together with helpers to interact with those tables

Index

Constants

This section is empty.

Variables

View Source
var (
	// UnallocatedMoney is a category UUID which is automatically created
	// during database migration phase and therefore always available
	UnallocatedMoney = makeConstAcctID(1)
	// StartingBalance is a category UUID which is automatically created
	// and hidden during database migration and used in frontend as constant
	StartingBalance = makeConstAcctID(2) //nolint:gomnd

)

Functions

This section is empty.

Types

type Account

type Account struct {
	BaseModel
	Name   string      `json:"name"`
	Type   AccountType `json:"type"`
	Hidden bool        `json:"hidden"`
}

Account represents a budget, tracking or category account - in general something holding money through the sum of transactions

type AccountBalance

type AccountBalance struct {
	Account
	Balance float64 `json:"balance"`
}

AccountBalance wraps an Account and adds the balance

type AccountType

type AccountType string

AccountType represents the type of an account

const (
	AccountTypeBudget   AccountType = "budget"
	AccountTypeCategory AccountType = "category"
	AccountTypeTracking AccountType = "tracking"
)

Known values of the AccountType enum

func (AccountType) IsValid

func (a AccountType) IsValid() bool

IsValid checks whether the given AccountType belongs to the known types

type BaseModel

type BaseModel struct {
	ID        uuid.UUID      `gorm:"type:uuid" json:"id"`
	CreatedAt time.Time      `json:"-"`
	UpdatedAt time.Time      `json:"-"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

BaseModel is used internally in all other models for common fields

func (*BaseModel) BeforeCreate

func (b *BaseModel) BeforeCreate(*gorm.DB) (err error)

BeforeCreate ensures the object UUID is filled

type Client

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

Client is the database client

func New

func New(dbtype, dsn string) (*Client, error)

New creates a new database client for the given DSN

func (*Client) CreateAccount

func (c *Client) CreateAccount(name string, accType AccountType) (a Account, err error)

CreateAccount creates and returns a new account of the given type

func (*Client) CreateTransaction

func (c *Client) CreateTransaction(tx Transaction) (ntx Transaction, err error)

CreateTransaction takes a prepared transaction and stores it

func (*Client) DeleteTransaction

func (c *Client) DeleteTransaction(id uuid.UUID) (err error)

DeleteTransaction deletes a transaction

func (*Client) GetAccount

func (c *Client) GetAccount(id uuid.UUID) (a Account, err error)

GetAccount retrieves an Account using its ID

func (*Client) GetTransactionByID

func (c *Client) GetTransactionByID(id uuid.UUID) (tx Transaction, err error)

GetTransactionByID returns a single transaction by its ID

func (*Client) ListAccountBalances

func (c *Client) ListAccountBalances(showHidden bool) (a []AccountBalance, err error)

ListAccountBalances returns a list of accounts with their corresponding balance

func (*Client) ListAccounts

func (c *Client) ListAccounts(showHidden bool) (a []Account, err error)

ListAccounts returns a list of all accounts

func (*Client) ListAccountsByType

func (c *Client) ListAccountsByType(at AccountType, showHidden bool) (a []Account, err error)

ListAccountsByType returns a list of all accounts of the given type

func (*Client) ListTransactions

func (c *Client) ListTransactions(since, until time.Time) (txs []Transaction, err error)

ListTransactions retrieves all transactions

func (*Client) ListTransactionsByAccount

func (c *Client) ListTransactionsByAccount(acc uuid.UUID, since, until time.Time) (txs []Transaction, err error)

ListTransactionsByAccount retrieves all transactions for an account or category

func (*Client) TransferMoney

func (c *Client) TransferMoney(from, to uuid.UUID, amount float64) (err error)

TransferMoney creates new Transactions for the given account transfer. The account type of the from and to account must match for this to work.

func (*Client) TransferMoneyWithCategory

func (c *Client) TransferMoneyWithCategory(from, to uuid.UUID, amount float64, category uuid.UUID) (err error)

TransferMoneyWithCategory creates new Transactions for the given account transfer. This is not possible for category type accounts.

func (*Client) UpdateAccountHidden

func (c *Client) UpdateAccountHidden(id uuid.UUID, hidden bool) (err error)

UpdateAccountHidden updates the hidden flag for the given Account

func (*Client) UpdateAccountName

func (c *Client) UpdateAccountName(id uuid.UUID, name string) (err error)

UpdateAccountName sets a new name for the given account ID

func (*Client) UpdateTransaction

func (c *Client) UpdateTransaction(txID uuid.UUID, tx Transaction) (err error)

UpdateTransaction takes a transaction, fetches the stored transaction applies some sanity actions and stores it back to the database

func (*Client) UpdateTransactionCategory

func (c *Client) UpdateTransactionCategory(id uuid.UUID, cat uuid.UUID) (err error)

UpdateTransactionCategory modifies the category of the given transaction. (It is not possible to remove a category with this)

func (*Client) UpdateTransactionCleared

func (c *Client) UpdateTransactionCleared(id uuid.UUID, cleared bool) (err error)

UpdateTransactionCleared modifies the "cleared" flag for the given transaction

type Transaction

type Transaction struct {
	BaseModel
	Time        time.Time     `json:"time"`
	Payee       string        `json:"payee"`
	Description string        `json:"description"`
	Amount      float64       `json:"amount"`
	Account     uuid.NullUUID `gorm:"type:uuid" json:"account"`
	Category    uuid.NullUUID `gorm:"type:uuid" json:"category"`
	Cleared     bool          `json:"cleared"`
}

Transaction represents some money movement between, from or to accounts

func (Transaction) Validate

func (t Transaction) Validate(c *Client) (err error)

Validate executes some basic checks on the transaction

Jump to

Keyboard shortcuts

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