Ejemplo n.º 1
0
        public async Task <IActionResult> Index(string name)
        {
            var account = _accountRepo
                          .SearchFor(a => a.UserId.ToString().Equals(User.Identity.Name) &&
                                     a.Name.Equals(name))
                          .FirstOrDefault();

            account.Transactions = await _transactionRepo
                                   .SearchFor(t => t.UserId.ToString().Equals(User.Identity.Name) &&
                                              t.AccountId == account.Id)
                                   .ToListAsync();

            var nowStart = account.GoalStartDate.Year == DateTime.Now.Year && account.GoalStartDate.Month == DateTime.Now.Month
                ? account.GoalStartDate
                : DateTimeHelper.TruncateToDayStart(DateTime.Now);

            var nowEnd = account.GoalEndDate.Year == DateTime.Now.Year && account.GoalEndDate.Month == DateTime.Now.Month
                ? account.GoalEndDate
                : DateTimeHelper.TruncateToDayStart(DateTime.Now.AddMonths(1));

            return(View(new DashboardViewModel()
            {
                SavedCurrentMonth = _transactionsService
                                    .AmountSaved(account.Transactions, nowStart, nowEnd),

                ToSaveCurrentMonth = _suggestions.AmountToSaveAMonth(account),

                CurrentMonthBalanceHistory = _transactionsService
                                             .BalanceHistory(account.Transactions)
                                             .Where(x => DateTimeHelper.InRange(x.Key.Date, nowStart, nowEnd))
                                             .ToDictionary(x => x.Key, x => x.Value),

                CurrentMonthTotalExpenseByCategory = _transactionsService
                                                     .TotalExpenseByCategory(account.Transactions, _categoryRepo.GetAll(), nowStart, nowEnd),

                FreeMoneyToSpend = _suggestions.FreeMoneyToSpend(account),

                EstimatedTime = _suggestions.EstimatedTime(account)
            }));
        }