This commit is contained in:
@@ -50,13 +50,9 @@ func (s *BudgetService) AddBudget(ctx context.Context, budget *BudgetEntity) (*B
|
||||
return nil, fmt.Errorf("unable to insert row: %w", err)
|
||||
}
|
||||
|
||||
query = `INSERT INTO users_budgets (user_id, budget_id) VALUES (@user_id, @budget_id) RETURNING id`
|
||||
args = pgx.NamedArgs{
|
||||
"user_id": userId,
|
||||
"budget_id": budget.Id,
|
||||
}
|
||||
if err := s.db.QueryRow(ctx, query, args).Scan(&budget.Id); err != nil {
|
||||
return nil, fmt.Errorf("unable to insert row: %w", err)
|
||||
_, err = s.AddUserToBudget(ctx, budget.Id, userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
@@ -92,3 +88,20 @@ func (s *BudgetService) GetBudgets(ctx context.Context) ([]*BudgetEntity, error)
|
||||
|
||||
return budgets, nil
|
||||
}
|
||||
|
||||
func (s *BudgetService) AddUserToBudget(
|
||||
ctx context.Context,
|
||||
budgetId int,
|
||||
userId int,
|
||||
) (int, error) {
|
||||
query := `INSERT INTO users_budgets (user_id, budget_id) VALUES (@user_id, @budget_id) RETURNING id`
|
||||
args := pgx.NamedArgs{
|
||||
"user_id": userId,
|
||||
"budget_id": budgetId,
|
||||
}
|
||||
id := 0
|
||||
if err := s.db.QueryRow(ctx, query, args).Scan(&id); err != nil {
|
||||
return 0, fmt.Errorf("unable to insert row: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user