コード例 #1
0
        public void Synchronize(IList <Screen> entityList, long userId)
        {
            IList <Screen> screens = _screenRepository
                                     .GetAllWithDapper()
                                     .ToList();

            var screensNaoExistentes = entityList
                                       .Where(w => !screens.Select(s => s.ControllerName).Contains(w.ControllerName))
                                       .ToList();

            var screensDelete = screens
                                .Where(w => !entityList.Select(s => s.ControllerName).Contains(w.ControllerName))
                                .ToList();

            var screensExistentes = screens
                                    .Join(entityList, t => t.ControllerName, e => e.ControllerName, (t, e) => new { t, e })
                                    .Select(s => new Screen
            {
                Id             = s.t.Id,
                Nome           = s.t.Nome,
                ControllerName = s.e.ControllerName,
                DataCadastro   = s.t.DataCadastro,
                Ativo          = s.t.Ativo,
                Create         = s.e.Create,
                Read           = s.e.Read,
                Update         = s.e.Update,
                Delete         = s.e.Delete
            })
                                    .ToList();

            _screenRepository.AddOrUpdate(screensExistentes, userId);

            if (screensNaoExistentes.Count > 0)
            {
                _screenRepository.AddOrUpdate(screensNaoExistentes, userId);
            }

            foreach (var screen in screensDelete)
            {
                _screenRepository.Remove(screen.Id, userId);
            }
        }