database

package
v0.0.0-...-1c68573 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Category

type Category struct {
	bun.BaseModel `bun:"table:categories"`

	ID   int64     `bun:"id,pk,autoincrement"`
	UUID uuid.UUID `bun:"uuid,type:uuid,notnull,default:uuid_generate_v4()"`

	Name string `bun:",notnull"`

	Owner   User  `bun:"rel:belongs-to,join:owner_id=id"`
	OwnerID int64 `bun:"owner_id,notnull"`
}

Category database model.

type CategoryQuerier

type CategoryQuerier interface {
	CreateCategory(ctx context.Context, c *Category) error
	CategoryExistsByUUID(ctx context.Context, uuid string) (bool, error)
	GetCategoryByUUID(ctx context.Context, uuid string, c *Category) error
	GetCategoriesByOwnerID(ctx context.Context, ownerID int64, c *[]Category) error
	UpdateCategory(ctx context.Context, c *Category) error
	DeleteCategoryByID(ctx context.Context, id int64) error
}

CategoryQuerier interface describes a type which executes database queries related to the Category model.

type Credentials

type Credentials struct {
	Host string
	Port int

	User     string
	Password string

	Database string
}

Credentials represents the credentials required to connect to a PostgreSQL database. It includes fields for the hostname, port number, username, password, and the name of the database to connect to.

type Currency

type Currency struct {
	bun.BaseModel `bun:"table:currencies"`

	ID int64 `bun:"id,pk,autoincrement"`

	Code   string  `bun:"code,notnull"`
	Symbol string  `bun:"symbol,notnull"`
	Rate   float64 `bun:"rate,notnull"`

	UpdatedAt time.Time `bun:",notnull,default:current_timestamp"`
}

Currency database model.

func (*Currency) BeforeAppendModel

func (c *Currency) BeforeAppendModel(_ context.Context, query bun.Query) error

type CurrencyQuerier

type CurrencyQuerier interface {
	GetCurrencyByCode(ctx context.Context, code string, c *Currency) error
	CreateCurrency(ctx context.Context, c *Currency) error
}

type Database

type Database interface {
	// TestConnection verifies the connection to the database.
	TestConnection() error

	// Init initializes the database with any necessary setup procedures.
	// It accepts a context.Context for handling cancellation signals.
	Init(ctx context.Context) error

	UserQuerier
	CategoryQuerier
	CurrencyQuerier
	TransactionQuerier
}

Database is an interface that abstracts the functionality of a database system. It provides methods for testing the connection to the database, initializing it, and accessing various data querying operations related to users, categories, currencies, and transactions.

type DefaultDatabase

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

DefaultDatabase is the default implementation of the Database interface. It encapsulates a client for interacting with the database.

func New

New creates a new instance of DefaultDatabase initialized with the provided credentials and returns a pointer to it. It establishes a connection to the PostgreSQL database using the provided credentials.

func (*DefaultDatabase) CategoryExistsByUUID

func (d *DefaultDatabase) CategoryExistsByUUID(ctx context.Context, uuid string) (bool, error)

func (*DefaultDatabase) CreateCategory

func (d *DefaultDatabase) CreateCategory(ctx context.Context, c *Category) error

func (*DefaultDatabase) CreateCurrency

func (d *DefaultDatabase) CreateCurrency(ctx context.Context, c *Currency) error

func (*DefaultDatabase) CreateTransaction

func (d *DefaultDatabase) CreateTransaction(ctx context.Context, t *Transaction) error

func (*DefaultDatabase) CreateUser

func (d *DefaultDatabase) CreateUser(ctx context.Context, u *User) error

func (*DefaultDatabase) DeleteCategoryByID

func (d *DefaultDatabase) DeleteCategoryByID(ctx context.Context, id int64) error

func (*DefaultDatabase) DeleteUserByUsername

func (d *DefaultDatabase) DeleteUserByUsername(ctx context.Context, username string) error

func (*DefaultDatabase) GetCategoriesByOwnerID

func (d *DefaultDatabase) GetCategoriesByOwnerID(ctx context.Context, ownerID int64, c *[]Category) error

func (*DefaultDatabase) GetCategoryByUUID

func (d *DefaultDatabase) GetCategoryByUUID(ctx context.Context, uuid string, c *Category) error

func (*DefaultDatabase) GetCurrencyByCode

func (d *DefaultDatabase) GetCurrencyByCode(ctx context.Context, code string, c *Currency) error

func (*DefaultDatabase) GetTransactionByUUID

func (d *DefaultDatabase) GetTransactionByUUID(ctx context.Context, uuid string, t *Transaction) error

func (*DefaultDatabase) GetUserByUsername

func (d *DefaultDatabase) GetUserByUsername(ctx context.Context, username string, u *User) error

func (*DefaultDatabase) Init

func (d *DefaultDatabase) Init(ctx context.Context) error

Init creates all necessary tables and extensions if they do not exist.

func (*DefaultDatabase) TestConnection

func (d *DefaultDatabase) TestConnection() error

TestConnection verifies database connection.

func (*DefaultDatabase) UpdateCategory

func (d *DefaultDatabase) UpdateCategory(ctx context.Context, c *Category) error

func (*DefaultDatabase) UserExistsByUsername

func (d *DefaultDatabase) UserExistsByUsername(ctx context.Context, username string) (bool, error)

type Transaction

type Transaction struct {
	bun.BaseModel `bun:"table:transactions"`

	ID   int64     `bun:"id,pk,autoincrement"`
	UUID uuid.UUID `bun:"uuid,type:uuid,notnull,default:uuid_generate_v4()"`

	Amount int32 `bun:"amount,notnull"`

	Currency   Currency `bun:"rel:belongs-to,join:currency_id=id"`
	CurrencyID int64    `bun:"currency_id,notnull"`

	Description string `bun:"description,nullzero"`

	Category   Category `bun:"rel:belongs-to,join:category_id=id"`
	CategoryID int64    `bun:"category_id,notnull"`

	Owner   User  `bun:"rel:belongs-to,join:owner_id=id"`
	OwnerID int64 `bun:"owner_id,notnull"`

	// Timestamp of the transaction (when it happened)
	// UTC is always used as the timezone.
	Timestamp time.Time `bun:",notnull"`

	CreatedAt time.Time `bun:",notnull,default:current_timestamp"`
	UpdatedAt time.Time `bun:",notnull,default:current_timestamp"`
}

Transaction database model.

func (*Transaction) BeforeAppendModel

func (t *Transaction) BeforeAppendModel(_ context.Context, query bun.Query) error

type TransactionQuerier

type TransactionQuerier interface {
	CreateTransaction(ctx context.Context, t *Transaction) error
	GetTransactionByUUID(ctx context.Context, uuid string, t *Transaction) error
}

TransactionQuerier interface describes a type which executes database queries related to the Transaction model.

type User

type User struct {
	bun.BaseModel `bun:"table:users"`

	ID int64 `bun:"id,pk,autoincrement"`

	Username string `bun:"username,notnull"`
	Password string `bun:"password,notnull"`
}

User database model.

type UserQuerier

type UserQuerier interface {
	CreateUser(ctx context.Context, u *User) error
	UserExistsByUsername(ctx context.Context, username string) (bool, error)
	GetUserByUsername(ctx context.Context, username string, u *User) error
	DeleteUserByUsername(ctx context.Context, username string) error
}

UserQuerier interface describes a type which executes database queries related to the User model.

Jump to

Keyboard shortcuts

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