// GET: Portfolios/Create public IActionResult Create() { // Create the viewModel var viewModel = new PortfolioCreateViewModel() { Portfolio = new Portfolio(), AssetClasses = _context.AssetClasses.ToList(), PortfolioSecurities = new List <PortfolioSecurityInput>() }; // Add 10 PortfolioSecurity objects to the viewModel's list for (int i = 0; i < 10; i++) { viewModel.PortfolioSecurities.Add(new PortfolioSecurityInput()); } return(View(viewModel)); }
public async Task <IActionResult> Create([Bind("Portfolio", "PortfolioSecurities")] PortfolioCreateViewModel viewModel) { // Make sure weights sum to 100 if (viewModel.PortfolioSecurities.Select(ps => ps.Weight).Sum() != 100) { viewModel.AssetClasses = _context.AssetClasses.ToList(); ViewData["WeightError"] = "The sum of all weights must equal 100"; return(View(viewModel)); } var user = await GetCurrentUserAsync(); string token = GetToken(); var client = _clientFactory.CreateClient(); // Get all the securities from the database var securities = _context.Securities; // Iterate over the list of PortfolioSecurities entered by the user foreach (PortfolioSecurityInput ps in viewModel.PortfolioSecurities) { if (ps.Security.Ticker != null) // Only check for security if the row was not blank { string ticker = ps.Security.Ticker; if (!securities.Any(s => s.Ticker == ticker)) { // Security is not in the DB and needs to be retrieved from IEX Cloud and saved to the DB var request = new HttpRequestMessage(HttpMethod.Get, $"https://cloud.iexapis.com/stable/stock/{ticker}/company?token={token}"); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { // Convert the response to an object and save the new security to the database var json = await response.Content.ReadAsStreamAsync(); var stockResponse = await System.Text.Json.JsonSerializer.DeserializeAsync <IEXSecurity>(json); //SaveSecurity(stockResponse); Security newSecurity = new Security { Name = stockResponse.CompanyName, Ticker = stockResponse.Ticker, Description = stockResponse.Description }; _context.Securities.Add(newSecurity); await _context.SaveChangesAsync(); } } } } // Now all the securities should be in the DB. Get a new reference to them and iterate over the list of portfolio securities again var updatedSecurities = _context.Securities; // Save the new portfolio to the database and get a reference to its Id viewModel.Portfolio.UserId = user.Id; _context.Add(viewModel.Portfolio); await _context.SaveChangesAsync(); int portfolioId = viewModel.Portfolio.Id; // iterate over PortfolioSecurities again and enter them into the database with their properties foreach (PortfolioSecurityInput ps in viewModel.PortfolioSecurities) { if (ps.Security.Ticker != null) // only create new PS if the row was not blank { Security matchingSecurity = updatedSecurities.First(s => s.Ticker == ps.Security.Ticker); PortfolioSecurity newPS = new PortfolioSecurity { PortfolioId = portfolioId, SecurityId = matchingSecurity.Id, Weight = (int)ps.Weight, AssetClassId = (int)ps.AssetClassId }; _context.Add(newPS); } } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }