Exemplo n.º 1
0
        private void UpdateNamedReactionReactants(string[] selectedReactants, NamedReaction reactionToUpdate)
        {
            if (selectedReactants == null)
            {
                reactionToUpdate.AppNamedreactionReactants = new List <NamedReactionReactants>();
                return;
            }

            var selectedReactantsHS = new HashSet <string>(selectedReactants);
            var reactionReactants   = new HashSet <long>
                                          (reactionToUpdate.AppNamedreactionReactants.Select(c => c.Reactant.Id));

            foreach (var reaction in _context.AppReactant)
            {
                if (selectedReactantsHS.Contains(reaction.Id.ToString()))
                {
                    if (!reactionReactants.Contains(reaction.Id))
                    {
                        reactionToUpdate.AppNamedreactionReactants.Add(new NamedReactionReactants {
                            NamedreactionId = reactionToUpdate.Id, ReactantId = reaction.Id
                        });
                    }
                }
                else
                {
                    if (reactionReactants.Contains(reaction.Id))
                    {
                        NamedReactionReactants reactionToRemove = reactionToUpdate.AppNamedreactionReactants.SingleOrDefault(i => i.ReactantId == reaction.Id);
                        _context.Remove(reactionToRemove);
                    }
                }
            }
        }
        public async Task <IActionResult> DeleteConfirmed(long id)
        {
            var appFunctionalgroup = await _context.AppFunctionalgroup.FindAsync(id);

            var fileName = _hostingEnvironment.WebRootPath + "/" + appFunctionalgroup.Image;

            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }
            var reactions = _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)
                            .Where(i => i.FunctionalGroupId == id).ToList();

            foreach (NamedReaction rxn in reactions)
            {
                fileName = _hostingEnvironment.WebRootPath + "/" + rxn.Image;
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }
                var reactionByProducts = new HashSet <long>
                                             (rxn.AppNamedreactionByProducts.Select(c => c.Reactant.Id));
                foreach (var reactant in _context.AppReactant)
                {
                    if (reactionByProducts.Contains(reactant.Id))
                    {
                        NamedReactionByProducts reactantToRemove = rxn.AppNamedreactionByProducts.SingleOrDefault(i => i.ReactantId == reactant.Id);
                        _context.Remove(reactantToRemove);
                    }
                }

                var reactionReactants = new HashSet <long>
                                            (rxn.AppNamedreactionReactants.Select(c => c.Reactant.Id));
                foreach (var reactant in _context.AppReactant)
                {
                    if (reactionReactants.Contains(reactant.Id))
                    {
                        NamedReactionReactants reactantToRemove = rxn.AppNamedreactionReactants.SingleOrDefault(i => i.ReactantId == reactant.Id);
                        _context.Remove(reactantToRemove);
                    }
                }
                rxn.FunctionalGroup = null;
                rxn.Catalyst        = null;
                rxn.Solvent         = null;
                _context.AppNamedreaction.Remove(rxn);
            }
            var references = _context.AppReference.Where(i => i.FunctionalGroupId == id).ToList();

            foreach (Reference r in references)
            {
                _context.AppReference.Remove(r);
            }

            _context.AppFunctionalgroup.Remove(appFunctionalgroup);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }