Extract view for transactions
This commit is contained in:
parent
16bcf516f6
commit
81aacf339e
@ -72,6 +72,24 @@ type CategoryGroup struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type DisplayTransaction struct {
|
||||
ID uuid.UUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount numeric.Numeric
|
||||
GroupID uuid.NullUUID
|
||||
Status TransactionStatus
|
||||
Account string
|
||||
PayeeID uuid.NullUUID
|
||||
CategoryID uuid.NullUUID
|
||||
Payee string
|
||||
CategoryGroup string
|
||||
Category string
|
||||
TransferAccount string
|
||||
BudgetID uuid.UUID
|
||||
AccountID uuid.UUID
|
||||
}
|
||||
|
||||
type Payee struct {
|
||||
ID uuid.UUID
|
||||
BudgetID uuid.UUID
|
||||
|
@ -27,47 +27,15 @@ DELETE FROM transactions
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: GetAllTransactionsForBudget :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo,
|
||||
transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, transactions.payee_id, transactions.category_id,
|
||||
COALESCE(payees.name, '') as payee,
|
||||
COALESCE(category_groups.name, '') as category_group,
|
||||
COALESCE(categories.name, '') as category,
|
||||
COALESCE((
|
||||
SELECT CONCAT(otherAccounts.name)
|
||||
FROM transactions otherTransactions
|
||||
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
|
||||
WHERE otherTransactions.group_id = transactions.group_id
|
||||
AND otherTransactions.id != transactions.id
|
||||
), '')::text as transfer_account
|
||||
FROM transactions
|
||||
INNER JOIN accounts ON accounts.id = transactions.account_id
|
||||
LEFT JOIN payees ON payees.id = transactions.payee_id
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
WHERE accounts.budget_id = $1
|
||||
SELECT t.*
|
||||
FROM display_transactions AS t
|
||||
WHERE t.budget_id = $1
|
||||
ORDER BY transactions.date DESC;
|
||||
|
||||
-- name: GetTransactionsForAccount :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo,
|
||||
transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, transactions.payee_id, transactions.category_id,
|
||||
COALESCE(payees.name, '') as payee,
|
||||
COALESCE(category_groups.name, '') as category_group,
|
||||
COALESCE(categories.name, '') as category,
|
||||
COALESCE((
|
||||
SELECT CONCAT(otherAccounts.name)
|
||||
FROM transactions otherTransactions
|
||||
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
|
||||
WHERE otherTransactions.group_id = transactions.group_id
|
||||
AND otherTransactions.id != transactions.id
|
||||
), '')::text as transfer_account
|
||||
FROM transactions
|
||||
INNER JOIN accounts ON accounts.id = transactions.account_id
|
||||
LEFT JOIN payees ON payees.id = transactions.payee_id
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
WHERE transactions.account_id = $1
|
||||
SELECT t.*
|
||||
FROM display_transactions AS t
|
||||
WHERE t.account_id = $1
|
||||
ORDER BY transactions.date DESC
|
||||
LIMIT 200;
|
||||
|
||||
|
25
postgres/schema/0015_transactions-view.sql
Normal file
25
postgres/schema/0015_transactions-view.sql
Normal file
@ -0,0 +1,25 @@
|
||||
-- +goose Up
|
||||
CREATE VIEW display_transactions AS
|
||||
SELECT transactions.id, transactions.date, transactions.memo,
|
||||
transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, transactions.payee_id, transactions.category_id,
|
||||
COALESCE(payees.name, '') as payee,
|
||||
COALESCE(category_groups.name, '') as category_group,
|
||||
COALESCE(categories.name, '') as category,
|
||||
COALESCE((
|
||||
SELECT CONCAT(otherAccounts.name)
|
||||
FROM transactions otherTransactions
|
||||
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
|
||||
WHERE otherTransactions.group_id = transactions.group_id
|
||||
AND otherTransactions.id != transactions.id
|
||||
), '')::text as transfer_account,
|
||||
accounts.budget_id, transactions.account_id
|
||||
FROM transactions
|
||||
INNER JOIN accounts ON accounts.id = transactions.account_id
|
||||
LEFT JOIN payees ON payees.id = transactions.payee_id
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
ORDER BY transactions.date DESC;
|
||||
|
||||
-- +goose Down
|
||||
DROP VIEW display_transactions;
|
@ -81,53 +81,21 @@ func (q *Queries) DeleteTransaction(ctx context.Context, id uuid.UUID) error {
|
||||
}
|
||||
|
||||
const getAllTransactionsForBudget = `-- name: GetAllTransactionsForBudget :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo,
|
||||
transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, transactions.payee_id, transactions.category_id,
|
||||
COALESCE(payees.name, '') as payee,
|
||||
COALESCE(category_groups.name, '') as category_group,
|
||||
COALESCE(categories.name, '') as category,
|
||||
COALESCE((
|
||||
SELECT CONCAT(otherAccounts.name)
|
||||
FROM transactions otherTransactions
|
||||
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
|
||||
WHERE otherTransactions.group_id = transactions.group_id
|
||||
AND otherTransactions.id != transactions.id
|
||||
), '')::text as transfer_account
|
||||
FROM transactions
|
||||
INNER JOIN accounts ON accounts.id = transactions.account_id
|
||||
LEFT JOIN payees ON payees.id = transactions.payee_id
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
WHERE accounts.budget_id = $1
|
||||
SELECT t.id, t.date, t.memo, t.amount, t.group_id, t.status, t.account, t.payee_id, t.category_id, t.payee, t.category_group, t.category, t.transfer_account, t.budget_id, t.account_id
|
||||
FROM display_transactions AS t
|
||||
WHERE t.budget_id = $1
|
||||
ORDER BY transactions.date DESC
|
||||
`
|
||||
|
||||
type GetAllTransactionsForBudgetRow struct {
|
||||
ID uuid.UUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount numeric.Numeric
|
||||
GroupID uuid.NullUUID
|
||||
Status TransactionStatus
|
||||
Account string
|
||||
PayeeID uuid.NullUUID
|
||||
CategoryID uuid.NullUUID
|
||||
Payee string
|
||||
CategoryGroup string
|
||||
Category string
|
||||
TransferAccount string
|
||||
}
|
||||
|
||||
func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]GetAllTransactionsForBudgetRow, error) {
|
||||
func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]DisplayTransaction, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getAllTransactionsForBudget, budgetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetAllTransactionsForBudgetRow
|
||||
var items []DisplayTransaction
|
||||
for rows.Next() {
|
||||
var i GetAllTransactionsForBudgetRow
|
||||
var i DisplayTransaction
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Date,
|
||||
@ -142,6 +110,8 @@ func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid
|
||||
&i.CategoryGroup,
|
||||
&i.Category,
|
||||
&i.TransferAccount,
|
||||
&i.BudgetID,
|
||||
&i.AccountID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -213,54 +183,22 @@ func (q *Queries) GetTransactionsByMonthAndCategory(ctx context.Context, budgetI
|
||||
}
|
||||
|
||||
const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo,
|
||||
transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, transactions.payee_id, transactions.category_id,
|
||||
COALESCE(payees.name, '') as payee,
|
||||
COALESCE(category_groups.name, '') as category_group,
|
||||
COALESCE(categories.name, '') as category,
|
||||
COALESCE((
|
||||
SELECT CONCAT(otherAccounts.name)
|
||||
FROM transactions otherTransactions
|
||||
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
|
||||
WHERE otherTransactions.group_id = transactions.group_id
|
||||
AND otherTransactions.id != transactions.id
|
||||
), '')::text as transfer_account
|
||||
FROM transactions
|
||||
INNER JOIN accounts ON accounts.id = transactions.account_id
|
||||
LEFT JOIN payees ON payees.id = transactions.payee_id
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
WHERE transactions.account_id = $1
|
||||
SELECT t.id, t.date, t.memo, t.amount, t.group_id, t.status, t.account, t.payee_id, t.category_id, t.payee, t.category_group, t.category, t.transfer_account, t.budget_id, t.account_id
|
||||
FROM display_transactions AS t
|
||||
WHERE t.account_id = $1
|
||||
ORDER BY transactions.date DESC
|
||||
LIMIT 200
|
||||
`
|
||||
|
||||
type GetTransactionsForAccountRow struct {
|
||||
ID uuid.UUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount numeric.Numeric
|
||||
GroupID uuid.NullUUID
|
||||
Status TransactionStatus
|
||||
Account string
|
||||
PayeeID uuid.NullUUID
|
||||
CategoryID uuid.NullUUID
|
||||
Payee string
|
||||
CategoryGroup string
|
||||
Category string
|
||||
TransferAccount string
|
||||
}
|
||||
|
||||
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]GetTransactionsForAccountRow, error) {
|
||||
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]DisplayTransaction, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getTransactionsForAccount, accountID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTransactionsForAccountRow
|
||||
var items []DisplayTransaction
|
||||
for rows.Next() {
|
||||
var i GetTransactionsForAccountRow
|
||||
var i DisplayTransaction
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Date,
|
||||
@ -275,6 +213,8 @@ func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.
|
||||
&i.CategoryGroup,
|
||||
&i.Category,
|
||||
&i.TransferAccount,
|
||||
&i.BudgetID,
|
||||
&i.AccountID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user