Example #1
0
        public async Task <IActionResult> Index(string searchString, SortState sortOrder = SortState.DateTimeDesc)
        {
            string userId = _userManager.GetUserId(User);

            List <IncomeCategory> incomeCategory = await _balanceService.GetUserIncomeCategories(userId);

            List <ExpenseCategory> expenseCategory = await _balanceService.GetUserExpenseCategories(userId);

            IEnumerable <Currency> currency = await _balanceService.GetCurrency();

            SelectList currencySelectList = new SelectList(currency, "Id", "Code");

            CreateIncomeViewModel createIncomeViewModel = new CreateIncomeViewModel
            {
                Categories = new SelectList(incomeCategory, "Id", "Title"),
                Currencies = currencySelectList
            };

            CreateExpenseViewModel createExpenseViewModel = new CreateExpenseViewModel
            {
                Categories = new SelectList(expenseCategory, "Id", "Title"),
                Currencies = currencySelectList
            };

            IReadOnlyList <Income> incomes = await _balanceService.GetUserIncomes(userId, searchString);

            IReadOnlyList <Expense> expense = await _balanceService.GetUserExpenses(userId, searchString);

            var operations = _mapper.Map <IReadOnlyList <Income>, List <OperationViewModel> >(incomes);

            operations.AddRange(_mapper.Map <IReadOnlyList <Expense>, List <OperationViewModel> >(expense));

            operations = sortOrder switch
            {
                SortState.DateTimeDesc => operations.OrderByDescending(s => s.Date).ToList(),
                SortState.CategoryAsc => operations.OrderBy(s => s.Category).ToList(),
                SortState.CategoryDesc => operations.OrderByDescending(s => s.Category).ToList(),
                SortState.SumAsc => operations.OrderBy(s => s.SumByn).ToList(),
                SortState.SumDesc => operations.OrderByDescending(s => s.SumByn).ToList(),
                _ => operations.OrderBy(s => s.Date).ToList(),
            };

            BalanceViewModel balanceViewModel = new BalanceViewModel
            {
                CreateIncome   = createIncomeViewModel,
                CreateExpense  = createExpenseViewModel,
                Operations     = operations,
                SearchString   = searchString,
                SortOperations = new SortOperationsViewModel(sortOrder)
            };

            return(View(balanceViewModel));
        }