public async Task ExecuteAsync(UpdateCustomEntityVersionPageModuleCommand command, IExecutionContext executionContext)
        {
            var dbModule = await _dbContext
                           .CustomEntityVersionPageModules
                           .Include(m => m.PageModuleTypeTemplate)
                           .Include(m => m.PageModuleType)
                           .Include(m => m.CustomEntityVersion)
                           .Include(m => m.CustomEntityVersion.CustomEntity)
                           .Where(l => l.CustomEntityVersionPageModuleId == command.CustomEntityVersionPageModuleId)
                           .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(dbModule, command.CustomEntityVersionPageModuleId);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(dbModule.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode);

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

            using (var scope = _transactionScopeFactory.Create())
            {
                await _pageModuleCommandHelper.UpdateModelAsync(command, dbModule);

                await _dbContext.SaveChangesAsync();

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionPageModuleEntityDefinition.DefinitionCode,
                    dbModule.CustomEntityVersionPageModuleId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

                scope.Complete();
            }

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

            await _messageAggregator.PublishAsync(new CustomEntityVersionModuleUpdatedMessage()
            {
                CustomEntityId              = dbModule.CustomEntityVersion.CustomEntityId,
                CustomEntityDefinitionCode  = dbModule.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode,
                CustomEntityVersionModuleId = dbModule.CustomEntityVersionPageModuleId
            });
        }
        public async Task ExecuteAsync(AddCustomEntityVersionPageModuleCommand command, IExecutionContext executionContext)
        {
            var customEntityVersion = _dbContext
                                      .CustomEntityVersions
                                      .Include(s => s.CustomEntityVersionPageModules)
                                      .Include(s => s.CustomEntity)
                                      .FirstOrDefault(v => v.CustomEntityVersionId == command.CustomEntityVersionId);

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

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

            var templateSectionSection = await _dbContext
                                         .PageTemplateSections
                                         .FirstOrDefaultAsync(l => l.PageTemplateSectionId == command.PageTemplateSectionId);

            EntityNotFoundException.ThrowIfNull(templateSectionSection, command.PageTemplateSectionId);

            var customEntityVersionModules = customEntityVersion
                                             .CustomEntityVersionPageModules
                                             .Where(m => m.PageTemplateSectionId == templateSectionSection.PageTemplateSectionId);

            CustomEntityVersionPageModule adjacentItem = null;

            if (command.AdjacentVersionModuleId.HasValue)
            {
                adjacentItem = customEntityVersionModules
                               .SingleOrDefault(m => m.CustomEntityVersionPageModuleId == command.AdjacentVersionModuleId);
                EntityNotFoundException.ThrowIfNull(adjacentItem, command.AdjacentVersionModuleId);
            }

            var newModule = new CustomEntityVersionPageModule();

            newModule.PageTemplateSection = templateSectionSection;

            await _pageModuleCommandHelper.UpdateModelAsync(command, newModule);

            newModule.CustomEntityVersion = customEntityVersion;

            _entityOrderableHelper.SetOrderingForInsert(customEntityVersionModules, newModule, command.InsertMode, adjacentItem);

            _dbContext.CustomEntityVersionPageModules.Add(newModule);

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

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionPageModuleEntityDefinition.DefinitionCode,
                    newModule.CustomEntityVersionPageModuleId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

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

            command.OutputCustomEntityVersionId = newModule.CustomEntityVersionPageModuleId;

            await _messageAggregator.PublishAsync(new CustomEntityVersionModuleAddedMessage()
            {
                CustomEntityId = customEntityVersion.CustomEntityId,
                CustomEntityVersionPageModuleId = newModule.CustomEntityVersionPageModuleId,
                CustomEntityDefinitionCode      = customEntityVersion.CustomEntity.CustomEntityDefinitionCode
            });
        }
        public async Task ExecuteAsync(AddPageVersionModuleCommand command, IExecutionContext executionContext)
        {
            var templateSectionSection = await _dbContext
                                         .PageTemplateSections
                                         .FirstOrDefaultAsync(l => l.PageTemplateSectionId == command.PageTemplateSectionId);

            EntityNotFoundException.ThrowIfNull(templateSectionSection, command.PageTemplateSectionId);

            var pageVersion = _dbContext
                              .PageVersions
                              .Include(s => s.PageVersionModules)
                              .FirstOrDefault(v => v.PageVersionId == command.PageVersionId);

            EntityNotFoundException.ThrowIfNull(pageVersion, command.PageVersionId);

            if (pageVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
            {
                throw new NotPermittedException("Page modules cannot be added unless the page version is in draft status");
            }

            var pageVersionModules = pageVersion
                                     .PageVersionModules
                                     .Where(m => m.PageTemplateSectionId == templateSectionSection.PageTemplateSectionId);

            PageVersionModule adjacentItem = null;

            if (command.AdjacentVersionModuleId.HasValue)
            {
                adjacentItem = pageVersionModules
                               .SingleOrDefault(m => m.PageVersionModuleId == command.AdjacentVersionModuleId);
                EntityNotFoundException.ThrowIfNull(adjacentItem, command.AdjacentVersionModuleId);
            }

            var newModule = new PageVersionModule();

            newModule.PageTemplateSection = templateSectionSection;

            await _pageModuleCommandHelper.UpdateModelAsync(command, newModule);

            newModule.PageVersion = pageVersion;
            newModule.UpdateDate  = executionContext.ExecutionDate;

            _entityAuditHelper.SetCreated(newModule, executionContext);
            _entityOrderableHelper.SetOrderingForInsert(pageVersionModules, newModule, command.InsertMode, adjacentItem);

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

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    PageVersionModuleEntityDefinition.DefinitionCode,
                    newModule.PageVersionModuleId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

                scope.Complete();
            }
            _pageCache.Clear(pageVersion.PageId);

            command.OutputPageModuleId = newModule.PageVersionModuleId;

            await _messageAggregator.PublishAsync(new PageVersionModuleAddedMessage()
            {
                PageId = pageVersion.PageId,
                PageVersionModuleId = newModule.PageVersionModuleId
            });
        }