private IndexViewModel GetTransactionIndexViewModel(int userId, int? accountId, int? categoryId, DateTime? from, DateTime? to) { var accountList = _accountServices.All(); var accounts = accountList.Select(x => new SelectListItem { Value = x.AccountID.ToString(), Text = x.Name }); var account = (accountId.HasValue) ? accountList.FirstOrDefault(x => x.AccountID == accountId.Value) : accountList.FirstOrDefault(x => x.Default); if (account == null) account = accountList.First(); var allCategories = _categoryServices.All(); var categoryList = (account != null) ? allCategories.Where(c => c.Account_AccountID == account.AccountID) : allCategories.Where(c => c.Account_AccountID == accountList.First().AccountID); var categories = categoryList.Select(x => new SelectListItem { Value = x.CategoryID.ToString(), Text = x.Name }); // Default to last 30 days var now = DateTime.Now; var defaultTo = new DateTime(now.Year, now.Month, now.Day); var defaultFrom = defaultTo.AddDays(-30); var model = new IndexViewModel { Accounts = accounts, AccountID = account.AccountID, Categories = categories, CategoryID = categoryId, From = (from.HasValue) ? from.Value : defaultFrom, To = (to.HasValue) ? to.Value : defaultTo }; if (accountId.HasValue) { if (categoryId.HasValue) { model.Transactions = _transactionServices.GetForAccount(model.AccountID.Value, model.CategoryID.Value, model.From, model.To) .OrderByDescending(x => x.Date) .ToList(); } else { model.Transactions = _transactionServices.GetForAccount(model.AccountID.Value, model.From, model.To) .OrderByDescending(x => x.Date) .ToList(); } } return model; }
public ActionResult Index(ProfileViewModel profile, IndexViewModel model) { return View(GetTransactionIndexViewModel(profile.UserID, model.AccountID, model.CategoryID, model.From, model.To)); }