db

package
v0.0.0-...-678ecff Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2019 License: AGPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TRANSACTION_ALL = iota
	TRANSACTION_INCOMING
	TRANSACTION_OUTGOING
)

Variables

View Source
var (
	// ErrInvalidBudgetRatioSet is the error returned when encountering an invalid set of budgets & ratios
	ErrInvalidBudgetRatioSet = errors.New("Budget & ratio sets have different sizes")
	// ErrInvalidRatio is the error returned when encountering an invalid ratio
	ErrInvalidRatio = errors.New("Ratios contain non-numerical characters or don't add up to 100%")
)
View Source
var (
	// SchemaVersion needs to be incremented every time the SQL schema changes
	SchemaVersion = 1

	// ErrInvalidID is the error returned when encountering an invalid database ID
	ErrInvalidID = errors.New("Invalid ID")
)

Functions

func GetDatabase

func GetDatabase() *sql.DB

GetDatabase connects to the database on first run and returns the existing connection on further calls

func InitDatabase

func InitDatabase()

InitDatabase sets up the database with all required tables and indexes

func SetupPostgres

func SetupPostgres(pc config.PostgreSQLConnection)

SetupPostgres sets the db configuration

func UUID

func UUID() (string, error)

UUID returns a new unique identifier

func WipeDatabase

func WipeDatabase()

WipeDatabase drops all database tables - use carefully!

Types

type APIContext

type APIContext struct {
	Config config.Data

	Queries []PgQuery

	Auth *User
	// contains filtered or unexported fields
}

APIContext is API's central context

func (*APIContext) Authentication

func (context *APIContext) Authentication(request *restful.Request) (interface{}, error)

Authentication parses the request for an access-/authtoken and returns the matching user

func (*APIContext) Begin

func (context *APIContext) Begin() (*APIContextTx, error)

Begin returns a new API transactional context

func (*APIContext) BuildImageURL

func (context *APIContext) BuildImageURL(id string, placeholder string) string

BuildImageURL returns the canonical URL for an image

func (*APIContext) Exec

func (context *APIContext) Exec(query string, args ...interface{}) (sql.Result, error)

Exec runs a postgres Exec

func (*APIContext) GetBudgetByUUID

func (context *APIContext) GetBudgetByUUID(uuid string) (Budget, error)

GetBudgetByUUID returns a budget by UUID from the cache

func (*APIContext) GetCodeByID

func (context *APIContext) GetCodeByID(id int64) (Code, error)

GetCodeByID returns a code by ID from the cache

func (*APIContext) GetProjectByID

func (context *APIContext) GetProjectByID(id int64) (Project, error)

GetProjectByID loads a project by ID from the database

func (*APIContext) GetProjectByUUID

func (context *APIContext) GetProjectByUUID(uuid string) (Project, error)

GetProjectByUUID returns a project by UUID from the cache

func (*APIContext) GetUserByAccessToken

func (context *APIContext) GetUserByAccessToken(token string) (interface{}, error)

GetUserByAccessToken loads a user by accesstoken from the database

func (*APIContext) GetUserByEmail

func (context *APIContext) GetUserByEmail(email string) (User, error)

GetUserByEmail loads a user by email from the database

func (*APIContext) GetUserByNameAndPassword

func (context *APIContext) GetUserByNameAndPassword(name, password string) (User, error)

GetUserByNameAndPassword loads a user by name & password from the database

func (*APIContext) GetUserByUUID

func (context *APIContext) GetUserByUUID(uuid string) (User, error)

GetUserByUUID returns a user by UUID from the cache

func (*APIContext) LatestPaymentFromSource

func (context *APIContext) LatestPaymentFromSource(source string) (Payment, error)

func (*APIContext) LoadAllBudgets

func (context *APIContext) LoadAllBudgets() ([]Budget, error)

LoadAllBudgets loads all budgets from the database

func (*APIContext) LoadAllCodes

func (context *APIContext) LoadAllCodes() ([]Code, error)

LoadAllCodes loads all codes from the database

func (*APIContext) LoadAllProjects

func (context *APIContext) LoadAllProjects() ([]Project, error)

LoadAllProjects loads all projects from the database

func (*APIContext) LoadAllUsers

func (context *APIContext) LoadAllUsers() ([]User, error)

LoadAllUsers loads all users from the database

func (*APIContext) LoadBudgetByID

func (context *APIContext) LoadBudgetByID(id int64) (Budget, error)

LoadBudgetByID loads a budget by UUID from the database

func (*APIContext) LoadBudgetByUUID

func (context *APIContext) LoadBudgetByUUID(uuid string) (Budget, error)

LoadBudgetByUUID loads a budget by UUID from the database

func (*APIContext) LoadBudgets

func (context *APIContext) LoadBudgets(project *Project) ([]Budget, error)

LoadBudgets loads all budgets for a project

func (*APIContext) LoadCodeByBudgetUUID

func (context *APIContext) LoadCodeByBudgetUUID(budgetID string) (Code, error)

LoadCodeByBudgetID loads a code by a single budgetID from the database

func (*APIContext) LoadCodeByBudgetsAndRatios

func (context *APIContext) LoadCodeByBudgetsAndRatios(budgetIDs, ratios StringSlice, userID string) (Code, error)

LoadCodeByBudgetsAndRatios loads a code by budgetIDs and their ratios from the database

func (*APIContext) LoadCodeByCode

func (context *APIContext) LoadCodeByCode(c string) (Code, error)

LoadCodeByCode loads a code by Code from the database

func (*APIContext) LoadCodeByID

func (context *APIContext) LoadCodeByID(id int64) (Code, error)

LoadCodeByID loads a code by ID from the database

func (*APIContext) LoadImage

func (context *APIContext) LoadImage(id string) ([]byte, error)

LoadImage reads an image from disk

func (*APIContext) LoadPaymentByID

func (context *APIContext) LoadPaymentByID(id int64) (Payment, error)

LoadPaymentByID loads a payment by ID from the database

func (*APIContext) LoadPaymentsForDonor

func (context *APIContext) LoadPaymentsForDonor(donor string) ([]Payment, error)

LoadPaymentsForDonor loads all payments for a specific donor

func (*APIContext) LoadPendingPayments

func (context *APIContext) LoadPendingPayments(direction int) ([]Payment, error)

LoadPendingPayment loads all pending payments

func (*APIContext) LoadProjectBySlug

func (context *APIContext) LoadProjectBySlug(slug string) (Project, error)

LoadProjectBySlug loads a project by ID from the database

func (*APIContext) LoadProjectByUUID

func (context *APIContext) LoadProjectByUUID(uuid string) (Project, error)

LoadProjectByUUID loads a project by UUID from the database

func (*APIContext) LoadRootBudgetForProject

func (context *APIContext) LoadRootBudgetForProject(project *Project) (Budget, error)

LoadRootBudgetForProject loads the root budget for a project from the database

func (*APIContext) LoadStatistics

func (context *APIContext) LoadStatistics(projectID int64) (Statistics, error)

LoadStatistics loads statistics for a project from the database

func (*APIContext) LoadTransactionByID

func (context *APIContext) LoadTransactionByID(id int64) (Transaction, error)

LoadTransactionByID loads a transaction by ID from the database

func (*APIContext) LoadUserByID

func (context *APIContext) LoadUserByID(id int64) (User, error)

LoadUserByID loads a user by ID from the database

func (*APIContext) LoadUserByUUID

func (context *APIContext) LoadUserByUUID(uuid string) (User, error)

LoadUserByUUID loads a user by UUID from the database

func (*APIContext) LogSummary

func (context *APIContext) LogSummary()

LogSummary logs out the current context stats

func (*APIContext) NewAPIContext

func (context *APIContext) NewAPIContext() smolder.APIContext

NewAPIContext returns a new API context

func (*APIContext) Query

func (context *APIContext) Query(query string, args ...interface{}) (*sql.Rows, error)

Query runs a postgres Query

func (*APIContext) QueryRow

func (context *APIContext) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow runs a postgres QueryRow

func (*APIContext) Search

func (context *APIContext) Search(term string) (Search, error)

Search searches the database for projects, budgets & payments

func (*APIContext) SearchBudgets

func (context *APIContext) SearchBudgets(term string) ([]Budget, error)

SearchBudgets searches database for budgets

func (*APIContext) SearchCodes

func (context *APIContext) SearchCodes(term string) ([]Code, error)

SearchCodes searches database for codes

func (*APIContext) SearchPayments

func (context *APIContext) SearchPayments(term string) ([]Payment, error)

SearchPayments searches database for payments

func (*APIContext) SearchProjects

func (context *APIContext) SearchProjects(term string) ([]Project, error)

SearchProjects searches database for projects

func (*APIContext) SetAuth

func (context *APIContext) SetAuth(auth interface{})

func (*APIContext) StoreImage

func (context *APIContext) StoreImage(logo []byte) (string, error)

StoreImage writes an image to disk and returns the hashsum

func (*APIContext) Transfer

func (context *APIContext) Transfer(fromBudget, toBudget int64, amount int64, purpose string, paymentID int64, ts time.Time) (Transaction, error)

type APIContextTx

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

APIContextTx is a transactional API conteollyxt

func (*APIContextTx) Commit

func (hTx *APIContextTx) Commit() error

Commit runs a postgres Commit in the transactional context

func (*APIContextTx) Exec

func (hTx *APIContextTx) Exec(query string, args ...interface{}) (sql.Result, error)

Exec runs a postgres Exec in the transactional context

func (*APIContextTx) Query

func (hTx *APIContextTx) Query(query string, args ...interface{}) (*sql.Rows, error)

Query runs a postgres Query in the transactional context

func (*APIContextTx) QueryRow

func (hTx *APIContextTx) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow runs a postgres QueryRow in the transactional context

func (*APIContextTx) Rollback

func (hTx *APIContextTx) Rollback() error

Rollback runs a postgres Rollback in the transactional context

type BigintSlice

type BigintSlice []int64

BigintSlice is a custom int64 slice for postgres encoding

func (*BigintSlice) Scan

func (s *BigintSlice) Scan(value interface{}) error

Scan converts a postgres value into an int64 slice

func (BigintSlice) Value

func (s BigintSlice) Value() (driver.Value, error)

Value constructs a single postgres query string from a slice of int64

type Budget

type Budget struct {
	ID             int64
	UUID           string
	ProjectID      *int64
	UserID         *int64
	ParentID       int64
	Name           string
	Description    string
	Private        bool
	PrivateBalance bool
}

Budget represents the db schema of a budget

func (*Budget) Balance

func (budget *Budget) Balance(context *APIContext) (int64, error)

Balance returns this budget's total balance

func (*Budget) BalanceStats

func (budget *Budget) BalanceStats(context *APIContext) ([]int64, error)

BalanceStats returns this budget's total balance for the past months

func (*Budget) Delete

func (budget *Budget) Delete(context *APIContext) error

Delete a budget from the database

func (*Budget) HasAccess

func (budget *Budget) HasAccess(user *User) bool

func (*Budget) HasTransactionAccess

func (budget *Budget) HasTransactionAccess(user *User) bool

func (*Budget) LoadPayments

func (budget *Budget) LoadPayments(context *APIContext) ([]Payment, error)

LoadPayments loads all payments for a budget

func (*Budget) LoadTransactions

func (budget *Budget) LoadTransactions(context *APIContext) ([]Transaction, error)

LoadTransactions loads all transactions for a budget

func (*Budget) Save

func (budget *Budget) Save(context *APIContext) error

Save a budget to the database

func (*Budget) Update

func (budget *Budget) Update(context *APIContext) error

Update a budget in the database

type BudgetRatioPair

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

BudgetRatioPair represents a pair of budgets and ratios

type BudgetSorter

type BudgetSorter BudgetRatioPair

BudgetSorter is used to sort the pair by ratio

func (BudgetSorter) Len

func (a BudgetSorter) Len() int

func (BudgetSorter) Less

func (a BudgetSorter) Less(i, j int) bool

func (BudgetSorter) Swap

func (a BudgetSorter) Swap(i, j int)

type Code

type Code struct {
	ID        int64
	Code      string
	BudgetIDs StringSlice
	Ratios    StringSlice
	UserID    *int64
}

Code represents the db schema of a code

type Payment

type Payment struct {
	ID                  int64
	BudgetID            int64
	CreatedAt           time.Time
	Amount              int64
	Currency            string
	Code                string
	Purpose             string
	RemoteAccount       string
	RemoteName          string
	RemoteTransactionID string
	RemoteBankID        string
	Source              string
	Pending             bool
}

Payment represents the db schema of a payment

func (*Payment) Process

func (payment *Payment) Process(context *APIContext, cutBudget int64) error

Process turns a payment into various budget transactions

func (*Payment) Save

func (payment *Payment) Save(context *APIContext) error

Save a payment to the database

func (*Payment) Update

func (payment *Payment) Update(context *APIContext) error

Update a payment in the database

type PgQuery

type PgQuery struct {
	Query    string
	Duration time.Duration
	TxID     int
}

PgQuery keeps stats for a single postgres query

type Project

type Project struct {
	ID             int64
	UUID           string
	Slug           string
	Name           string
	Summary        string
	About          string
	Website        string
	License        string
	Repository     string
	CreatedAt      time.Time
	Private        bool
	PrivateBalance bool
	ProcessingCut  int64
	Activated      bool
	UserID         *int64
}

Project represents the db schema of a project

func (*Project) Balance

func (project *Project) Balance(context *APIContext) (int64, error)

Balance returns this project's total balance

func (*Project) BalanceStats

func (project *Project) BalanceStats(context *APIContext) ([]int64, error)

BalanceStats returns this project's total balances for the past months

func (*Project) Contributors

func (project *Project) Contributors(context *APIContext) ([]User, error)

Contributors loads all contributors from the database

func (*Project) HasAccess

func (project *Project) HasAccess(user *User) bool

func (*Project) HasTransactionAccess

func (project *Project) HasTransactionAccess(user *User) bool

func (*Project) LoadTransactions

func (project *Project) LoadTransactions(context *APIContext) ([]Transaction, error)

LoadTransactions loads all transactions for a project

func (*Project) Save

func (project *Project) Save(context *APIContext) error

Save a project to the database

func (*Project) Update

func (project *Project) Update(context *APIContext) error

Update a project in the database

type Search struct {
	ID       string
	Projects []Project
	Budgets  []Budget
	Payments []Payment
}

Search represents the db schema of a search

type Statistics

type Statistics struct {
	ID            string
	ProjectID     int64
	BudgetID      int64
	MonthlyChange int64
	PastMonths    []int64
}

Statistics represents the db schema of a statistic

type StringSlice

type StringSlice []string

StringSlice is a custom string slice for postgres encoding

func (*StringSlice) Scan

func (s *StringSlice) Scan(value interface{}) error

Scan converts a postgres value into a string slice

func (StringSlice) Value

func (s StringSlice) Value() (driver.Value, error)

Value constructs a single postgres query string from a slice of strings

type Transaction

type Transaction struct {
	ID           int64
	BudgetID     int64
	FromBudgetID *int64
	ToBudgetID   *int64
	Amount       int64
	CreatedAt    time.Time
	Purpose      string
	PaymentID    *int64
}

Transaction represents the db schema of a transaction

func (*Transaction) Save

func (transaction *Transaction) Save(context *APIContext) error

Save a transaction to the database

type User

type User struct {
	ID        int64
	UUID      string
	Email     string
	Nickname  string
	About     string
	Address   StringSlice
	ZIP       string
	City      string
	Country   string
	Avatar    string
	Activated bool
	AuthToken StringSlice
}

User represents the db schema of a user

func (*User) Save

func (user *User) Save(context *APIContext) error

Save a user to the database

func (*User) Update

func (user *User) Update(context *APIContext) error

Update a user in the database

func (*User) UpdatePassword

func (user *User) UpdatePassword(context *APIContext, password string) error

UpdatePassword sets a new user password in the database

Jump to

Keyboard shortcuts

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