add get budgets method
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-11-24 16:53:52 +07:00
parent bf85f31056
commit 95fe8b530e
5 changed files with 72 additions and 13 deletions
@@ -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