This commit is contained in:
@@ -64,3 +64,31 @@ func (s *BudgetService) AddBudget(ctx context.Context, budget *BudgetEntity) (*B
|
||||
}
|
||||
return budget, nil
|
||||
}
|
||||
|
||||
func (s *BudgetService) GetBudgets(ctx context.Context) ([]*BudgetEntity, error) {
|
||||
userId, err := context_utils.GetUserId(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := `SELECT b.id, b.name, b.start_day, b.monthly_limit FROM budgets b JOIN users_budgets ub ON ub.budget_id = b.id WHERE ub.user_id = @user_id`
|
||||
args := pgx.NamedArgs{
|
||||
"user_id": userId,
|
||||
}
|
||||
rows, err := s.db.Query(ctx, query, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
budgets := []*BudgetEntity{}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
budget := &BudgetEntity{}
|
||||
err = rows.Scan(&budget.Id, &budget.Name, &budget.StartDay, &budget.MonthlyLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
budgets = append(budgets, budget)
|
||||
}
|
||||
|
||||
return budgets, nil
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user