Ejemplo n.º 1
0
        public async Task ExecuteAsync(DeleteCustomEntityCommand command, IExecutionContext executionContext)
        {
            var customEntity = await _dbContext
                               .CustomEntities
                               .SingleOrDefaultAsync(p => p.CustomEntityId == command.CustomEntityId);

            if (customEntity != null)
            {
                await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityDeletePermission>(customEntity.CustomEntityDefinitionCode);

                using (var scope = _transactionScopeFactory.Create(_dbContext))
                {
                    await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(customEntity.CustomEntityDefinitionCode, customEntity.CustomEntityId), executionContext);

                    _dbContext.CustomEntities.Remove(customEntity);
                    await _dbContext.SaveChangesAsync();

                    await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId);

                    scope.Complete();
                }
                _customEntityCache.Clear(customEntity.CustomEntityDefinitionCode, command.CustomEntityId);

                await _messageAggregator.PublishAsync(new CustomEntityDeletedMessage()
                {
                    CustomEntityId             = command.CustomEntityId,
                    CustomEntityDefinitionCode = customEntity.CustomEntityDefinitionCode
                });
            }
        }
Ejemplo n.º 2
0
        public async Task <UpdateCustomEntityDraftVersionCommand> ExecuteAsync(GetUpdateCommandByIdQuery <UpdateCustomEntityDraftVersionCommand> query, IExecutionContext executionContext)
        {
            var dbResult = await _dbContext
                           .CustomEntityVersions
                           .Include(v => v.CustomEntity)
                           .AsNoTracking()
                           .FilterByActive()
                           .FilterByCustomEntityId(query.Id)
                           .Where(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft)
                           .SingleOrDefaultAsync();

            if (dbResult == null)
            {
                return(null);
            }
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode);

            var command = new UpdateCustomEntityDraftVersionCommand()
            {
                CustomEntityDefinitionCode = dbResult.CustomEntity.CustomEntityDefinitionCode,
                CustomEntityId             = dbResult.CustomEntityId,
                Title = dbResult.Title
            };

            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(command.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, command.CustomEntityDefinitionCode);
            command.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbResult.SerializedData, definition.DataModelType);

            return(command);
        }
Ejemplo n.º 3
0
        public async Task ExecuteAsync(UpdateCustomEntityUrlCommand command, IExecutionContext executionContext)
        {
            var entity = await _dbContext
                         .CustomEntities
                         .Where(e => e.CustomEntityId == command.CustomEntityId)
                         .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(entity, command.CustomEntityId);
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdateUrlPermission>(entity.CustomEntityDefinitionCode);

            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(entity.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, entity.CustomEntityDefinitionCode);

            await ValidateIsUnique(command, definition);

            Map(command, entity, definition);

            await _dbContext.SaveChangesAsync();

            _customEntityCache.Clear(entity.CustomEntityDefinitionCode, command.CustomEntityId);

            await _messageAggregator.PublishAsync(new CustomEntityUrlChangedMessage()
            {
                CustomEntityId             = command.CustomEntityId,
                CustomEntityDefinitionCode = entity.CustomEntityDefinitionCode,
                HasPublishedVersionChanged = entity.PublishStatusCode == PublishStatusCode.Published
            });
        }
        public async Task <CustomEntityDetails> ExecuteAsync(GetCustomEntityDetailsByIdQuery query, IExecutionContext executionContext)
        {
            var customEntityVersion = await Query(query.CustomEntityId).FirstOrDefaultAsync();

            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityReadPermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode);

            return(await MapAsync(query, customEntityVersion, executionContext));
        }
Ejemplo n.º 5
0
        public async Task ExecuteAsync(PublishCustomEntityCommand command, IExecutionContext executionContext)
        {
            // Prefer draft, but update published entity if no draft (only one draft permitted)
            var version = await _dbContext
                          .CustomEntityVersions
                          .Include(v => v.CustomEntity)
                          .Where(v => v.CustomEntityId == command.CustomEntityId && (v.WorkFlowStatusId == (int)WorkFlowStatus.Draft || v.WorkFlowStatusId == (int)WorkFlowStatus.Published))
                          .OrderByDescending(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft)
                          .ThenByDescending(v => v.CreateDate)
                          .FirstOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(version, command.CustomEntityId);

            var definition = _customEntityDefinitionRepository.GetByCode(version.CustomEntity.CustomEntityDefinitionCode);

            EntityNotFoundException.ThrowIfNull(definition, version.CustomEntity.CustomEntityDefinitionCode);

            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityPublishPermission>(definition.CustomEntityDefinitionCode);

            UpdatePublishDate(command, executionContext, version);

            if (version.WorkFlowStatusId == (int)WorkFlowStatus.Published)
            {
                // only thing we can do with a published version is update the date
                await _dbContext.SaveChangesAsync();
            }
            else
            {
                await ValidateTitle(version, definition);

                using (var scope = _transactionScopeFactory.Create(_dbContext))
                {
                    await UpdateUrlSlugIfRequired(version, definition);

                    version.WorkFlowStatusId = (int)WorkFlowStatus.Published;
                    version.CustomEntity.PublishStatusCode = PublishStatusCode.Published;

                    await _dbContext.SaveChangesAsync();

                    await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId);

                    scope.Complete();
                }
            }

            _customEntityCache.Clear(version.CustomEntity.CustomEntityDefinitionCode, command.CustomEntityId);

            await _messageAggregator.PublishAsync(new CustomEntityPublishedMessage()
            {
                CustomEntityId             = command.CustomEntityId,
                CustomEntityDefinitionCode = version.CustomEntity.CustomEntityDefinitionCode
            });
        }
Ejemplo n.º 6
0
        public async Task <CustomEntityRenderSummary> ExecuteAsync(GetCustomEntityRenderSummaryByIdQuery query, IExecutionContext executionContext)
        {
            var dbResult = await QueryAsync(query, executionContext);

            if (dbResult == null)
            {
                return(null);
            }

            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode);

            var result = await _customEntityRenderSummaryMapper.MapAsync(dbResult, executionContext);

            return(result);
        }
Ejemplo n.º 7
0
        public async Task ExecuteAsync(UpdateCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext)
        {
            var dbBlock = await _dbContext
                          .CustomEntityVersionPageBlocks
                          .Include(m => m.PageBlockTypeTemplate)
                          .Include(m => m.PageBlockType)
                          .Include(m => m.CustomEntityVersion)
                          .ThenInclude(v => v.CustomEntity)
                          .Where(l => l.CustomEntityVersionPageBlockId == command.CustomEntityVersionPageBlockId)
                          .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(dbBlock, command.CustomEntityVersionPageBlockId);
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode);

            if (dbBlock.CustomEntityVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
            {
                throw new NotPermittedException("Page blocks cannot be updated unless the entity is in draft status");
            }

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                await _pageBlockCommandHelper.UpdateModelAsync(command, dbBlock);

                await _dbContext.SaveChangesAsync();

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionPageBlockEntityDefinition.DefinitionCode,
                    dbBlock.CustomEntityVersionPageBlockId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

                scope.Complete();
            }

            _customEntityCache.Clear(dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, dbBlock.CustomEntityVersion.CustomEntity.CustomEntityId);

            await _messageAggregator.PublishAsync(new CustomEntityVersionBlockUpdatedMessage()
            {
                CustomEntityId             = dbBlock.CustomEntityVersion.CustomEntityId,
                CustomEntityDefinitionCode = dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode,
                CustomEntityVersionBlockId = dbBlock.CustomEntityVersionPageBlockId
            });
        }
Ejemplo n.º 8
0
        public async Task ExecuteAsync(DeleteCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext)
        {
            var dbResult = await _dbContext
                           .CustomEntityVersionPageBlocks
                           .Where(m => m.CustomEntityVersionPageBlockId == command.CustomEntityVersionPageBlockId)
                           .Select(m => new
            {
                Block                      = m,
                CustomEntityId             = m.CustomEntityVersion.CustomEntityId,
                CustomEntityDefinitionCode = m.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode,
                WorkFlowStatusId           = m.CustomEntityVersion.WorkFlowStatusId
            })
                           .SingleOrDefaultAsync();

            if (dbResult != null)
            {
                await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(dbResult.CustomEntityDefinitionCode);

                if (dbResult.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
                {
                    throw new NotPermittedException("Page blocks cannot be deleted unless the entity is in draft status");
                }

                var customEntityVersionBlockId = dbResult.Block.CustomEntityVersionPageBlockId;

                using (var scope = _transactionScopeFactory.Create(_dbContext))
                {
                    await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(CustomEntityVersionPageBlockEntityDefinition.DefinitionCode, dbResult.Block.CustomEntityVersionPageBlockId));

                    _dbContext.CustomEntityVersionPageBlocks.Remove(dbResult.Block);
                    await _dbContext.SaveChangesAsync();

                    scope.Complete();
                }
                _customEntityCache.Clear(dbResult.CustomEntityDefinitionCode, dbResult.CustomEntityId);

                await _messageAggregator.PublishAsync(new CustomEntityVersionBlockDeletedMessage()
                {
                    CustomEntityId             = dbResult.CustomEntityId,
                    CustomEntityDefinitionCode = dbResult.CustomEntityDefinitionCode,
                    CustomEntityVersionId      = customEntityVersionBlockId
                });
            }
        }
Ejemplo n.º 9
0
        public async Task ExecuteAsync(UpdateCustomEntityOrderingPositionCommand command, IExecutionContext executionContext)
        {
            var customEntity = await _dbContext
                               .CustomEntities
                               .AsNoTracking()
                               .FilterByCustomEntityId(command.CustomEntityId)
                               .FirstOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(customEntity, command.CustomEntityId);
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(customEntity.CustomEntityDefinitionCode);

            if (!command.Position.HasValue)
            {
                // The new position might be null (which means no ordering allocated)
                await SetOrderingNull(customEntity);
            }
            else
            {
                var orderedIds = await _dbContext
                                 .CustomEntities
                                 .Where(e => e.CustomEntityDefinitionCode == customEntity.CustomEntityDefinitionCode &&
                                        e.CustomEntityId != command.CustomEntityId &&
                                        e.Ordering.HasValue)
                                 .OrderBy(e => e.Ordering)
                                 .Select(e => e.CustomEntityId)
                                 .ToListAsync();

                var newPos = orderedIds.Count <= command.Position ? command.Position : null;

                if (!newPos.HasValue)
                {
                    await SetOrderingNull(customEntity);
                }
                else
                {
                    orderedIds.Insert(newPos.Value, command.CustomEntityId);

                    var reorderCommand = new ReOrderCustomEntitiesCommand();
                    reorderCommand.CustomEntityDefinitionCode = customEntity.CustomEntityDefinitionCode;
                    reorderCommand.OrderedCustomEntityIds     = orderedIds.ToArray();
                }
            }
        }
Ejemplo n.º 10
0
        public async Task <ICollection <CustomEntityVersionSummary> > ExecuteAsync(GetCustomEntityVersionSummariesByCustomEntityIdQuery query, IExecutionContext executionContext)
        {
            var definitionCode = await _dbContext
                                 .CustomEntities
                                 .AsNoTracking()
                                 .Where(c => c.CustomEntityId == query.CustomEntityId)
                                 .Select(c => c.CustomEntityDefinitionCode)
                                 .FirstOrDefaultAsync();

            if (definitionCode == null)
            {
                return(null);
            }

            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityReadPermission>(definitionCode);

            var dbVersions = await Query(query.CustomEntityId).ToListAsync();

            var versions = _customEntityVersionSummaryMapper.MapVersions(query.CustomEntityId, dbVersions);

            return(versions);
        }
Ejemplo n.º 11
0
        private async Task<CustomEntityRenderDetails> MapCustomEntityAsync(CustomEntityVersion dbResult)
        {
            await _permissionValidationService.EnforceCustomEntityPermissionAsync<CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode);

            var entity = new CustomEntityRenderDetails()
            {
                CreateDate = DbDateTimeMapper.AsUtc(dbResult.CreateDate),
                CustomEntityDefinitionCode = dbResult.CustomEntity.CustomEntityDefinitionCode,
                CustomEntityId = dbResult.CustomEntityId,
                CustomEntityVersionId = dbResult.CustomEntityVersionId,
                Ordering = dbResult.CustomEntity.Ordering,
                Title = dbResult.Title,
                UrlSlug = dbResult.CustomEntity.UrlSlug,
                WorkFlowStatus = (WorkFlowStatus)dbResult.WorkFlowStatusId,
                PublishDate = DbDateTimeMapper.AsUtc(dbResult.CustomEntity.PublishDate)
            };

            entity.PublishStatus = PublishStatusMapper.FromCode(dbResult.CustomEntity.PublishStatusCode);
            entity.Model = _customEntityDataModelMapper.Map(dbResult.CustomEntity.CustomEntityDefinitionCode, dbResult.SerializedData);
            
            return entity;
        }
Ejemplo n.º 12
0
        private async Task <CustomEntityVersionPageBlockRenderDetails> MapAsync(
            CustomEntityVersionPageBlock versionBlock,
            string blockTypeFileName,
            string customEntityDefinitionCode,
            PublishStatusQuery publishStatus,
            IExecutionContext executionContext
            )
        {
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityReadPermission>(customEntityDefinitionCode);

            var blockTypeQuery = new GetPageBlockTypeSummaryByIdQuery(versionBlock.PageBlockTypeId);
            var blockType      = await _queryExecutor.ExecuteAsync(blockTypeQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(blockType, versionBlock.PageBlockTypeId);

            var result = new CustomEntityVersionPageBlockRenderDetails();

            result.CustomEntityVersionPageBlockId = versionBlock.CustomEntityVersionPageBlockId;
            result.BlockType    = blockType;
            result.DisplayModel = await _pageVersionBlockModelMapper.MapDisplayModelAsync(blockTypeFileName, versionBlock, publishStatus);

            return(result);
        }
Ejemplo n.º 13
0
        public async Task ExecuteAsync(DeleteCustomEntityDraftVersionCommand command, IExecutionContext executionContext)
        {
            var draft = await _dbContext
                        .CustomEntityVersions
                        .Include(v => v.CustomEntity)
                        .SingleOrDefaultAsync(v => v.CustomEntityId == command.CustomEntityId &&
                                              v.WorkFlowStatusId == (int)WorkFlowStatus.Draft);

            if (draft != null)
            {
                await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(draft.CustomEntity.CustomEntityDefinitionCode);

                var definitionCode = draft.CustomEntity.CustomEntityDefinitionCode;
                var versionId      = draft.CustomEntityVersionId;

                using (var scope = _transactionScopeFactory.Create(_dbContext))
                {
                    await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(CustomEntityVersionEntityDefinition.DefinitionCode, draft.CustomEntityVersionId));

                    _dbContext.CustomEntityVersions.Remove(draft);
                    await _dbContext.SaveChangesAsync();

                    await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId);

                    scope.Complete();
                }
                _customEntityCache.Clear(definitionCode, command.CustomEntityId);

                await _messageAggregator.PublishAsync(new CustomEntityDraftVersionDeletedMessage()
                {
                    CustomEntityId             = command.CustomEntityId,
                    CustomEntityDefinitionCode = definitionCode,
                    CustomEntityVersionId      = versionId
                });
            }
        }
Ejemplo n.º 14
0
        public async Task <UpdateCustomEntityVersionPageBlockCommand> ExecuteAsync(GetUpdateCommandByIdQuery <UpdateCustomEntityVersionPageBlockCommand> query, IExecutionContext executionContext)
        {
            var dbResult = await _dbContext
                           .CustomEntityVersionPageBlocks
                           .AsNoTracking()
                           .Where(b => b.CustomEntityVersionPageBlockId == query.Id)
                           .Select(b => new
            {
                PageBlock                  = b,
                PageBlockTypeFileName      = b.PageBlockType.FileName,
                CustomEntityDefinitionCode = b.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode
            })
                           .SingleOrDefaultAsync();

            if (dbResult == null)
            {
                return(null);
            }
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityReadPermission>(dbResult.CustomEntityDefinitionCode);

            var result = Map(dbResult.PageBlock, dbResult.PageBlockTypeFileName);

            return(result);
        }
Ejemplo n.º 15
0
        public async Task ExecuteAsync(AddCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext)
        {
            var customEntityVersion = _dbContext
                                      .CustomEntityVersions
                                      .Include(s => s.CustomEntityVersionPageBlocks)
                                      .Include(s => s.CustomEntity)
                                      .FirstOrDefault(v => v.CustomEntityVersionId == command.CustomEntityVersionId);

            EntityNotFoundException.ThrowIfNull(customEntityVersion, command.CustomEntityVersionId);
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode);

            if (customEntityVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
            {
                throw new NotPermittedException("Page blocks cannot be deleted unless the entity is in draft status");
            }

            var templateRegion = await _dbContext
                                 .PageTemplateRegions
                                 .FirstOrDefaultAsync(l => l.PageTemplateRegionId == command.PageTemplateRegionId);

            EntityNotFoundException.ThrowIfNull(templateRegion, command.PageTemplateRegionId);

            var customEntityVersionBlocks = customEntityVersion
                                            .CustomEntityVersionPageBlocks
                                            .Where(m => m.PageTemplateRegionId == templateRegion.PageTemplateRegionId);

            CustomEntityVersionPageBlock adjacentItem = null;

            if (command.AdjacentVersionBlockId.HasValue)
            {
                adjacentItem = customEntityVersionBlocks
                               .SingleOrDefault(m => m.CustomEntityVersionPageBlockId == command.AdjacentVersionBlockId);
                EntityNotFoundException.ThrowIfNull(adjacentItem, command.AdjacentVersionBlockId);
            }

            var newBlock = new CustomEntityVersionPageBlock();

            newBlock.PageTemplateRegion = templateRegion;

            await _pageBlockCommandHelper.UpdateModelAsync(command, newBlock);

            newBlock.CustomEntityVersion = customEntityVersion;

            _entityOrderableHelper.SetOrderingForInsert(customEntityVersionBlocks, newBlock, command.InsertMode, adjacentItem);

            _dbContext.CustomEntityVersionPageBlocks.Add(newBlock);

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                await _dbContext.SaveChangesAsync();

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionPageBlockEntityDefinition.DefinitionCode,
                    newBlock.CustomEntityVersionPageBlockId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

                scope.Complete();
            }
            _customEntityCache.Clear(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, customEntityVersion.CustomEntityId);

            command.OutputCustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId;

            await _messageAggregator.PublishAsync(new CustomEntityVersionBlockAddedMessage()
            {
                CustomEntityId = customEntityVersion.CustomEntityId,
                CustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId,
                CustomEntityDefinitionCode     = customEntityVersion.CustomEntity.CustomEntityDefinitionCode
            });
        }
Ejemplo n.º 16
0
        public async Task ExecuteAsync(MoveCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext)
        {
            var dbResult = await _dbContext
                           .CustomEntityVersionPageBlocks
                           .Where(b => b.CustomEntityVersionPageBlockId == command.CustomEntityVersionPageBlockId)
                           .Select(b => new
            {
                Block                      = b,
                CustomEntityId             = b.CustomEntityVersion.CustomEntityId,
                CustomEntityDefinitionCode = b.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode,
                WorkFlowStatusId           = b.CustomEntityVersion.WorkFlowStatusId
            })
                           .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(dbResult, command.CustomEntityVersionPageBlockId);
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(dbResult.CustomEntityDefinitionCode);

            if (dbResult.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
            {
                throw new NotPermittedException("Page blocks cannot be moved unless the entity is in draft status");
            }

            var block = dbResult.Block;
            var blockToSwapWithQuery = _dbContext
                                       .CustomEntityVersionPageBlocks
                                       .Where(p => p.PageTemplateRegionId == block.PageTemplateRegionId && p.CustomEntityVersionId == block.CustomEntityVersionId);

            CustomEntityVersionPageBlock blockToSwapWith;

            switch (command.Direction)
            {
            case OrderedItemMoveDirection.Up:
                blockToSwapWith = await blockToSwapWithQuery
                                  .Where(p => p.Ordering < block.Ordering)
                                  .OrderByDescending(p => p.Ordering)
                                  .FirstOrDefaultAsync();

                break;

            case OrderedItemMoveDirection.Down:
                blockToSwapWith = await blockToSwapWithQuery
                                  .Where(p => p.Ordering > block.Ordering)
                                  .OrderBy(p => p.Ordering)
                                  .FirstOrDefaultAsync();

                break;

            default:
                throw new InvalidOperationException("OrderedItemMoveDirection not recognised: " + command.Direction);
            }

            if (blockToSwapWith == null)
            {
                return;
            }

            int oldOrdering = block.Ordering;

            block.Ordering           = blockToSwapWith.Ordering;
            blockToSwapWith.Ordering = oldOrdering;

            await _dbContext.SaveChangesAsync();

            _customEntityCache.Clear(dbResult.CustomEntityDefinitionCode, dbResult.CustomEntityId);

            await _messageAggregator.PublishAsync(new CustomEntityVersionBlockMovedMessage()
            {
                CustomEntityId             = dbResult.CustomEntityId,
                CustomEntityDefinitionCode = dbResult.CustomEntityDefinitionCode,
                CustomEntityVersionBlockId = dbResult.Block.CustomEntityVersionPageBlockId
            });
        }