bank

package
v0.0.0-...-0eb8b80 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2022 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAccountExists = bcommon.HTTPError{"account exists", http.StatusBadRequest}

ErrAccountExists is returned when creating an account that already exists.

View Source
var ErrAccountHasBalance = bcommon.HTTPError{"account has a nonzero balance", http.StatusBadRequest}

ErrAccountHasBalance is returned when deleting a nonempty account.

View Source
var ErrInsufficientFunds = bcommon.HTTPError{"insufficient funds", http.StatusBadRequest}

ErrInsufficientFunds is returned when there are insufficient funds for some action.

View Source
var ErrNoSuchAccount = bcommon.HTTPError{"account does not exist", http.StatusNotFound}

ErrNoSuchAccount is returned when accessing an account which does not exist.

View Source
var ErrNoSuchConfig = bcommon.HTTPError{"no such configuration item", http.StatusNotFound}

ErrNoSuchConfig is return when a nonexistent configuration item is accessed.

View Source
var ErrNoSuchUser = bcommon.HTTPError{"user does not exist", http.StatusNotFound}

ErrNoSuchUser is returned when accessing a user who does not exist.

View Source
var ErrPasswordMismatch = errors.New("password mismatch")

ErrPasswordMismatch is returned by VerifyPassword for incorrect passwords.

View Source
var ErrUnsuitableParties = bcommon.HTTPError{"invalid or inconsistent parties to transaction", http.StatusBadRequest}

ErrUnsuitableParties is returned when there is something wrong with the proposed parties to a transaction.

View Source
var ErrUserExists = bcommon.HTTPError{"user exists", http.StatusBadRequest}

ErrUserExists is returned when creating a user that already exists.

View Source
var Version = "0.6.0-dirty"

Version is the version string of the program

Functions

func GetAccounts

func GetAccounts(tx *sql.Tx) (accounts []string, err error)

GetAccounts returns the list of account names.

func GetConfig

func GetConfig(tx *sql.Tx, key string) (value string, err error)

GetConfig returns the value of a configuration item.

func GetConfigs

func GetConfigs(tx *sql.Tx) (config map[string]string, err error)

GetConfigs returns the full configuration table.

func GetUsers

func GetUsers(tx *sql.Tx) (users []string, err error)

GetUsers returns the list of user names.

func OpenDatabase

func OpenDatabase(driver, source string) (db *sql.DB, err error)

OpenDatabase opens a database handle.

func PutConfig

func PutConfig(tx *sql.Tx, key, value string) (err error)

PutConfig updates or stores a configuration item.

func SetPassword

func SetPassword(password string) (stored string, err error)

SetPassword computes a new stored string for a password.

func Transact

func Transact(database *sql.DB, body func(tx *sql.Tx) (err error)) (err error)

Transact runs a function inside a database transaction. On success the transaction is committed. On error the transaction is rolled back.

func VerifyPassword

func VerifyPassword(stored, password string) (err error)

VerifyPassword verifies a password against a stored version.

Types

type Account

type Account struct {
	// Name of the account
	Account string

	// Current balance
	Balance int
}

Account defines a single account.

func (*Account) Delete

func (a *Account) Delete(tx *sql.Tx) (err error)

Delete deletes an account.

Accounts with nonzero balances cannot be deleted.

func (*Account) Get

func (a *Account) Get(tx *sql.Tx, account string) (err error)

Get fills in the data for a account.

func (*Account) Put

func (a *Account) Put(tx *sql.Tx, new bool) (err error)

Put updates account data in the database. Set new to true to create or false to update.

type Bank

type Bank struct {
	DB *sql.DB
}

Bank wraps access to a bank database.

func NewBank

func NewBank(driver, source string) (b *Bank, err error)

NewBank creates a new Bank object.

func (*Bank) CheckPassword

func (b *Bank) CheckPassword(user, password string) (err error)

CheckPassword checks a user password.

func (*Bank) Close

func (b *Bank) Close() (err error)

Close closes the Bank object.

func (*Bank) DeleteAccount

func (b *Bank) DeleteAccount(account string) (err error)

DeleteAccount deletes a user

func (*Bank) DeleteUser

func (b *Bank) DeleteUser(user string) (err error)

DeleteUser deletes a user

func (*Bank) Distribute

func (b *Bank) Distribute(user string, origin string, destinations []string, description string) (err error)

Distribute makes a distribution transaction.

func (*Bank) GetAccounts

func (b *Bank) GetAccounts() (accounts []string, err error)

GetAccounts list accounts.

func (*Bank) GetConfig

func (b *Bank) GetConfig(key string) (value string, err error)

GetConfig retrieves a configuration item.

func (*Bank) GetConfigs

func (b *Bank) GetConfigs() (configs map[string]string, err error)

GetConfigs retrieves all configuration items.

func (*Bank) GetTransactions

func (b *Bank) GetTransactions(limit, offset int, after int) (transactions []Transaction, err error)

GetTransactions gets some transactions.

Starting with the most recent transaction, it skips `offset` transactions and then gets the next `limit` transactions.

func (*Bank) GetUsers

func (b *Bank) GetUsers() (users []string, err error)

GetUsers list users.

func (*Bank) NewAccount

func (b *Bank) NewAccount(account string) (err error)

NewAccount creates a new account.

func (*Bank) NewBank

func (b *Bank) NewBank() (err error)

NewBank creates tables for a new bank.

func (*Bank) NewTransaction

func (b *Bank) NewTransaction(user, origin, destination, description string, amount int) (err error)

NewTransaction makes a transaction.

func (*Bank) NewUser

func (b *Bank) NewUser(user, password string) (err error)

NewUser creates a new user.

func (*Bank) PutConfig

func (b *Bank) PutConfig(key, value string) (err error)

PutConfig sets a configuration item.

func (*Bank) SetPassword

func (b *Bank) SetPassword(user, password string) (err error)

SetPassword changes a user password.

type Transaction

type Transaction struct {
	// Transaction ID
	ID int

	// Unix time of the transaction
	Time int64

	// User who make the transaction
	User string

	// Description of the transaction
	Description string

	// Origin account
	Origin string

	// Destination account
	Destination string

	// Amount in pence
	Amount int

	OriginBalanceAfter int

	DestinationBalanceAfter int
}

Transaction defines a single transaction.

func GetTransactions

func GetTransactions(tx *sql.Tx, limit, offset int, after int) (transactions []Transaction, err error)

GetTransactions returns all transactions in a range.

func (*Transaction) Put

func (t *Transaction) Put(tx *sql.Tx, new bool) (err error)

Put updates transaction data in the database. Set new to true to create or false to update.

type User

type User struct {
	// Name of the user
	User string

	// Hashed password
	Password string
}

User defines a single user.

func (*User) Delete

func (u *User) Delete(tx *sql.Tx) (err error)

Delete deletes a user.

func (*User) Get

func (u *User) Get(tx *sql.Tx, user string) (err error)

Get fills in the data for a user.

func (*User) Put

func (u *User) Put(tx *sql.Tx, new bool) (err error)

Put updates user data in the database. Set new to true to create or false to update.

Jump to

Keyboard shortcuts

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