pg

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package pg provides a PostgreSQL implementation for DB

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host     string `env:"DB_PG_HOST" envDefault:"localhost"`
	Port     int    `env:"DB_PG_PORT" envDefault:"5432"`
	User     string `env:"DB_PG_USER" envDefault:"postgres"`
	Password string `env:"DB_PG_PASSWORD"`
	Database string `env:"DB_PG_DATABASE" envDefault:"postgres"`
}

type DB

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

func NewDB

func NewDB(config Config, log logrus.FieldLogger) (*DB, error)

NewDB creates a new connection to the db and pings it

func (DB) AddIncome

func (db DB) AddIncome(ctx context.Context, args common.AddIncomeArgs) (id uint, err error)

AddIncome adds a new income with passed params

func (DB) AddMonthlyPayment

func (db DB) AddMonthlyPayment(ctx context.Context, args common.AddMonthlyPaymentArgs) (id uint, err error)

AddMonthlyPayment adds new Monthly Payment

func (DB) AddSpend

func (db DB) AddSpend(ctx context.Context, args common.AddSpendArgs) (id uint, err error)

AddSpend adds a new Spend

func (DB) AddSpendType

func (db DB) AddSpendType(ctx context.Context, args common.AddSpendTypeArgs) (id uint, err error)

AddSpendType adds new Spend Type

func (*DB) DropDB

func (db *DB) DropDB() error

DropDB drops all tables and relations. USE ONLY IN TESTS!

func (DB) EditIncome

func (db DB) EditIncome(ctx context.Context, args common.EditIncomeArgs) error

EditIncome edits income with passed id, nil args are ignored

func (DB) EditMonthlyPayment

func (db DB) EditMonthlyPayment(ctx context.Context, args common.EditMonthlyPaymentArgs) error

EditMonthlyPayment modifies existing Monthly Payment

func (DB) EditSpend

func (db DB) EditSpend(ctx context.Context, args common.EditSpendArgs) error

EditSpend edits existeng Spend

func (DB) EditSpendType

func (db DB) EditSpendType(ctx context.Context, args common.EditSpendTypeArgs) error

EditSpendType modifies existing Spend Type

func (DB) GetDay

func (db DB) GetDay(ctx context.Context, id uint) (common.Day, error)

func (DB) GetDayIDByDate

func (db DB) GetDayIDByDate(ctx context.Context, year int, month int, day int) (id uint, err error)

func (DB) GetMonth

func (db DB) GetMonth(ctx context.Context, id uint) (common.Month, error)

func (DB) GetMonthID

func (db DB) GetMonthID(ctx context.Context, year, month int) (id uint, err error)

func (DB) GetMonths

func (db DB) GetMonths(ctx context.Context, years ...int) ([]common.Month, error)

GetMonths returns months of passed years. Months doesn't contains relations (Incomes, Days and etc.)

func (DB) GetSpendType

func (db DB) GetSpendType(ctx context.Context, id uint) (common.SpendType, error)

GetSpendType returns Spend Type with passed id

func (DB) GetSpendTypes

func (db DB) GetSpendTypes(ctx context.Context) ([]common.SpendType, error)

GetSpendTypes returns all Spend Types

func (*DB) Prepare

func (db *DB) Prepare() error

Prepare prepares the database:

  • create tables
  • init tables (add days for current month if needed)
  • run some subproccess

func (DB) RemoveIncome

func (db DB) RemoveIncome(ctx context.Context, id uint) error

RemoveIncome removes income with passed id

func (DB) RemoveMonthlyPayment

func (db DB) RemoveMonthlyPayment(ctx context.Context, id uint) error

RemoveMonthlyPayment removes Monthly Payment with passed id

func (DB) RemoveSpend

func (db DB) RemoveSpend(ctx context.Context, id uint) error

RemoveSpend removes Spend with passed id

func (DB) RemoveSpendType

func (db DB) RemoveSpendType(ctx context.Context, id uint) error

RemoveSpendType removes Spend Type with passed id

func (DB) SearchSpends added in v0.2.0

func (db DB) SearchSpends(ctx context.Context, args common.SearchSpendsArgs) ([]common.Spend, error)

func (*DB) Shutdown

func (db *DB) Shutdown() error

Shutdown closes the connection to the db

type Day added in v0.2.0

type Day struct {
	ID uint `pg:"id,pk"`

	// MonthID is a foreign key to 'months' table
	MonthID uint `pg:"month_id"`

	Day int `pg:"day"`
	// Saldo is a DailyBudget - Cost of all Spends multiplied by 100 (can be negative)
	Saldo  money.Money `pg:"saldo,use_zero"`
	Spends []Spend     `pg:"rel:has-many,join_fk:day_id"`
	// contains filtered or unexported fields
}

Day represents day entity in PostgreSQL db

func (Day) ToCommon added in v0.2.0

func (d Day) ToCommon(year int, month time.Month) common.Day

ToCommon converts Day to common Day structure from "github.com/ShoshinNikita/budget-manager/internal/db" package

type Income added in v0.2.0

type Income struct {
	ID uint `pg:"id,pk"`

	// MonthID is a foreign key to 'months' table
	MonthID uint `pg:"month_id"`

	Title  string      `pg:"title"`
	Notes  string      `pg:"notes"`
	Income money.Money `pg:"income"`
	// contains filtered or unexported fields
}

Income represents income entity in PostgreSQL db

func (Income) ToCommon added in v0.2.0

func (in Income) ToCommon(year int, month time.Month) common.Income

ToCommon converts Income to common Income structure from "github.com/ShoshinNikita/budget-manager/internal/db" package

type Month added in v0.2.0

type Month struct {
	ID uint `pg:"id,pk"`

	Year  int        `pg:"year"`
	Month time.Month `pg:"month"`

	Incomes         []Income         `pg:"rel:has-many,join_fk:month_id"`
	MonthlyPayments []MonthlyPayment `pg:"rel:has-many,join_fk:month_id"`

	// DailyBudget is a (TotalIncome - Cost of Monthly Payments) / Number of Days
	DailyBudget money.Money `pg:"daily_budget,use_zero"`
	Days        []Day       `pg:"rel:has-many,join_fk:month_id"`

	TotalIncome money.Money `pg:"total_income,use_zero"`
	// TotalSpend is a cost of all Monthly Payments and Spends
	TotalSpend money.Money `pg:"total_spend,use_zero"`
	// Result is TotalIncome - TotalSpend
	Result money.Money `pg:"result,use_zero"`
	// contains filtered or unexported fields
}

Month represents month entity in PostgreSQL db

func (Month) ToCommon added in v0.2.0

func (m Month) ToCommon() common.Month

ToCommon converts Month to common Month structure from "github.com/ShoshinNikita/budget-manager/internal/db" package

type MonthlyPayment added in v0.2.0

type MonthlyPayment struct {
	ID uint `pg:"id,pk"`

	// MonthID is a foreign key to 'months' table
	MonthID uint `pg:"month_id"`

	Title  string      `pg:"title"`
	TypeID uint        `pg:"type_id"`
	Type   *SpendType  `pg:"rel:has-one,fk:type_id"`
	Notes  string      `pg:"notes"`
	Cost   money.Money `pg:"cost"`
	// contains filtered or unexported fields
}

MonthlyPayment represents monthly payment entity in PostgreSQL db

func (MonthlyPayment) ToCommon added in v0.2.0

func (mp MonthlyPayment) ToCommon(year int, month time.Month) common.MonthlyPayment

ToCommon converts MonthlyPayment to common MonthlyPayment structure from "github.com/ShoshinNikita/budget-manager/internal/db" package

type Spend added in v0.2.0

type Spend struct {

	// DayID is a foreign key to 'days' table
	DayID uint `pg:"day_id"`

	ID uint `pg:"id,pk"`

	Title  string      `pg:"title"`
	TypeID uint        `pg:"type_id"`
	Type   *SpendType  `pg:"rel:has-one,fk:type_id"`
	Notes  string      `pg:"notes"`
	Cost   money.Money `pg:"cost,use_zero"`
	// contains filtered or unexported fields
}

Spend represents spend entity in PostgreSQL db

func (Spend) ToCommon added in v0.2.0

func (s Spend) ToCommon(year int, month time.Month, day int) common.Spend

ToCommon converts Spend to common Spend structure from "github.com/ShoshinNikita/budget-manager/internal/db" package

type SpendType added in v0.2.0

type SpendType struct {
	ID       uint   `pg:"id,pk"`
	Name     string `pg:"name"`
	ParentID uint   `pg:"parent_id"`
	// contains filtered or unexported fields
}

SpendType represents spend type entity in PostgreSQL db

func (*SpendType) ToCommon added in v0.2.0

func (s *SpendType) ToCommon() *common.SpendType

ToCommon converts SpendType to common SpendType structure from "github.com/ShoshinNikita/budget-manager/internal/db" package

We return a pointer instead of a value unlike other 'ToCommon' methods because Spend Type can be optional

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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