diff --git a/postgres/models.go b/postgres/models.go index ad4bf6b..bacb571 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -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 diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index 4f46bd8..fed2e4e 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -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; diff --git a/postgres/schema/0015_transactions-view.sql b/postgres/schema/0015_transactions-view.sql new file mode 100644 index 0000000..dd961eb --- /dev/null +++ b/postgres/schema/0015_transactions-view.sql @@ -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; \ No newline at end of file diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index fafe8ae..6e7f63b 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -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 }