Example #1
0
        public async Task <IActionResult> CreateIncome(CreateIncomeViewModel createIncome)
        {
            string userId = _userManager.GetUserId(User);

            if (ModelState.IsValid)
            {
                Income income = _mapper.Map <Income>(createIncome);
                income.UserId = userId;
                await _balanceService.AddIncome(income);

                return(RedirectToAction("Index"));
            }

            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");

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

            createIncome.Categories = new SelectList(incomeCategory, "Id", "Title");
            createIncome.Currencies = currencySelectList;

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

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

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

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

            BalanceViewModel balanceViewModel = new BalanceViewModel
            {
                CreateIncome   = createIncome,
                CreateExpense  = createExpenseViewModel,
                Operations     = operations.OrderBy(x => x.Date).ToList(),
                SortOperations = new SortOperationsViewModel(SortState.DateTimeAsc)
            };

            return(View("Index", balanceViewModel));
        }