private void UpdateNamedReactionByProducts(string[] selectedByProducts, NamedReaction reactionToUpdate) { if (selectedByProducts == null) { reactionToUpdate.AppNamedreactionByProducts = new List <NamedReactionByProducts>(); return; } var selectedReactantsHS = new HashSet <string>(selectedByProducts); var reactionByProducts = new HashSet <long> (reactionToUpdate.AppNamedreactionByProducts.Select(c => c.Reactant.Id)); foreach (var reactant in _context.AppReactant) { if (selectedReactantsHS.Contains(reactant.Id.ToString())) { if (!reactionByProducts.Contains(reactant.Id)) { reactionToUpdate.AppNamedreactionByProducts.Add(new NamedReactionByProducts { NamedreactionId = reactionToUpdate.Id, ReactantId = reactant.Id }); } } else { if (reactionByProducts.Contains(reactant.Id)) { NamedReactionByProducts courseToRemove = reactionToUpdate.AppNamedreactionByProducts.SingleOrDefault(i => i.ReactantId == reactant.Id); _context.Remove(courseToRemove); } } } }
// GET: Namedreactions/Create public IActionResult Create() { var reaction = new NamedReaction { AppNamedreactionReactants = new List <NamedReactionReactants>(), AppNamedreactionByProducts = new List <NamedReactionByProducts>() }; PopulateReactantData(reaction); List <SelectListItem> acidBaseList = new List <SelectListItem> { new SelectListItem { Value = "AC", Text = "Acid" }, new SelectListItem { Value = "BA", Text = "Base" }, new SelectListItem { Value = "AB", Text = "Acid Or Base" }, new SelectListItem { Value = "NA", Text = "Not Applicable" } }; ViewData["AcidBaseList"] = new SelectList(acidBaseList, "Value", "Text", "NA"); List <SelectListItem> heatList = new List <SelectListItem> { new SelectListItem { Value = "HE", Text = "Heat" }, new SelectListItem { Value = "NA", Text = "Not Applicable" } }; ViewData["HeatList"] = new SelectList(heatList, "Value", "Text", "NA"); ViewData["CatalystId"] = new SelectList(_context.AppCatalyst.OrderBy(i => i.Name.ToLower()).ToList(), "Id", "Name", 1); ViewData["FunctionalGroupId"] = new SelectList(_context.AppFunctionalgroup.OrderBy(i => i.Name.ToLower()).ToList(), "Id", "Name"); ViewData["SolventId"] = new SelectList(_context.AppSolvent.OrderBy(i => i.Name.ToLower()).ToList(), "Id", "Name", 1); return(View()); }
private void PopulateReactantData(NamedReaction reaction) { var allReactants = _context.AppReactant.OrderBy(i => i.Name.ToLower()).ToList(); var reactionReactants = new HashSet <long>(reaction.AppNamedreactionReactants.Select(c => c.ReactantId)); var reactantList = new List <SelectListItem>(); List <string> selectedReactants = new List <string>(); var reactionByProducts = new HashSet <long>(reaction.AppNamedreactionByProducts.Select(c => c.ReactantId)); var byProductList = new List <SelectListItem>(); List <string> selectedByProducts = new List <string>(); foreach (var reactant in allReactants) { bool isReactant = reactionReactants.Contains(reactant.Id); if (isReactant) { selectedReactants.Add(reactant.Id.ToString()); } reactantList.Add(new SelectListItem { Value = reactant.Id.ToString(), Text = reactant.Name, Selected = isReactant }); bool isByProduct = reactionByProducts.Contains(reactant.Id); if (isByProduct) { selectedByProducts.Add(reactant.Id.ToString()); } byProductList.Add(new SelectListItem { Value = reactant.Id.ToString(), Text = reactant.Name, Selected = isByProduct }); } ViewData["Reactants"] = new MultiSelectList(reactantList, "Value", "Text", selectedReactants); ViewData["ByProducts"] = new MultiSelectList(byProductList, "Value", "Text", selectedByProducts); }
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)); }
public async Task <IActionResult> Index(int?Id, int?funcGroupId, int?namedReactionId, string nameSearchString, string smilesSearchString) { if (!string.IsNullOrEmpty(smilesSearchString)) { ViewData["SearchString"] = smilesSearchString.Trim(); } var viewModel = new SustainableChemistryWeb.ViewModels.FunctionalGroupIndexData(); viewModel.FunctionalGroups = await _context.AppFunctionalgroup .Include(i => i.AppNamedreaction) .ThenInclude(i => i.AppReference) .AsNoTracking() .OrderBy(i => i.Name) .ToListAsync(); List <FunctionalGroup> fgFound = new List <FunctionalGroup>(); ChemInfo.Molecule molecule = null; string IUPacName = string.Empty; string CASNo = string.Empty; string DTXSID = string.Empty; if (!String.IsNullOrEmpty(smilesSearchString)) { if (TestCASNo(ref CASNo, ref smilesSearchString, ref IUPacName, ref DTXSID)) { molecule = new ChemInfo.Molecule(smilesSearchString.Trim()); if (molecule.Atoms.Length != 0) { foreach (var fg in viewModel.FunctionalGroups) { string smarts = fg.Smarts; if (!string.IsNullOrEmpty(fg.Smarts)) { if (molecule.FindFunctionalGroup(fg)) { fgFound.Add(fg); } } } if (molecule.Aromatic) { fgFound.Add(_context.AppFunctionalgroup .FirstOrDefault(m => m.Id == 35)); } if (molecule.Heterocyclic) { fgFound.Add(_context.AppFunctionalgroup .FirstOrDefault(m => m.Id == 118)); } if (molecule.HeterocyclicAromatic) { fgFound.Add(_context.AppFunctionalgroup .FirstOrDefault(m => m.Id == 224)); } } } else { return(RedirectToAction("Index", "Home", new { message = "Search String \"" + smilesSearchString + "\" was not found." })); } } else if (!String.IsNullOrEmpty(nameSearchString)) { ViewData["NameSearchString"] = nameSearchString.Trim(); fgFound.AddRange(viewModel.FunctionalGroups.Where(s => s.Name.Contains(nameSearchString, StringComparison.OrdinalIgnoreCase))); ViewData["FunctionalGroupName"] = nameSearchString.Trim(); } else if (funcGroupId != null) { ViewData["FunctionalGroupID"] = funcGroupId.Value; FunctionalGroup group = viewModel.FunctionalGroups.Where( i => i.Id == funcGroupId.Value).Single(); fgFound.Add(group); } if (funcGroupId != null) { ViewData["FunctionalGroupID"] = funcGroupId.Value; FunctionalGroup group = viewModel.FunctionalGroups.Where( i => i.Id == funcGroupId.Value).Single(); viewModel.NamedReactions = group.AppNamedreaction; ViewData["FunctionalGroupName"] = group.Name; } if (namedReactionId != null) { ViewData["NamedReactionID"] = namedReactionId.Value; NamedReaction rxn = viewModel.NamedReactions.Where( i => i.Id == namedReactionId.Value).Single(); var referenceViewModels = new List <SustainableChemistryWeb.ViewModels.ReferenceViewModel>(); ViewData["NamedReactionName"] = rxn.Name; foreach (var referecnce in rxn.AppReference) { referenceViewModels.Add(new SustainableChemistryWeb.ViewModels.ReferenceViewModel { Id = referecnce.Id, FunctionalGroupId = referecnce.FunctionalGroupId, FunctionalGroup = viewModel.FunctionalGroups.Where( i => i.Id == referecnce.FunctionalGroupId).Single(), ReactionId = referecnce.ReactionId, Reaction = referecnce.Reaction, Risdata = referecnce.Risdata }); } viewModel.References = referenceViewModels; } if (!string.IsNullOrEmpty(nameSearchString) || !string.IsNullOrEmpty(smilesSearchString) || funcGroupId != null) { viewModel.FunctionalGroups = fgFound.OrderBy(i => i.Name); } ViewData["SmilesString"] = smilesSearchString; ViewData["CASNO"] = CASNo; ViewData["IUPACName"] = IUPacName; ViewData["DTXSID"] = DTXSID; return(View(viewModel)); }