public async Task <IActionResult> Index()
        {
            var viewModel = new BudgetIndexViewModel()
            {
                Budget           = await _repo.GetBudget(),
                TotalForTheMonth = await _repo.GetTotalsForBudget(),
                Categories       = await _repo.GetList <Category>()
            };

            return(View(viewModel));
        }
Exemplo n.º 2
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var budget = await _repo.GetBudget();

            var totalsForBudget = await _repo.GetTotalsForBudget();

            double percentage = Math.Round(totalsForBudget.Select(x => x.Value).Sum() / budget.Select(x => x.Amount).Sum() * 100);

            if (percentage > 100)
            {
                percentage = 100;
            }

            int categroriesOverBudget = 0;

            foreach (var item in budget)
            {
                float totalForBudget = totalsForBudget.Where(x => x.Key == item.CategoryId)
                                       .Select(x => x.Value)
                                       .FirstOrDefault();

                if (item.Amount > 0 && totalForBudget > item.Amount)
                {
                    categroriesOverBudget++;
                }
            }

            var model = new BudgetUsedViewModel()
            {
                PercentageUsed       = percentage,
                Chart                = GenerateChart(percentage),
                CategoriesInBudget   = budget.Where(x => x.Amount > 0).Count(),
                CategoriesOverBudget = categroriesOverBudget,
                BudgetId             = budget.Select(x => x.BudgetId).FirstOrDefault()
            };

            return(View(model));
        }