Implement assignment import and clear of budget
This commit is contained in:
54
postgres/assignments.sql.go
Normal file
54
postgres/assignments.sql.go
Normal file
@ -0,0 +1,54 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// source: assignments.sql
|
||||
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createAssignment = `-- name: CreateAssignment :one
|
||||
INSERT INTO assignments (
|
||||
date, amount, category_id
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
)
|
||||
RETURNING id, category_id, date, memo, amount
|
||||
`
|
||||
|
||||
type CreateAssignmentParams struct {
|
||||
Date time.Time
|
||||
Amount Numeric
|
||||
CategoryID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAssignment(ctx context.Context, arg CreateAssignmentParams) (Assignment, error) {
|
||||
row := q.db.QueryRowContext(ctx, createAssignment, arg.Date, arg.Amount, arg.CategoryID)
|
||||
var i Assignment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CategoryID,
|
||||
&i.Date,
|
||||
&i.Memo,
|
||||
&i.Amount,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteAllAssignments = `-- name: DeleteAllAssignments :execrows
|
||||
DELETE FROM assignments
|
||||
USING categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE categories.id = assignments.category_id AND category_groups.budget_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteAllAssignments(ctx context.Context, budgetID uuid.UUID) (int64, error) {
|
||||
result, err := q.db.ExecContext(ctx, deleteAllAssignments, budgetID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
@ -91,6 +91,14 @@ func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetC
|
||||
|
||||
const getCategoriesWithBalance = `-- name: GetCategoriesWithBalance :many
|
||||
SELECT categories.id, categories.name, category_groups.name as group,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT SUM(a_hist.amount)
|
||||
FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id
|
||||
AND a_hist.date < $1
|
||||
)
|
||||
, 0)::decimal(12,2) as balance_assignments,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT SUM(t_hist.amount)
|
||||
@ -121,11 +129,12 @@ type GetCategoriesWithBalanceParams struct {
|
||||
}
|
||||
|
||||
type GetCategoriesWithBalanceRow struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
Group string
|
||||
Balance Numeric
|
||||
Activity Numeric
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
Group string
|
||||
BalanceAssignments Numeric
|
||||
Balance Numeric
|
||||
Activity Numeric
|
||||
}
|
||||
|
||||
func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) {
|
||||
@ -141,6 +150,7 @@ func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategorie
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Group,
|
||||
&i.BalanceAssignments,
|
||||
&i.Balance,
|
||||
&i.Activity,
|
||||
); err != nil {
|
||||
|
@ -15,6 +15,14 @@ type Account struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type Assignment struct {
|
||||
ID uuid.UUID
|
||||
CategoryID uuid.UUID
|
||||
Date time.Time
|
||||
Memo sql.NullString
|
||||
Amount Numeric
|
||||
}
|
||||
|
||||
type Budget struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
|
13
postgres/queries/assignments.sql
Normal file
13
postgres/queries/assignments.sql
Normal file
@ -0,0 +1,13 @@
|
||||
-- name: CreateAssignment :one
|
||||
INSERT INTO assignments (
|
||||
date, amount, category_id
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteAllAssignments :execrows
|
||||
DELETE FROM assignments
|
||||
USING categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE categories.id = assignments.category_id AND category_groups.budget_id = @budget_id;
|
@ -21,6 +21,14 @@ WHERE category_groups.budget_id = $1;
|
||||
|
||||
-- name: GetCategoriesWithBalance :many
|
||||
SELECT categories.id, categories.name, category_groups.name as group,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT SUM(a_hist.amount)
|
||||
FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id
|
||||
AND a_hist.date < @from_date
|
||||
)
|
||||
, 0)::decimal(12,2) as balance_assignments,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT SUM(t_hist.amount)
|
||||
|
@ -26,4 +26,10 @@ 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;
|
||||
LIMIT 200;
|
||||
|
||||
-- name: DeleteAllTransactions :execrows
|
||||
DELETE FROM transactions
|
||||
USING accounts
|
||||
WHERE accounts.budget_id = @budget_id
|
||||
AND accounts.id = transactions.account_id;
|
11
postgres/schema/202112071547_assignments.sql
Normal file
11
postgres/schema/202112071547_assignments.sql
Normal file
@ -0,0 +1,11 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE assignments (
|
||||
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||
category_id uuid NOT NULL REFERENCES categories (id),
|
||||
date date NOT NULL,
|
||||
memo text,
|
||||
amount decimal(12,2) NOT NULL
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE assignments;
|
@ -48,6 +48,21 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa
|
||||
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 getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
|
||||
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount,
|
||||
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category
|
||||
|
Reference in New Issue
Block a user