public async Task <Result> Handle(Query request, CancellationToken cancellationToken) { var hasBudgetCategoriesAccess = await _accessControlService.HasBudgetCategoriesAccessAsync(request.BudgetCategoryIds); if (!hasBudgetCategoriesAccess) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var responseData = new List <BudgetCategoryBalanceDto>(); foreach (var budgetCategoryId in request.BudgetCategoryIds) { var balances = _balanceService.GetCategoryBalances(budgetCategoryId, null, null).ToList(); var thisMonthBalance = balances.First(x => x.Year == DateTime.Today.Year && x.Month == DateTime.Today.Month); var balance = new BudgetCategoryBalanceDto() { BudgetCategoryId = budgetCategoryId, ThisMonthTransactionsTotal = thisMonthBalance.TransactionsTotal, ThisMonthBudgetedAmount = thisMonthBalance.BudgetedAmount, TotalTransactionsBalance = new MoneyAmount(thisMonthBalance.BudgetedAmount.CurrencyCode, balances.Sum(x => x.TransactionsTotal.Amount + x.AllocationsTotal.Amount)) }; balance.ThisMonthBudgetedAmountLeft = balance.ThisMonthBudgetedAmount - balance.ThisMonthTransactionsTotal; responseData.Add(balance); } return(new Result() { Data = responseData, Total = responseData.Count }); }