コード例 #1
0
        public async Task <IActionResult> CreateExpense(CreateExpenseViewModel createExpense)
        {
            string userId = _userManager.GetUserId(User);

            if (ModelState.IsValid)
            {
                if (createExpense.SumByn < await _balanceService.GetBalance(userId, createExpense.Date))
                {
                    Expense expense = _mapper.Map <Expense>(createExpense);
                    expense.UserId = _userManager.GetUserId(User);
                    await _balanceService.AddExpense(expense);

                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("CreateExpense.Sum", "Не достаточно средств");
                }
            }

            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
            };

            createExpense.Categories = new SelectList(expenseCategory, "Id", "Title");
            createExpense.Currencies = currencySelectList;

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

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

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

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

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


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