repository

package
v0.0.0-...-d05f80c Latest Latest
Warning

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

Go to latest
Published: May 22, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package repository contains all the functionality for working with the DB.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MigrateUp

func MigrateUp(logger *zap.SugaredLogger, db *sqlx.DB, path string) error

MigrateUp applies migrations to DB.

Types

type Operation

type Operation struct {
	ID          int64     `db:"id"`
	Wallet      string    `db:"wallet"`
	Type        string    `db:"type"`
	Amount      uint64    `db:"amount"`
	OtherWallet string    `db:"other_wallet"`
	CreatedAt   time.Time `db:"created_at"`
}

type Repo

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

Repo performs database operations.

func NewRepo

func NewRepo(logger *zap.SugaredLogger, cfg config.DB) (*Repo, error)

NewRepo creates instance of repository using config and applies migrations.

func NewRepoWithDB

func NewRepoWithDB(logger *zap.SugaredLogger, db *sqlx.DB) (*Repo, error)

NewRepoWithDB creates instance of repository using existing DB.

func (*Repo) CreateWallet

func (r *Repo) CreateWallet(walletName string) error

CreateWallet creates new wallet with unique name, or do nothing if wallet already exists.

func (*Repo) GetOperations

func (r *Repo) GetOperations(filter dto.OperationsFilter) ([]dto.Operation, error)

GetOperations selects operations for specified wallet using filter. Operations ordered by time.

func (*Repo) GetWallet

func (r *Repo) GetWallet(walletName string) (*dto.Wallet, error)

GetWallet selects wallet by name.

func (*Repo) GetWalletsForUpdateTx

func (r *Repo) GetWalletsForUpdateTx(tx *sqlx.Tx, walletNames []string) ([]dto.Wallet, error)

GetWalletsForUpdateTx selects wallets and obtains a lock for them at the database level using transaction. It will wait if some of the required wallets already locked in another goroutine.

func (*Repo) IncreaseWalletBalance

func (r *Repo) IncreaseWalletBalance(walletName string, amount uint64) error

IncreaseWalletBalance runs two operations in transaction:

  • increases wallet balance;
  • add new operation with type deposit.

func (*Repo) RunWithTransaction

func (r *Repo) RunWithTransaction(f func(tx *sqlx.Tx) error) error

RunWithTransaction runs the given function inside a transaction.

func (*Repo) TransferTx

func (r *Repo) TransferTx(tx *sqlx.Tx, walletFrom, walletTo string, amount uint64) error

TransferTx runs four operations using transaction:

  • decreases balance of wallet_from if there is enough money;
  • add new operation with type withdrawal for wallet_from;
  • increases balance of wallet_to;
  • add new operation with type deposit for wallet_to.

type Wallet

type Wallet struct {
	Name      string    `db:"name"`
	Balance   uint64    `db:"balance"`
	CreatedAt time.Time `db:"created_at"`
	UpdatedAt time.Time `db:"updated_at"`
}

Jump to

Keyboard shortcuts

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