303 lines
7.6 KiB
Go
303 lines
7.6 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// source: transactions.sql
|
|
|
|
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const createTransaction = `-- name: CreateTransaction :one
|
|
INSERT INTO transactions
|
|
(date, memo, amount, account_id, payee_id, category_id, group_id, status)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
RETURNING id, date, memo, amount, account_id, category_id, payee_id, group_id, status
|
|
`
|
|
|
|
type CreateTransactionParams struct {
|
|
Date time.Time
|
|
Memo string
|
|
Amount Numeric
|
|
AccountID uuid.UUID
|
|
PayeeID uuid.NullUUID
|
|
CategoryID uuid.NullUUID
|
|
GroupID uuid.NullUUID
|
|
Status TransactionStatus
|
|
}
|
|
|
|
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (Transaction, error) {
|
|
row := q.db.QueryRowContext(ctx, createTransaction,
|
|
arg.Date,
|
|
arg.Memo,
|
|
arg.Amount,
|
|
arg.AccountID,
|
|
arg.PayeeID,
|
|
arg.CategoryID,
|
|
arg.GroupID,
|
|
arg.Status,
|
|
)
|
|
var i Transaction
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Date,
|
|
&i.Memo,
|
|
&i.Amount,
|
|
&i.AccountID,
|
|
&i.CategoryID,
|
|
&i.PayeeID,
|
|
&i.GroupID,
|
|
&i.Status,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteAllTransactions = `-- name: DeleteAllTransactions :execrows
|
|
DELETE FROM transactions
|
|
USING accounts
|
|
WHERE accounts.budget_id = $1
|
|
AND accounts.id = transactions.account_id
|
|
`
|
|
|
|
func (q *Queries) DeleteAllTransactions(ctx context.Context, budgetID uuid.UUID) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, deleteAllTransactions, budgetID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const deleteTransaction = `-- name: DeleteTransaction :exec
|
|
DELETE FROM transactions
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteTransaction(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.ExecContext(ctx, deleteTransaction, id)
|
|
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
|
|
`
|
|
|
|
func (q *Queries) GetTransaction(ctx context.Context, id uuid.UUID) (Transaction, error) {
|
|
row := q.db.QueryRowContext(ctx, getTransaction, id)
|
|
var i Transaction
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Date,
|
|
&i.Memo,
|
|
&i.Amount,
|
|
&i.AccountID,
|
|
&i.CategoryID,
|
|
&i.PayeeID,
|
|
&i.GroupID,
|
|
&i.Status,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getTransactionsByMonthAndCategory = `-- name: GetTransactionsByMonthAndCategory :many
|
|
SELECT date, category_id, budget_id, amount
|
|
FROM transactions_by_month
|
|
WHERE transactions_by_month.budget_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetTransactionsByMonthAndCategory(ctx context.Context, budgetID uuid.UUID) ([]TransactionsByMonth, error) {
|
|
rows, err := q.db.QueryContext(ctx, getTransactionsByMonthAndCategory, budgetID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []TransactionsByMonth
|
|
for rows.Next() {
|
|
var i TransactionsByMonth
|
|
if err := rows.Scan(
|
|
&i.Date,
|
|
&i.CategoryID,
|
|
&i.BudgetID,
|
|
&i.Amount,
|
|
); 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 getTransactionsForAccount = `-- name: GetTransactionsForAccount :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,
|
|
(
|
|
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
|
|
) 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
|
|
ORDER BY transactions.date DESC
|
|
LIMIT 200
|
|
`
|
|
|
|
type GetTransactionsForAccountRow struct {
|
|
ID uuid.UUID
|
|
Date time.Time
|
|
Memo string
|
|
Amount Numeric
|
|
GroupID uuid.NullUUID
|
|
Status TransactionStatus
|
|
Account string
|
|
Payee string
|
|
CategoryGroup string
|
|
Category string
|
|
TransferAccount interface{}
|
|
}
|
|
|
|
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]GetTransactionsForAccountRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getTransactionsForAccount, accountID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetTransactionsForAccountRow
|
|
for rows.Next() {
|
|
var i GetTransactionsForAccountRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Date,
|
|
&i.Memo,
|
|
&i.Amount,
|
|
&i.GroupID,
|
|
&i.Status,
|
|
&i.Account,
|
|
&i.Payee,
|
|
&i.CategoryGroup,
|
|
&i.Category,
|
|
&i.TransferAccount,
|
|
); 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,
|
|
memo = $2,
|
|
amount = $3,
|
|
account_id = $4,
|
|
payee_id = $5,
|
|
category_id = $6
|
|
WHERE id = $7
|
|
`
|
|
|
|
type UpdateTransactionParams struct {
|
|
Date time.Time
|
|
Memo string
|
|
Amount Numeric
|
|
AccountID uuid.UUID
|
|
PayeeID uuid.NullUUID
|
|
CategoryID uuid.NullUUID
|
|
ID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdateTransaction(ctx context.Context, arg UpdateTransactionParams) error {
|
|
_, err := q.db.ExecContext(ctx, updateTransaction,
|
|
arg.Date,
|
|
arg.Memo,
|
|
arg.Amount,
|
|
arg.AccountID,
|
|
arg.PayeeID,
|
|
arg.CategoryID,
|
|
arg.ID,
|
|
)
|
|
return err
|
|
}
|