public async Task <IActionResult> OnPostDeleteCryptoAsync(int?id) { _logger.LogWarning($"OnPost: InvestorId {id}, DROP crypto {CryptoIdToDelete}"); if (id == null) { return(NotFound()); } Investor = await _context.Investor.Include(i => i.InvestorCryptos).ThenInclude(ic => ic.Crypto).FirstOrDefaultAsync(m => m.InvestorId == id); if (Investor == null) { return(NotFound()); } InvestorCrypto cryptoToDrop = _context.InvestorCrypto.Find(CryptoIdToDelete, id.Value); if (cryptoToDrop != null) { _context.Remove(cryptoToDrop); _context.SaveChanges(); } else { _logger.LogWarning("Investor Does Not have this crypto"); } return(RedirectToPage(new { id = id })); //reloads details page of that specific investor }
public async Task <IActionResult> OnPostAsync(int?id) { _logger.LogWarning($"OnPost: InvestorId {id}, ADD crypto {CryptoIdToAdd}"); if (CryptoIdToAdd == 0) { ModelState.AddModelError("CryptoIdToAdd", "This field is a required field"); return(Page()); } if (id == null) { return(NotFound()); } Investor = await _context.Investor.Include(i => i.InvestorCryptos).ThenInclude(ic => ic.Crypto).FirstOrDefaultAsync(m => m.InvestorId == id); AllCryptos = await _context.Crypto.ToListAsync(); CryptoDropDown = new SelectList(AllCryptos, "CryptoId", "Name", "Price"); if (Investor == null) { return(NotFound()); } if (!_context.InvestorCrypto.Any(ic => ic.CryptoId == CryptoIdToAdd && ic.InvestorId == id.Value)) { InvestorCrypto cryptoToAdd = new InvestorCrypto { InvestorId = id.Value, CryptoId = CryptoIdToAdd, PurchaseDate = DateTime.Now }; _context.Add(cryptoToAdd); _context.SaveChanges(); } else { _logger.LogWarning("Investor Already bought this crypto"); } return(RedirectToPage(new{ id = id })); }