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)); }
public RedirectToRouteResult Create(CreateExpenseViewModel viewModel) { CreateExpenseModel model = new CreateExpenseModel(); model.balanceSheetId = viewModel.balanceSheetId; model.chargeableId = viewModel.chargeableId; _createExpenseCommand.Execute(model); return(RedirectToAction("Detail", "balanceSheet", model.balanceSheetId)); }
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)); }
public void WhenInitialzed_ShouldModelBeInstatiated() { //arrange var expenseServiceMock = new Mock <IExpenseService>(); var dialogServiceMock = new Mock <IDialogService>(); var vm = new CreateExpenseViewModel(dialogServiceMock.Object, expenseServiceMock.Object); //assert Assert.IsNotNull(vm.Model); Assert.IsNotNull(vm.Model.SpentDate); }
public ActionResult Expense(CreateExpenseViewModel model) { try { return(null); } catch (Exception e) { this.HandleError(e); return(this.JsonError(data: null, message: ValidationResources.Generic_Error)); } }
public void OnButtonCancel_ShouldCallDialogServiceClose() { //arrange var expenseServiceMock = new Mock <IExpenseService>(); var dialogServiceMock = new Mock <IDialogService>(); dialogServiceMock.Setup(x => x.Close(It.IsAny <bool>())); var vm = new CreateExpenseViewModel(dialogServiceMock.Object, expenseServiceMock.Object); //act vm.OnButtonCancelClicked(); //assert dialogServiceMock.Verify(x => x.Close(false)); }
public CreateExpenseViewModel Create(int balanceSheetId) { var chargeables = _chargeablesListQuery.Execute(); var viewModel = new CreateExpenseViewModel(); viewModel.Chargeables = chargeables .Select(x => new SelectListItem() { Value = x.Id.ToString(), Text = x.Name }) .ToList(); viewModel.balanceSheetId = balanceSheetId; return(viewModel); }
public void OnButtonSave_WhenInputIsCorrect_ShouldAddToExpenseService() { //arrange var expenseServiceMock = new Mock <IExpenseService>(); expenseServiceMock.Setup(x => x.Add(It.IsAny <Expense>())); var dialogServiceMock = new Mock <IDialogService>(); dialogServiceMock.Setup(x => x.Close(It.IsAny <bool>())); var vm = new CreateExpenseViewModel(dialogServiceMock.Object, expenseServiceMock.Object); //act vm.OnButtonSaveClicked(); //assert expenseServiceMock.Verify(x => x.Add(vm.Model)); dialogServiceMock.Verify(x => x.Close(true)); }
public ActionResult Expense() { try { var defaultAccount = this.Current.Services.BankAccountService.GetDefaultAccountForuser(this.Current.User.Id); var defaultCategory = this.Current.Services.CategoryService.GetDefaultCategoryForUser(this.Current.User.Id); var model = new CreateExpenseViewModel { SourceBankAccountId = defaultAccount.Id, Currency = defaultAccount.Currency, CategoryId = defaultCategory.Id, ExchangeRate = 1.0f, ValueDate = DateTime.UtcNow }; return(View(model)); } catch (Exception e) { this.HandleError(e); throw e; } }