db

package
v0.0.0-...-d059590 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	ID        int32        `json:"id"`
	PublicID  string       `json:"public_id"`
	IsBlocked sql.NullBool `json:"is_blocked"`
	BlockedAt sql.NullTime `json:"blocked_at"`
	CreatedAt time.Time    `json:"created_at"`
	UpdatedAt time.Time    `json:"updated_at"`
	DeletedAt sql.NullTime `json:"deleted_at"`
	UserID    int32        `json:"user_id"`
	Balance   int64        `json:"balance"`
	Currency  string       `json:"currency"`
}

type AddAccountBalanceParams

type AddAccountBalanceParams struct {
	Amount int64 `json:"amount"`
	ID     int32 `json:"id"`
}

type CreateAccountParams

type CreateAccountParams struct {
	PublicID string `json:"public_id"`
	UserID   int32  `json:"user_id"`
	Balance  int64  `json:"balance"`
	Currency string `json:"currency"`
}

type CreateEntryParams

type CreateEntryParams struct {
	PublicID    string    `json:"public_id"`
	AccountID   int32     `json:"account_id"`
	Amount      int64     `json:"amount"`
	Type        EntryType `json:"type"`
	LastBalance int64     `json:"last_balance"`
}

type CreateTransferParams

type CreateTransferParams struct {
	PublicID      string `json:"public_id"`
	FromAccountID int32  `json:"from_account_id"`
	ToAccountID   int32  `json:"to_account_id"`
	Amount        int64  `json:"amount"`
}

type CreateUserParams

type CreateUserParams struct {
	PublicID  string `json:"public_id"`
	Firstname string `json:"firstname"`
	Lastname  string `json:"lastname"`
	Email     string `json:"email"`
	Password  string `json:"password"`
	Salt      string `json:"salt"`
}

type DBTX

type DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

type Entry

type Entry struct {
	ID        int32        `json:"id"`
	AccountID int32        `json:"account_id"`
	PublicID  string       `json:"public_id"`
	CreatedAt time.Time    `json:"created_at"`
	UpdatedAt time.Time    `json:"updated_at"`
	DeletedAt sql.NullTime `json:"deleted_at"`
	// can be negative or positive
	Amount      int64     `json:"amount"`
	Type        EntryType `json:"type"`
	LastBalance int64     `json:"last_balance"`
}

type EntryType

type EntryType string
const (
	EntryTypeDEBIT  EntryType = "DEBIT"
	EntryTypeCREDIT EntryType = "CREDIT"
)

func (*EntryType) Scan

func (e *EntryType) Scan(src interface{}) error

type ListAccountsParams

type ListAccountsParams struct {
	UserID int32 `json:"user_id"`
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type ListEntriesForAccountIdParams

type ListEntriesForAccountIdParams struct {
	AccountID int32 `json:"account_id"`
	Limit     int32 `json:"limit"`
	Offset    int32 `json:"offset"`
}

type ListEntriesParams

type ListEntriesParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type ListTransfersForFromAccountIdParams

type ListTransfersForFromAccountIdParams struct {
	FromAccountID int32 `json:"from_account_id"`
	Limit         int32 `json:"limit"`
	Offset        int32 `json:"offset"`
}

type ListTransfersForToAccountIdParams

type ListTransfersForToAccountIdParams struct {
	ToAccountID int32 `json:"to_account_id"`
	Limit       int32 `json:"limit"`
	Offset      int32 `json:"offset"`
}

type ListTransfersParams

type ListTransfersParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type ListUsersParams

type ListUsersParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type NullEntryType

type NullEntryType struct {
	EntryType EntryType `json:"entry_type"`
	Valid     bool      `json:"valid"` // Valid is true if EntryType is not NULL
}

func (*NullEntryType) Scan

func (ns *NullEntryType) Scan(value interface{}) error

Scan implements the Scanner interface.

func (NullEntryType) Value

func (ns NullEntryType) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Querier

type Querier interface {
	AddAccountBalance(ctx context.Context, arg AddAccountBalanceParams) (Account, error)
	CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error)
	CreateEntry(ctx context.Context, arg CreateEntryParams) (Entry, error)
	CreateTransfer(ctx context.Context, arg CreateTransferParams) (Transfer, error)
	CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
	DeleteAccount(ctx context.Context, id int32) error
	DeleteEntry(ctx context.Context, id int32) error
	DeleteTransfer(ctx context.Context, id int32) error
	DeleteUser(ctx context.Context, id int32) error
	GetAccount(ctx context.Context, id int32) (Account, error)
	GetAccountByPublicId(ctx context.Context, publicID string) (Account, error)
	GetAccountByUserId(ctx context.Context, userID int32) (Account, error)
	GetAccountForUpdate(ctx context.Context, id int32) (Account, error)
	GetAllUserAccounts(ctx context.Context, userID int32) ([]Account, error)
	GetEntry(ctx context.Context, id int32) (Entry, error)
	GetEntryByAccountId(ctx context.Context, accountID int32) (Entry, error)
	GetTransfer(ctx context.Context, id int32) (Transfer, error)
	GetTransferByFromAccountId(ctx context.Context, fromAccountID int32) (Transfer, error)
	GetTransferByToAccountId(ctx context.Context, toAccountID int32) (Transfer, error)
	GetUser(ctx context.Context, id int32) (User, error)
	GetUserByEmail(ctx context.Context, email string) (User, error)
	GetUserByPublicID(ctx context.Context, publicID string) (User, error)
	ListAccounts(ctx context.Context, arg ListAccountsParams) ([]Account, error)
	ListEntries(ctx context.Context, arg ListEntriesParams) ([]Entry, error)
	ListEntriesForAccountId(ctx context.Context, arg ListEntriesForAccountIdParams) ([]Entry, error)
	ListTransfers(ctx context.Context, arg ListTransfersParams) ([]Transfer, error)
	ListTransfersForFromAccountId(ctx context.Context, arg ListTransfersForFromAccountIdParams) ([]Transfer, error)
	ListTransfersForToAccountId(ctx context.Context, arg ListTransfersForToAccountIdParams) ([]Transfer, error)
	ListUsers(ctx context.Context, arg ListUsersParams) ([]User, error)
	UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error)
}

type Queries

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

func New

func New(db DBTX) *Queries

func (*Queries) AddAccountBalance

func (q *Queries) AddAccountBalance(ctx context.Context, arg AddAccountBalanceParams) (Account, error)

func (*Queries) CreateAccount

func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error)

func (*Queries) CreateEntry

func (q *Queries) CreateEntry(ctx context.Context, arg CreateEntryParams) (Entry, error)

func (*Queries) CreateTransfer

func (q *Queries) CreateTransfer(ctx context.Context, arg CreateTransferParams) (Transfer, error)

func (*Queries) CreateUser

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error)

func (*Queries) DeleteAccount

func (q *Queries) DeleteAccount(ctx context.Context, id int32) error

func (*Queries) DeleteEntry

func (q *Queries) DeleteEntry(ctx context.Context, id int32) error

func (*Queries) DeleteTransfer

func (q *Queries) DeleteTransfer(ctx context.Context, id int32) error

func (*Queries) DeleteUser

func (q *Queries) DeleteUser(ctx context.Context, id int32) error

func (*Queries) GetAccount

func (q *Queries) GetAccount(ctx context.Context, id int32) (Account, error)

func (*Queries) GetAccountByPublicId

func (q *Queries) GetAccountByPublicId(ctx context.Context, publicID string) (Account, error)

func (*Queries) GetAccountByUserId

func (q *Queries) GetAccountByUserId(ctx context.Context, userID int32) (Account, error)

func (*Queries) GetAccountForUpdate

func (q *Queries) GetAccountForUpdate(ctx context.Context, id int32) (Account, error)

func (*Queries) GetAllUserAccounts

func (q *Queries) GetAllUserAccounts(ctx context.Context, userID int32) ([]Account, error)

func (*Queries) GetEntry

func (q *Queries) GetEntry(ctx context.Context, id int32) (Entry, error)

func (*Queries) GetEntryByAccountId

func (q *Queries) GetEntryByAccountId(ctx context.Context, accountID int32) (Entry, error)

func (*Queries) GetTransfer

func (q *Queries) GetTransfer(ctx context.Context, id int32) (Transfer, error)

func (*Queries) GetTransferByFromAccountId

func (q *Queries) GetTransferByFromAccountId(ctx context.Context, fromAccountID int32) (Transfer, error)

func (*Queries) GetTransferByToAccountId

func (q *Queries) GetTransferByToAccountId(ctx context.Context, toAccountID int32) (Transfer, error)

func (*Queries) GetUser

func (q *Queries) GetUser(ctx context.Context, id int32) (User, error)

func (*Queries) GetUserByEmail

func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error)

func (*Queries) GetUserByPublicID

func (q *Queries) GetUserByPublicID(ctx context.Context, publicID string) (User, error)

func (*Queries) ListAccounts

func (q *Queries) ListAccounts(ctx context.Context, arg ListAccountsParams) ([]Account, error)

func (*Queries) ListEntries

func (q *Queries) ListEntries(ctx context.Context, arg ListEntriesParams) ([]Entry, error)

func (*Queries) ListEntriesForAccountId

func (q *Queries) ListEntriesForAccountId(ctx context.Context, arg ListEntriesForAccountIdParams) ([]Entry, error)

func (*Queries) ListTransfers

func (q *Queries) ListTransfers(ctx context.Context, arg ListTransfersParams) ([]Transfer, error)

func (*Queries) ListTransfersForFromAccountId

func (q *Queries) ListTransfersForFromAccountId(ctx context.Context, arg ListTransfersForFromAccountIdParams) ([]Transfer, error)

func (*Queries) ListTransfersForToAccountId

func (q *Queries) ListTransfersForToAccountId(ctx context.Context, arg ListTransfersForToAccountIdParams) ([]Transfer, error)

func (*Queries) ListUsers

func (q *Queries) ListUsers(ctx context.Context, arg ListUsersParams) ([]User, error)

func (*Queries) UpdateAccount

func (q *Queries) UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error)

func (*Queries) WithTx

func (q *Queries) WithTx(tx *sql.Tx) *Queries

type SQLStore

type SQLStore struct {
	*Queries
	// contains filtered or unexported fields
}

SQLStore provide all functions to execute SQL queries and transactions

func (*SQLStore) TransferTx

func (store *SQLStore) TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error)

TransferTx performs money transfer from one account to another It creates a transfer record, add account entries and update accounts' balance Within a single transaction

type Store

type Store interface {
	TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error)
	Querier
}

Store provide all functions to execute db queries and transactions

func NewStore

func NewStore(db *sql.DB) Store

NewStore creates a new store

type Transfer

type Transfer struct {
	ID            int32        `json:"id"`
	FromAccountID int32        `json:"from_account_id"`
	ToAccountID   int32        `json:"to_account_id"`
	PublicID      string       `json:"public_id"`
	CreatedAt     time.Time    `json:"created_at"`
	UpdatedAt     time.Time    `json:"updated_at"`
	DeletedAt     sql.NullTime `json:"deleted_at"`
	// must be positive
	Amount int64 `json:"amount"`
}

type TransferTxParams

type TransferTxParams struct {
	FromAccountID int32 `json:"from_account_id"`
	ToAccountID   int32 `json:"to_account_id"`
	Amount        int32 `json:"amount"`
}

type TransferTxResult

type TransferTxResult struct {
	Transfer    Transfer `json:"transfer"`
	FromAccount Account  `json:"from_account"`
	ToAccount   Account  `json:"to_account"`
	FromEntry   Entry    `json:"from_entry"`
	ToEntry     Entry    `json:"to_entry"`
}

type UpdateAccountParams

type UpdateAccountParams struct {
	ID      int32 `json:"id"`
	Balance int64 `json:"balance"`
}

type User

type User struct {
	ID                       int32          `json:"id"`
	PublicID                 string         `json:"public_id"`
	IsBlocked                sql.NullBool   `json:"is_blocked"`
	BlockedAt                sql.NullTime   `json:"blocked_at"`
	CreatedAt                time.Time      `json:"created_at"`
	UpdatedAt                time.Time      `json:"updated_at"`
	DeletedAt                sql.NullTime   `json:"deleted_at"`
	Firstname                string         `json:"firstname"`
	Lastname                 string         `json:"lastname"`
	Email                    string         `json:"email"`
	Password                 string         `json:"password"`
	Salt                     string         `json:"salt"`
	SecurityToken            sql.NullString `json:"security_token"`
	EmailConfirmed           sql.NullBool   `json:"email_confirmed"`
	SecurityTokenRequestedAt sql.NullTime   `json:"security_token_requested_at"`
}

Jump to

Keyboard shortcuts

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