213 lines
5.2 KiB
Go
213 lines
5.2 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// source: accounts.sql
|
|
|
|
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const createAccount = `-- name: CreateAccount :one
|
|
INSERT INTO accounts
|
|
(name, budget_id)
|
|
VALUES ($1, $2)
|
|
RETURNING id, budget_id, name, on_budget
|
|
`
|
|
|
|
type CreateAccountParams struct {
|
|
Name string
|
|
BudgetID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, createAccount, arg.Name, arg.BudgetID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.BudgetID,
|
|
&i.Name,
|
|
&i.OnBudget,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAccount = `-- name: GetAccount :one
|
|
SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts
|
|
WHERE accounts.id = $1
|
|
`
|
|
|
|
func (q *Queries) GetAccount(ctx context.Context, id uuid.UUID) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, getAccount, id)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.BudgetID,
|
|
&i.Name,
|
|
&i.OnBudget,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAccounts = `-- name: GetAccounts :many
|
|
SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts
|
|
WHERE accounts.budget_id = $1
|
|
ORDER BY accounts.name
|
|
`
|
|
|
|
func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Account, error) {
|
|
rows, err := q.db.QueryContext(ctx, getAccounts, budgetID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Account
|
|
for rows.Next() {
|
|
var i Account
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.BudgetID,
|
|
&i.Name,
|
|
&i.OnBudget,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getAccountsWithBalance = `-- name: GetAccountsWithBalance :many
|
|
SELECT accounts.id, accounts.name, accounts.on_budget,
|
|
(SELECT MAX(transactions.date) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.status = 'Reconciled')::date as last_reconciled,
|
|
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW())::decimal(12,2) as working_balance,
|
|
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status IN ('Cleared', 'Reconciled'))::decimal(12,2) as cleared_balance,
|
|
(SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status = 'Reconciled')::decimal(12,2) as reconciled_balance
|
|
FROM accounts
|
|
WHERE accounts.budget_id = $1
|
|
ORDER BY accounts.name
|
|
`
|
|
|
|
type GetAccountsWithBalanceRow struct {
|
|
ID uuid.UUID
|
|
Name string
|
|
OnBudget bool
|
|
LastReconciled time.Time
|
|
WorkingBalance numeric.Numeric
|
|
ClearedBalance numeric.Numeric
|
|
ReconciledBalance numeric.Numeric
|
|
}
|
|
|
|
func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID) ([]GetAccountsWithBalanceRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getAccountsWithBalance, budgetID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetAccountsWithBalanceRow
|
|
for rows.Next() {
|
|
var i GetAccountsWithBalanceRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.OnBudget,
|
|
&i.LastReconciled,
|
|
&i.WorkingBalance,
|
|
&i.ClearedBalance,
|
|
&i.ReconciledBalance,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const searchAccounts = `-- name: SearchAccounts :many
|
|
SELECT accounts.id, accounts.budget_id, accounts.name, 'account' as type FROM accounts
|
|
WHERE accounts.budget_id = $1
|
|
AND accounts.name LIKE $2
|
|
ORDER BY accounts.name
|
|
`
|
|
|
|
type SearchAccountsParams struct {
|
|
BudgetID uuid.UUID
|
|
Search string
|
|
}
|
|
|
|
type SearchAccountsRow struct {
|
|
ID uuid.UUID
|
|
BudgetID uuid.UUID
|
|
Name string
|
|
Type interface{}
|
|
}
|
|
|
|
func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams) ([]SearchAccountsRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, searchAccounts, arg.BudgetID, arg.Search)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []SearchAccountsRow
|
|
for rows.Next() {
|
|
var i SearchAccountsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.BudgetID,
|
|
&i.Name,
|
|
&i.Type,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateAccount = `-- name: UpdateAccount :one
|
|
UPDATE accounts
|
|
SET name = $1,
|
|
on_budget = $2
|
|
WHERE accounts.id = $3
|
|
RETURNING id, budget_id, name, on_budget
|
|
`
|
|
|
|
type UpdateAccountParams struct {
|
|
Name string
|
|
OnBudget bool
|
|
ID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error) {
|
|
row := q.db.QueryRowContext(ctx, updateAccount, arg.Name, arg.OnBudget, arg.ID)
|
|
var i Account
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.BudgetID,
|
|
&i.Name,
|
|
&i.OnBudget,
|
|
)
|
|
return i, err
|
|
}
|