Try to display simple budget screen
This commit is contained in:
@ -5,6 +5,7 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@ -88,6 +89,59 @@ func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetC
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getCategoriesWithBalance = `-- name: GetCategoriesWithBalance :many
|
||||
SELECT categories.id, categories.name, category_groups.name as group, SUM(t_hist.amount)::decimal(12,2) as balance, SUM(t_this.amount)::decimal(12,2) as activity
|
||||
FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
INNER JOIN transactions t_hist ON categories.id = t_hist.category_id AND t_hist.date < $1
|
||||
INNER JOIN transactions t_this ON categories.id = t_this.category_id AND t_this.date >= $1 AND t_this.date < $2
|
||||
WHERE category_groups.budget_id = $3
|
||||
GROUP BY categories.id, categories.name, category_groups.name
|
||||
`
|
||||
|
||||
type GetCategoriesWithBalanceParams struct {
|
||||
FromDate time.Time
|
||||
ToDate time.Time
|
||||
BudgetID uuid.UUID
|
||||
}
|
||||
|
||||
type GetCategoriesWithBalanceRow struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
Group string
|
||||
Balance Numeric
|
||||
Activity Numeric
|
||||
}
|
||||
|
||||
func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getCategoriesWithBalance, arg.FromDate, arg.ToDate, arg.BudgetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetCategoriesWithBalanceRow
|
||||
for rows.Next() {
|
||||
var i GetCategoriesWithBalanceRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Group,
|
||||
&i.Balance,
|
||||
&i.Activity,
|
||||
); 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 getCategoryGroups = `-- name: GetCategoryGroups :many
|
||||
SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups
|
||||
WHERE category_groups.budget_id = $1
|
||||
|
Reference in New Issue
Block a user