public async Task <IActionResult> Add()
        {
            try
            {
                AddWalletInputModel model = new AddWalletInputModel()
                {
                    Amount = 0,
                    Name   = string.Empty,
                };

                var currencies = await this.currenciesService.GetAllAsync();

                model.Currencies = currencies
                                   .Select(c => new CurrencyViewModel
                {
                    Code       = c.Code,
                    CurrencyId = c.CurrencyId,
                    Name       = c.Name,
                })
                                   .ToList();

                return(this.View(model));
            }
            catch (Exception ex)
            {
                return(this.Redirect($"/Home/Error?message={ex.Message}"));
            }
        }
        public async Task <IActionResult> Add(AddWalletInputModel input)
        {
            try
            {
                if (!this.ModelState.IsValid)
                {
                    var currencies = await this.currenciesService.GetAllAsync();

                    input.Currencies = currencies
                                       .Select(c => new CurrencyViewModel
                    {
                        Code       = c.Code,
                        CurrencyId = c.CurrencyId,
                        Name       = c.Name,
                    })
                                       .ToList();

                    return(this.View(input));
                }

                var user = await this.userManager.GetUserAsync(this.User);

                if (!await this.currenciesService.IsCurrencyExistAsync(input.CurrencyId))
                {
                    var currencies = await this.currenciesService.GetAllAsync();

                    input.Currencies = currencies
                                       .Select(c => new CurrencyViewModel
                    {
                        Code       = c.Code,
                        CurrencyId = c.CurrencyId,
                        Name       = c.Name,
                    })
                                       .ToList();

                    this.TempData["CurrencyExist"] = "Please select a valid currency!";
                    this.ViewBag.CurrencyExist     = this.TempData["CurrencyExist"];
                    return(this.View(input));
                }

                int walletId = await this.walletsService
                               .AddAsync(user.Id, input.Name, input.Amount, input.CurrencyId);

                return(this.Redirect($"/Wallets/Details/{walletId}"));
            }
            catch (Exception ex)
            {
                return(this.Redirect($"/Home/Error?message={ex.Message}"));
            }
        }
        public async Task ControllerShouldReturnViewWhenCurrencyIdIsInvalid()
        {
            this.FillDatabase();

            this.inputModel = new AddWalletInputModel
            {
                Amount     = 250,
                CurrencyId = 100,
                Name       = "Test Wallet",
            };

            var result = await this.controller.Add(this.inputModel);

            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <AddWalletInputModel>(viewResult.ViewData.Model);
        }
        public async Task ControllerShouldReturnRedirectWhenWalletIsAdded()
        {
            this.FillDatabase();

            this.inputModel = new AddWalletInputModel
            {
                Amount     = 250,
                CurrencyId = 1,
                Name       = "Test Wallet",
            };

            var result = await this.controller.Add(this.inputModel);

            var redirectToActionResult = Assert.IsType <RedirectResult>(result);

            Assert.Equal("W", redirectToActionResult.Url[1].ToString());
        }
        public async Task ControllerShouldCreateNewWallet()
        {
            this.FillDatabase();

            this.inputModel = new AddWalletInputModel
            {
                Amount     = 250,
                CurrencyId = 1,
                Name       = "Test Wallet",
            };

            await this.controller.Add(this.inputModel);

            var createdWallet = this.db.Wallets.FirstOrDefault(w => w.Name == "Test Wallet");

            Assert.Equal("Test Wallet", createdWallet.Name);
            Assert.Equal(1, createdWallet.CurrencyId);
            Assert.Equal(250, createdWallet.MoneyAmount);
        }