add add user to budget route
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-11-24 22:24:44 +07:00
parent 95fe8b530e
commit 130a3236e0
9 changed files with 483 additions and 413 deletions
+20 -7
View File
@@ -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