Implement Export in YNAB-Format #15
@ -22,7 +22,7 @@ WHERE id = $7;
|
||||
DELETE FROM transactions
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: GetTransactionsForBudget :many
|
||||
-- name: GetAllTransactionsForBudget :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category
|
||||
FROM transactions
|
||||
@ -31,8 +31,7 @@ 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
|
||||
ORDER BY transactions.date DESC
|
||||
LIMIT 200;
|
||||
ORDER BY transactions.date DESC;
|
||||
|
||||
-- name: GetTransactionsForAccount :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo,
|
||||
|
@ -79,6 +79,65 @@ func (q *Queries) DeleteTransaction(ctx context.Context, id uuid.UUID) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const getAllTransactionsForBudget = `-- name: GetAllTransactionsForBudget :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category
|
||||
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
|
||||
ORDER BY transactions.date DESC
|
||||
`
|
||||
|
||||
type GetAllTransactionsForBudgetRow struct {
|
||||
ID uuid.UUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount Numeric
|
||||
GroupID uuid.NullUUID
|
||||
Status TransactionStatus
|
||||
Account string
|
||||
Payee string
|
||||
CategoryGroup string
|
||||
Category string
|
||||
}
|
||||
|
||||
func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]GetAllTransactionsForBudgetRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getAllTransactionsForBudget, budgetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetAllTransactionsForBudgetRow
|
||||
for rows.Next() {
|
||||
var i GetAllTransactionsForBudgetRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Date,
|
||||
&i.Memo,
|
||||
&i.Amount,
|
||||
&i.GroupID,
|
||||
&i.Status,
|
||||
&i.Account,
|
||||
&i.Payee,
|
||||
&i.CategoryGroup,
|
||||
&i.Category,
|
||||
); 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 getTransaction = `-- name: GetTransaction :one
|
||||
SELECT id, date, memo, amount, account_id, category_id, payee_id, group_id, status FROM transactions
|
||||
WHERE id = $1
|
||||
@ -208,66 +267,6 @@ func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.group_id, transactions.status,
|
||||
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category
|
||||
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
|
||||
ORDER BY transactions.date DESC
|
||||
LIMIT 200
|
||||
`
|
||||
|
||||
type GetTransactionsForBudgetRow struct {
|
||||
ID uuid.UUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount Numeric
|
||||
GroupID uuid.NullUUID
|
||||
Status TransactionStatus
|
||||
Account string
|
||||
Payee string
|
||||
CategoryGroup string
|
||||
Category string
|
||||
}
|
||||
|
||||
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]GetTransactionsForBudgetRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getTransactionsForBudget, budgetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTransactionsForBudgetRow
|
||||
for rows.Next() {
|
||||
var i GetTransactionsForBudgetRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Date,
|
||||
&i.Memo,
|
||||
&i.Amount,
|
||||
&i.GroupID,
|
||||
&i.Status,
|
||||
&i.Account,
|
||||
&i.Payee,
|
||||
&i.CategoryGroup,
|
||||
&i.Category,
|
||||
); 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 updateTransaction = `-- name: UpdateTransaction :exec
|
||||
UPDATE transactions
|
||||
SET date = $1,
|
||||
|
Loading…
x
Reference in New Issue
Block a user