data

package
v0.0.0-...-5472ab2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TransactionTypeExpenseIncome is an Expense/Income transaction (totals do not have to be equal).
	TransactionTypeExpenseIncome = iota
	// TransactionTypeTransfer is a Transfer transaction (totals for each currency should be zero).
	TransactionTypeTransfer
)

Variables

View Source
var ErrUserAlreadyExists = fmt.Errorf("uuid conflicts with existing user")

ErrUserAlreadyExists is an error when a user cannot be renamed because their username is already in use.

View Source
var GetAllTransactionsOptions = GetTransactionOptions{Offset: 0, Limit: ^uint64(0)}

GetAllTransactionsOptions is a GetTransactionOptions which returns all transactions in one page.

Functions

func DefaultOptions

func DefaultOptions() pogreb.Options

DefaultOptions returns default options for the database, customized based on environment variables.

Types

type Account

type Account struct {
	UUID           string
	Name           string
	Balance        int64
	Currency       string
	IncludeInTotal bool
	ShowInList     bool
}

Account keeps the balance and other details for an account.

type DBService

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

DBService provides services for reading and writing structs in the database.

func Open

func Open(options pogreb.Options) (*DBService, error)

Open opens the database with options and returns a DBService instance.

func (*DBService) Backup

func (s *DBService) Backup(user *User) (string, error)

Backup returns a serialized copy of all data for user.

func (*DBService) Close

func (service *DBService) Close()

Close closes the underlying database.

func (*DBService) CountTransactions

func (s *DBService) CountTransactions(user *User, options TransactionFilterOptions) (uint64, error)

CountTransactions returns the number of transactions matching the filter options.

func (*DBService) CreateAccount

func (s *DBService) CreateAccount(user *User, account *Account) error

CreateAccount creates and saves the specified account. It generates sets the ID to the generated account ID.

func (*DBService) CreateTransaction

func (s *DBService) CreateTransaction(user *User, transaction *Transaction) error

CreateTransaction saves a new Transaction into the database.

func (*DBService) DeleteAccount

func (s *DBService) DeleteAccount(user *User, accountUUID string) error

DeleteAccount deletes an account by its UUID. If the account doesn't exist, it returns an error.

func (*DBService) DeleteTransaction

func (s *DBService) DeleteTransaction(user *User, transactionUUID string) error

DeleteTransaction deletes a Transaction and its sort index key by its UUID. Deleting a transaction also updates the affected Account balance. If transaction doesn't exist, returns an error.

func (*DBService) GC

func (service *DBService) GC()

GC deletes expired items and attempts to perform a database cleanup.

func (*DBService) GetAccount

func (s *DBService) GetAccount(user *User, accountUUID string) (*Account, error)

GetAccount returns an Account by its UUID. If the Account doesn't exist, it returns nil.

func (*DBService) GetAccounts

func (s *DBService) GetAccounts(user *User) ([]*Account, error)

GetAccounts returns all accounts for user.

func (*DBService) GetOrCreateConfigVariable

func (s *DBService) GetOrCreateConfigVariable(varName string, generator func() (string, error)) (string, error)

GetOrCreateConfigVariable returns the value for the varName ServerConfig variable, or if there's no entry, uses generator to create and save a value.

func (*DBService) GetTags

func (s *DBService) GetTags(user *User) ([]string, error)

GetTags returns an unsorted (but deduplicated) list of tags for user.

func (*DBService) GetTransaction

func (s *DBService) GetTransaction(user *User, transactionUUID string) (*Transaction, error)

GetTransaction returns a Transaction by its UUID. If the Transaction doesn't exist, it returns nil.

func (*DBService) GetTransactions

func (s *DBService) GetTransactions(user *User, options GetTransactionOptions) ([]*Transaction, error)

GetTransactions returns transactions for user matching the filter and paging options. Returns an empty list if no transactions match the options.

func (*DBService) GetUser

func (s *DBService) GetUser(username string) (*User, error)

GetUser returns the User by username. If user doesn't exist, returns nil.

func (*DBService) Restore

func (s *DBService) Restore(user *User, value string) error

Restore replaces all data for user with the provided serialized backup.

func (*DBService) SaveUser

func (s *DBService) SaveUser(user *User) error

SaveUser saves updates an existing user in the database.

func (*DBService) SetConfigVariable

func (s *DBService) SetConfigVariable(varName, varValue string) error

SetConfigVariable returns the value for the varName ServerConfig variable, or nil if no value is saved.

func (*DBService) UpdateAccount

func (s *DBService) UpdateAccount(user *User, account *Account) error

UpdateAccount saves an already existing account. If the account doesn't exist, it returns an error.

func (*DBService) UpdateTransaction

func (s *DBService) UpdateTransaction(user *User, transaction *Transaction) error

UpdateTransaction updates an existing Transaction in the database.

type GetTransactionOptions

type GetTransactionOptions struct {
	Offset uint64
	Limit  uint64
	TransactionFilterOptions
}

GetTransactionOptions specifies paging and filtering options for retrieving transactions.

type Transaction

type Transaction struct {
	UUID        string
	Description string
	Type        int
	Tags        []string
	Date        string
	Components  []TransactionComponent
}

Transaction saves details for one expense item.

type TransactionComponent

type TransactionComponent struct {
	Amount      int64
	AccountUUID string
}

TransactionComponent contains details for a part of a Transaction.

type TransactionFilterOptions

type TransactionFilterOptions struct {
	FilterDescription    string
	FilterFromDate       string
	FilterToDate         string
	FilterTags           []string
	FilterAccounts       []string
	ExcludeExpenseIncome bool
	ExcludeTransfer      bool
}

TransactionFilterOptions specifies filter parameters for Transactions.

func (*TransactionFilterOptions) IsEmpty

func (options *TransactionFilterOptions) IsEmpty() bool

IsEmpty returns if options do not apply any filtering (all transactions match this filter).

func (*TransactionFilterOptions) Matches

func (options *TransactionFilterOptions) Matches(transaction *Transaction) bool

Matches returns true if transaction is accepted by the filter.

type User

type User struct {
	UUID     string
	Password string
	// contains filtered or unexported fields
}

User keeps configuration for a user and information used to link a user with their data.

func NewUser

func NewUser(username string) *User

NewUser creates a User with the provided username and a generated UUID.

func (*User) GetUsername

func (user *User) GetUsername() string

GetUsername returns the user's current username.

func (*User) SetPassword

func (user *User) SetPassword(newPassword string) error

SetPassword sets a new password for user. The password is hashed and salted with bcrypt.

func (*User) SetUsername

func (user *User) SetUsername(newUsername string) error

SetUsername sets a new username for User which will be updated when SaveUser is called.

func (*User) ValidatePassword

func (user *User) ValidatePassword(password string) error

ValidatePassword checks if password matches the user's password.

Jump to

Keyboard shortcuts

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