public async Task <IActionResult> DeleteConfirmed(long id) { var appNamedreaction = await _context.AppNamedreaction .Include(a => a.Catalyst) .Include(a => a.FunctionalGroup) .Include(a => a.Solvent) .Include(i => i.AppNamedreactionReactants) .ThenInclude(i => i.Reactant) .Include(i => i.AppNamedreactionByProducts) .ThenInclude(i => i.Reactant) .Include(a => a.AppNamedreactionByProducts) .FirstOrDefaultAsync(m => m.Id == id); var fileName = _hostingEnvironment.WebRootPath + "/" + appNamedreaction.Image; if (System.IO.File.Exists(fileName)) { System.IO.File.Delete(fileName); } var reactionByProducts = new HashSet <long> (appNamedreaction.AppNamedreactionByProducts.Select(c => c.Reactant.Id)); foreach (var reactant in _context.AppReactant) { if (reactionByProducts.Contains(reactant.Id)) { NamedReactionByProducts reactantToRemove = appNamedreaction.AppNamedreactionByProducts.SingleOrDefault(i => i.ReactantId == reactant.Id); _context.Remove(reactantToRemove); } } var reactionReactants = new HashSet <long> (appNamedreaction.AppNamedreactionReactants.Select(c => c.Reactant.Id)); foreach (var reactant in _context.AppReactant) { if (reactionReactants.Contains(reactant.Id)) { NamedReactionReactants reactantToRemove = appNamedreaction.AppNamedreactionReactants.SingleOrDefault(i => i.ReactantId == reactant.Id); _context.Remove(reactantToRemove); } } appNamedreaction.FunctionalGroup = null; appNamedreaction.Catalyst = null; appNamedreaction.Solvent = null; var references = _context.AppReference.Where(i => i.ReactionId == appNamedreaction.Id).ToList(); foreach (Reference r in references) { _context.AppReference.Remove(r); } _context.AppNamedreaction.Remove(appNamedreaction); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create([Bind("Id,Name,ReactantA,ReactantB,ReactantC,Product,Heat,AcidBase,ImageFile,CatalystId,FunctionalGroupId,SolventId,Url")] SustainableChemistryWeb.ViewModels.NamedReactionViewModel namedReactionView, string[] reactants, string[] byProducts) { NamedReaction appNamedreaction = new NamedReaction() { Name = namedReactionView.Name, ReactantA = string.Empty, ReactantB = string.Empty, ReactantC = string.Empty, Product = string.IsNullOrEmpty(namedReactionView.Product) ? string.Empty : namedReactionView.Product, Heat = namedReactionView.Heat, AcidBase = namedReactionView.AcidBase, //Image = "Images/Reactions/" + Guid.NewGuid().ToString() + namedReactionView.Image == null ? ".jpg" : , CatalystId = namedReactionView.CatalystId, FunctionalGroupId = namedReactionView.FunctionalGroupId, SolventId = namedReactionView.SolventId, Url = string.IsNullOrEmpty(namedReactionView.Url) ? string.Empty : namedReactionView.Url, }; if (namedReactionView.Image != null) { appNamedreaction.Image = "Images/Reactions/" + Guid.NewGuid().ToString() + System.IO.Path.GetFileName(namedReactionView.ImageFile.FileName); using (var stream = new System.IO.FileStream(_hostingEnvironment.WebRootPath + "/" + appNamedreaction.Image, System.IO.FileMode.Create)) { await namedReactionView.ImageFile.CopyToAsync(stream); stream.Close(); } } else { appNamedreaction.Image = "Images/Reactions/" + Guid.NewGuid().ToString() + ".jpg"; System.IO.StreamReader image = new System.IO.StreamReader(_hostingEnvironment.WebRootPath + "/Images/Reactions/th.jpg"); using (var stream = new System.IO.FileStream(_hostingEnvironment.WebRootPath + "/" + appNamedreaction.Image, System.IO.FileMode.Create)) { await image.BaseStream.CopyToAsync(stream); stream.Close(); } } if (reactants != null) { foreach (var reactant in reactants) { var reactantToAdd = new NamedReactionReactants { NamedreactionId = appNamedreaction.Id, ReactantId = long.Parse(reactant) }; appNamedreaction.AppNamedreactionReactants.Add(reactantToAdd); } } if (byProducts != null) { foreach (var reactant in byProducts) { var byProductToAdd = new NamedReactionByProducts { NamedreactionId = appNamedreaction.Id, ReactantId = long.Parse(reactant) }; appNamedreaction.AppNamedreactionByProducts.Add(byProductToAdd); } } if (ModelState.IsValid) { _context.Add(appNamedreaction); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateReactantData(appNamedreaction); return(View(appNamedreaction)); }