diff --git a/postgres/budgets.sql.go b/postgres/budgets.sql.go index 0e85a95..c5cb3d4 100644 --- a/postgres/budgets.sql.go +++ b/postgres/budgets.sql.go @@ -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 +} diff --git a/postgres/budgetservice.go b/postgres/budgetservice.go index 2ef4898..c53cca3 100644 --- a/postgres/budgetservice.go +++ b/postgres/budgetservice.go @@ -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 } diff --git a/postgres/queries/budgets.sql b/postgres/queries/budgets.sql index 6df5dac..e340d31 100644 --- a/postgres/queries/budgets.sql +++ b/postgres/queries/budgets.sql @@ -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