Implement new budget with transaction to be able to satisfy not null columns
This commit is contained in:
parent
935499e3a8
commit
40a299141d
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user