Example #1
0
 public ProjectService(
     HomeBuilderDbContext context,
     LocationService locationService)
 {
     _context         = context;
     _locationService = locationService;
 }
Example #2
0
        public static void TryUpdateProjectBlocks <TKey>(this HomeBuilderDbContext context, IEnumerable <ProjectBlock> currentEntities, IEnumerable <ProjectBlock> newEntities, Func <ProjectBlock, TKey> getKey)
        {
            var dbSet = context.Set <ProjectBlock>();

            var deletedEntities = currentEntities.Except(newEntities, getKey);

            dbSet.RemoveRange(deletedEntities);

            var addedEntities = newEntities.Except(currentEntities, getKey);

            if (addedEntities.Count() != 0)
            {
                dbSet.AddRange(addedEntities);
            }

            var modifiedEntities = newEntities.Where(o => o.Id > 0);

            foreach (var entity in modifiedEntities)
            {
                var existingItem = currentEntities.FirstOrDefault(o => o.Id == entity.Id);

                var entityEntry = context.Entry(existingItem);
                entityEntry.CurrentValues.SetValues(entity);

                entityEntry.Property(o => o.ParentId).IsModified = false;

                if (entityEntry.Entity.Children.Count() != 0 || entity.Children.Count() != 0)
                {
                    foreach (var child in entity.Children)
                    {
                        child.ParentId = entityEntry.Entity.Id;
                    }
                    context.TryUpdateProjectBlocks(entityEntry.Entity.Children, entity.Children, getKey);
                }

                context.TryUpdateManyToMany(existingItem.ProjectBlockFiles, entity.ProjectBlockFiles, o => o.FileEntityId);

                foreach (var newDetail in entity.ProjectBlockDetails)
                {
                    var oldDetail = entityEntry.Entity.ProjectBlockDetails.FirstOrDefault(o => o.Language == newDetail.Language);
                    if (oldDetail.Language == newDetail.Language)
                    {
                        newDetail.Id = oldDetail.Id;
                        context.Entry(oldDetail).CurrentValues.SetValues(newDetail);
                    }
                }
            }
        }
 public PackageService(HomeBuilderDbContext context)
 {
     _context = context;
 }