db

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

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ForeignKeyViolation = "23503"
	UniqueViolation     = "23505"
)

Variables

View Source
var ErrRecordNotFound = sql.ErrNoRows
View Source
var ErrUniqueViolation = &pgconn.PgError{
	Code: UniqueViolation,
}

Functions

func ErrorCode

func ErrorCode(err error) string

func RunDBMigrations

func RunDBMigrations(db *sql.DB, migrationsURL string)

Types

type Account

type Account struct {
	ID        int64     `json:"id"`
	Owner     string    `json:"owner"`
	Balance   int64     `json:"balance"`
	Unit      string    `json:"unit"`
	CreatedAt time.Time `json:"created_at"`
}

type AddAccountBalanceParams

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

type CreateAccountParams

type CreateAccountParams struct {
	Owner   string `json:"owner"`
	Balance int64  `json:"balance"`
	Unit    string `json:"unit"`
}

type CreateEntryParams

type CreateEntryParams struct {
	AccountID int64 `json:"account_id"`
	Amount    int64 `json:"amount"`
}

type CreateSessionParams

type CreateSessionParams struct {
	ID           uuid.UUID `json:"id"`
	Username     string    `json:"username"`
	RefreshToken string    `json:"refresh_token"`
	UserAgent    string    `json:"user_agent"`
	ClientIp     string    `json:"client_ip"`
	IsBlocked    bool      `json:"is_blocked"`
	ExpiresAt    time.Time `json:"expires_at"`
	CreatedAt    time.Time `json:"created_at"`
}

type CreateTransferParams

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

type CreateUserParams

type CreateUserParams struct {
	Username       string `json:"username"`
	HashedPassword string `json:"hashed_password"`
	FullName       string `json:"fullname"`
	Email          string `json:"email"`
}

type CreateUserTxParams

type CreateUserTxParams struct {
	CreateUserParams // Embedding CreateUserParams for reuse
	AfterCreate      func(user User) error
}

CreateUserTxParams represents the parameters for creating a user transaction.

type CreateUserTxResult

type CreateUserTxResult struct {
	User User
}

CreateUserTxResult represents the result of creating a user transaction.

type CreateVerifyEmail

type CreateVerifyEmail struct {
	Username   string `json:"username"`
	Email      string `json:"email"`
	SecretCode string `json:"secret_code"`
}

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
}

DBTX defines the interface for database transactions.

type Entry

type Entry struct {
	ID        int64 `json:"id"`
	AccountID int64 `json:"account_id"`
	// can be negative or positive
	Amount    int64     `json:"amount"`
	CreatedAt time.Time `json:"created_at"`
}

type ListAccountsParams

type ListAccountsParams struct {
	Owner  string `json:"owner"`
	Limit  int32  `json:"limit"`
	Offset int32  `json:"offset"`
}

type ListEntriesParams

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

type ListTransfersParams

type ListTransfersParams struct {
	FromAccountID int64 `json:"from_account_id"`
	ToAccountID   int64 `json:"to_account_id"`
	Limit         int32 `json:"limit"`
	Offset        int32 `json:"offset"`
}

type Querier

type Querier interface {
	// User queries
	CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
	GetUser(ctx context.Context, username string) (User, error)
	UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)
	// Session queries
	GetSession(ctx context.Context, id uuid.UUID) (Session, error)
	CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)
	// Email queries
	CreateVerifyEmail(ctx context.Context, arg CreateVerifyEmail) (VerifyEmail, error)
	// Account queries
	AddAccountBalance(ctx context.Context, arg AddAccountBalanceParams) (Account, error)
	CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error)
	DeleteAccount(ctx context.Context, id int64) error
	GetAccount(ctx context.Context, id int64) (Account, error)
	ListAccounts(ctx context.Context, arg ListAccountsParams) ([]Account, error)
	UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error)
	// Transfer queries
	CreateTransfer(ctx context.Context, arg CreateTransferParams) (Transfer, error)
	GetTransfer(ctx context.Context, id int64) (Transfer, error)
	ListTransfers(ctx context.Context, arg ListTransfersParams) ([]Transfer, error)
	// Entry queries
	CreateEntry(ctx context.Context, arg CreateEntryParams) (Entry, error)
	GetEntry(ctx context.Context, id int64) (Entry, error)
	ListEntries(ctx context.Context, arg ListEntriesParams) ([]Entry, error)
}

type Queries

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

Queries provides methods for executing database queries.

func New

func New(db DBTX) *Queries

New creates a new instance of Queries using the provided database transaction.

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) CreateSession

func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, 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) CreateVerifyEmail

func (q *Queries) CreateVerifyEmail(ctx context.Context, arg CreateVerifyEmail) (VerifyEmail, error)

func (*Queries) DeleteAccount

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

func (*Queries) GetAccount

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

func (*Queries) GetEntry

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

func (*Queries) GetSession

func (q *Queries) GetSession(ctx context.Context, id uuid.UUID) (Session, error)

func (*Queries) GetTransfer

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

func (*Queries) GetUser

func (q *Queries) GetUser(ctx context.Context, username 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) ListTransfers

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

func (*Queries) UpdatVerifyEmail

func (q *Queries) UpdatVerifyEmail(ctx context.Context, arg UpdatVerifyEmailParams) (VerifyEmail, error)

func (*Queries) UpdateAccount

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

func (*Queries) UpdateUser

func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)

type SQLStore

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

SQLStore provides all functions to execute db queries and transactions

func (*SQLStore) CreateUserTx

func (store *SQLStore) CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)

CreateUserTx executes a transaction for creating a user.

func (*SQLStore) TransferTx

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

TransferTx performs a money/energy unit transfer from one account to another. It creates a new transfer record, adds new account entries, and updates account balances in a single database transaction.

func (*SQLStore) VerifyEmailTx

func (store *SQLStore) VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error)

type Session

type Session struct {
	ID           uuid.UUID `json:"id"`
	Username     string    `json:"username"`
	RefreshToken string    `json:"refresh_token"`
	UserAgent    string    `json:"user_agent"`
	ClientIp     string    `json:"client_ip"`
	IsBlocked    bool      `json:"is_blocked"`
	ExpiresAt    time.Time `json:"expires_at"`
	CreatedAt    time.Time `json:"created_at"`
}

type Store

type Store interface {
	Querier
	TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error)
	CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)
	VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error)
}

SQLStore provides all functions to execute db queries and transactions

func InitDatabase

func InitDatabase(config util.Config) Store

InitDatabase initializes the database connection and performs migrations.

type Transfer

type Transfer struct {
	ID            int64 `json:"id"`
	FromAccountID int64 `json:"from_account_id"`
	ToAccountID   int64 `json:"to_account_id"`
	// it must be positive
	Amount    int64     `json:"amount"`
	CreatedAt time.Time `json:"created_at"`
}

type TransferTxParams

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

TransferTxParams contains the input parameters of the transfer transaction

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"`
}

TransferTxResult is the result of the tranfer transaction

type UpdatVerifyEmailParams

type UpdatVerifyEmailParams struct {
	ID         int64  `json:"ID"`
	SecretCode string `json:"secret_code"`
}

type UpdateAccountParams

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

type UpdateUserParams

type UpdateUserParams struct {
	HashedPassword    sql.NullString `json:"hashed_password"`
	PasswordChangedAt sql.NullTime   `json:"password_changed_at"`
	FullName          sql.NullString `json:"fullname"`
	Email             sql.NullString `json:"email"`
	Username          string         `json:"username"`
	IsEmailVerified   sql.NullBool   `json:"is_email_verified"`
}

type User

type User struct {
	Username          string    `json:"username"`
	HashedPassword    string    `json:"hashed_password"`
	FullName          string    `json:"full_name"`
	Email             string    `json:"email"`
	PasswordChangedAt time.Time `json:"password_changed_at"`
	CreatedAt         time.Time `json:"created_at"`
	IsEmailVerified   bool      `json:"is_email_verified"`
	Role              string    `json:"role"`
}

type VerifyEmail

type VerifyEmail struct {
	ID         int64     `json:"id"`
	Username   string    `json:"username"`
	Email      string    `json:"email"`
	SecretCode string    `json:"secret_code"`
	IsUsed     bool      `json:"is_used"`
	CreatedAt  time.Time `json:"created_at"`
	ExpiredAt  time.Time `json:"expired_at"`
}

type VerifyEmailTxParams

type VerifyEmailTxParams struct {
	EmailId    int64
	SecretCode string
}

type VerifyEmailTxResult

type VerifyEmailTxResult struct {
	User        User
	VerifyEmail VerifyEmail
}

Jump to

Keyboard shortcuts

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