Exemplo n.º 1
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
            });
        }
        public async Task ExecuteAsync(AddPageVersionBlockCommand command, IExecutionContext executionContext)
        {
            var templateRegion = await _dbContext
                                 .PageTemplateRegions
                                 .FirstOrDefaultAsync(l => l.PageTemplateRegionId == command.PageTemplateRegionId);

            EntityNotFoundException.ThrowIfNull(templateRegion, command.PageTemplateRegionId);

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

            EntityNotFoundException.ThrowIfNull(pageVersion, command.PageVersionId);

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

            var pageVersionBlocks = pageVersion
                                    .PageVersionBlocks
                                    .Where(m => m.PageTemplateRegionId == templateRegion.PageTemplateRegionId);

            PageVersionBlock adjacentItem = null;

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

            var newBlock = new PageVersionBlock();

            newBlock.PageTemplateRegion = templateRegion;

            await _pageBlockCommandHelper.UpdateModelAsync(command, newBlock);

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

            _entityAuditHelper.SetCreated(newBlock, executionContext);
            _entityOrderableHelper.SetOrderingForInsert(pageVersionBlocks, newBlock, command.InsertMode, adjacentItem);

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

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    PageVersionBlockEntityDefinition.DefinitionCode,
                    newBlock.PageVersionBlockId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand, executionContext);

                scope.QueueCompletionTask(() => OnTransactionComplete(pageVersion, newBlock));

                await scope.CompleteAsync();
            }

            command.OutputPageBlockId = newBlock.PageVersionBlockId;
        }
Exemplo n.º 3
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);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, executionContext.UserContext);

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

            var page = await _dbContext
                       .Pages
                       .FilterActive()
                       .FilterByPageId(command.PageId)
                       .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(page, command.PageId);

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

            EntityNotFoundException.ThrowIfNull(templateRegion, command.PageTemplateRegionId);

            await ValidateTemplateUsedByPage(command, templateRegion);

            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);

                if (adjacentItem.PageTemplateRegionId != command.PageTemplateRegionId)
                {
                    throw new Exception("Error adding custom entity page block: the block specified in AdjacentVersionBlockId is in a different region to the block being added.");
                }
            }

            var newBlock = new CustomEntityVersionPageBlock();

            newBlock.PageTemplateRegion = templateRegion;
            newBlock.Page = page;
            newBlock.CustomEntityVersion = customEntityVersion;

            await _pageBlockCommandHelper.UpdateModelAsync(command, newBlock);

            _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, executionContext);

                scope.QueueCompletionTask(() => OnTransactionComplete(customEntityVersion, newBlock));

                await scope.CompleteAsync();
            }

            command.OutputCustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId;
        }
Exemplo n.º 4
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
            });
        }