Exemple #1
0
        public async Task <IActionResult> Details(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var appNamedreaction = await _context.AppNamedreaction
                                   .Include(a => a.Catalyst)
                                   .Include(a => a.FunctionalGroup)
                                   .Include(a => a.Solvent)
                                   .Include(a => a.AppNamedreactionReactants)
                                   .ThenInclude(a => a.Reactant)
                                   .Include(a => a.AppNamedreactionByProducts)
                                   .ThenInclude(a => a.Reactant)
                                   .AsNoTracking()
                                   .FirstOrDefaultAsync(m => m.Id == id);

            if (appNamedreaction == null)
            {
                return(NotFound());
            }

            var reactionViewModel = new SustainableChemistryWeb.ViewModels.NamedReactionViewModel
            {
                Id                         = appNamedreaction.Id,
                Name                       = appNamedreaction.Name,
                FunctionalGroup            = appNamedreaction.FunctionalGroup,
                Catalyst                   = appNamedreaction.Catalyst,
                Solvent                    = appNamedreaction.Solvent,
                Product                    = appNamedreaction.Product,
                AppNamedreactionByProducts = appNamedreaction.AppNamedreactionByProducts,
                AppNamedreactionReactants  = appNamedreaction.AppNamedreactionReactants,
                AppReference               = appNamedreaction.AppReference,
                Url                        = appNamedreaction.Url,
                Image                      = appNamedreaction.Image,
            };

            if (appNamedreaction.Heat == "HE")
            {
                reactionViewModel.Heat = "Heat";
            }
            else
            {
                reactionViewModel.Heat = "Not Applicable";
            }

            if (appNamedreaction.AcidBase == "AC")
            {
                reactionViewModel.AcidBase = "Acid";
            }
            else if (appNamedreaction.AcidBase == "BA")
            {
                reactionViewModel.AcidBase = "Base";
            }
            else if (appNamedreaction.AcidBase == "AB")
            {
                reactionViewModel.AcidBase = "Acid Or Base";
            }
            else
            {
                reactionViewModel.AcidBase = "Not Applicable";
            }

            return(View(reactionViewModel));
        }
Exemple #2
0
        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));
        }