Implement new budget with transaction to be able to satisfy not null columns

This commit is contained in:
Jan Bader 2021-12-11 20:19:52 +00:00
parent 935499e3a8
commit 40a299141d
3 changed files with 40 additions and 0 deletions

View File

@ -102,3 +102,19 @@ func (q *Queries) GetFirstActivity(ctx context.Context, budgetID uuid.UUID) (tim
err := row.Scan(&min_date)
return min_date, err
}
const setInflowCategory = `-- name: SetInflowCategory :exec
UPDATE budgets
SET income_category_id = $1
WHERE budgets.id = $2
`
type SetInflowCategoryParams struct {
IncomeCategoryID uuid.UUID
ID uuid.UUID
}
func (q *Queries) SetInflowCategory(ctx context.Context, arg SetInflowCategoryParams) error {
_, err := q.db.ExecContext(ctx, setInflowCategory, arg.IncomeCategoryID, arg.ID)
return err
}

View File

@ -22,5 +22,24 @@ func (s *Database) NewBudget(context context.Context, name string, userID uuid.U
return nil, err
}
group, err := q.CreateCategoryGroup(context, CreateCategoryGroupParams{
Name: "Inflow",
BudgetID: budget.ID,
})
if err != nil {
return nil, err
}
cat, err := q.CreateCategory(context, CreateCategoryParams{
Name: "Ready to assign",
CategoryGroupID: group.ID,
})
q.SetInflowCategory(context, SetInflowCategoryParams{
IncomeCategoryID: cat.ID,
ID: budget.ID,
})
tx.Commit()
return &budget, nil
}

View File

@ -4,6 +4,11 @@ INSERT INTO budgets
VALUES ($1, NOW())
RETURNING *;
-- name: SetInflowCategory :exec
UPDATE budgets
SET income_category_id = $1
WHERE budgets.id = $2;
-- name: GetBudgetsForUser :many
SELECT budgets.* FROM budgets
LEFT JOIN user_budgets ON budgets.id = user_budgets.budget_id