public async Task AddTradeShouldCreateNewBuyTrade() { // Arrange this.FillDatabase(); this.inputModel = new AddTradeInputModel { Price = 20, InvestmentWalletId = 2, Quantity = 5, Type = ViewModels.Trades.Enums.TradeType.Buy, SelectedCompany = new CompanyViewModel { Id = "company3", Name = "Amazon", Ticker = "amzn", }, }; // Act var result = await this.controller.Add(this.inputModel); // Assert var redirectResult = Assert.IsType <RedirectResult>(result); var newTrade = this.db.Trades.FirstOrDefault(x => !x.Id.StartsWith("trade")); Assert.Equal(2, newTrade.InvestmentWalletId); Assert.Equal(-20, newTrade.Price); Assert.Equal(5, newTrade.StockQuantity); Assert.Equal("company3", newTrade.CompanyId); Assert.Equal(4, this.db.Trades.Count()); Assert.Equal(TradeType.Buy, newTrade.Type); }
public async Task <IActionResult> Add(int investmentWalletId) { try { var companies = await this.companiesService.GetAllCompaniesAsync(); string investmentWalletName = await this.tradesService.GetInvestmentWalletNameAsync(investmentWalletId); var model = new AddTradeInputModel { InvestmentWalletId = investmentWalletId, InvestmentWalletName = investmentWalletName, Companies = companies.Select(c => new CompanyViewModel { Name = c.Name, Ticker = c.Ticker, Id = c.Id, }) .ToList(), }; var selectedCompany = new CompanyViewModel(); model.SelectedCompany = selectedCompany; return(this.View(model)); } catch (Exception ex) { return(this.Redirect($"/Home/Error?message={ex.Message}")); } }
public async Task <IActionResult> Add(AddTradeInputModel input) { try { if (!this.ModelState.IsValid) { var companies = await this.companiesService.GetAllCompaniesAsync(); input.Companies = companies.Select(c => new CompanyViewModel { Name = c.Name, Ticker = c.Ticker, Id = c.Id, }) .ToList(); return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); var selectedCompany = await this.companiesService .GetCompanyByIdAsync(input.SelectedCompany.Id); input.SelectedCompany.Name = selectedCompany.Name; if (input.Type == TradeType.Buy) { await this.tradesService.CreateBuyTradeAsync(user.Id, input.InvestmentWalletId, input.SelectedCompany.Id, input.Quantity, input.Price); } else { await this.tradesService.CreateSellTradeAsync(user.Id, input.InvestmentWalletId, input.SelectedCompany.Id, input.Quantity, input.Price); } return(this.Redirect($"/Investments/Trades/{input.InvestmentWalletId}")); } catch (Exception ex) { return(this.Redirect($"/Home/Error?message={ex.Message}")); } }