Ejemplo n.º 1
0
        public async Task ExecuteAsync(UpdatePageUrlCommand command, IExecutionContext executionContext)
        {
            Normalize(command);

            var page = await _dbContext
                       .Pages
                       .Include(p => p.Locale)
                       .Include(p => p.PageDirectory)
                       .FilterActive()
                       .FilterById(command.PageId)
                       .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(page, command.PageId);

            await ValidateIsPageUniqueAsync(command, page, executionContext);

            var originalPageRoute = await _queryExecutor.ExecuteAsync(new GetPageRouteByIdQuery(page.PageId));

            EntityNotFoundException.ThrowIfNull(originalPageRoute, page.PageId);

            await MapPageAsync(command, executionContext, page);

            var isPublished = page.PublishStatusCode == PublishStatusCode.Published;

            await _dbContext.SaveChangesAsync();

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(originalPageRoute, isPublished));
        }
        public async Task ExecuteAsync(DeletePageDirectoryCommand command, IExecutionContext executionContext)
        {
            var pageDirectory = await _dbContext
                                .PageDirectories
                                .SingleOrDefaultAsync(d => d.PageDirectoryId == command.PageDirectoryId);

            if (pageDirectory == null)
            {
                return;
            }
            ValidateNotRootDirectory(pageDirectory);

            var directoriesToDelete = await GetDirectoryIdsToDeleteAsync(command);

            await ValidateDependencies(PageDirectoryEntityDefinition.DefinitionCode, directoriesToDelete.Keys, executionContext);

            var pagesToDelete = await GetPageIdsToDeleteAndValidatePermissionAsync(directoriesToDelete.Keys, executionContext);

            await ValidateDependencies(PageEntityDefinition.DefinitionCode, pagesToDelete.Keys, executionContext);

            _dbContext.PageDirectories.Remove(pageDirectory);
            await _dbContext.SaveChangesAsync();

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(directoriesToDelete.Values, pagesToDelete.Values));
        }
        public async Task ExecuteAsync(UpdatePageCommand command, IExecutionContext executionContext)
        {
            var page = await GetPageAsync(command.PageId);

            MapPage(command, executionContext, page);

            await _dbContext.SaveChangesAsync();

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(page));
        }
Ejemplo n.º 4
0
        public async Task ExecuteAsync(UpdatePageCommand command, IExecutionContext executionContext)
        {
            var page = await GetPageById(command.PageId).SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(page, command.PageId);

            MapPage(command, executionContext, page);
            await _dbContext.SaveChangesAsync();

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(page));
        }
        public async Task ExecuteAsync(AddPageDraftVersionCommand command, IExecutionContext executionContext)
        {
            var newVersionId = await _pageStoredProcedures.AddDraftAsync(
                command.PageId,
                command.CopyFromPageVersionId,
                executionContext.ExecutionDate,
                executionContext.UserContext.UserId.Value);

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(command, newVersionId));

            // Set Ouput
            command.OutputPageVersionId = newVersionId;
        }
        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);

            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityPublishPermission>(definition.CustomEntityDefinitionCode, executionContext.UserContext);

            UpdatePublishDate(command, executionContext, version);

            if (version.WorkFlowStatusId == (int)WorkFlowStatus.Published &&
                version.CustomEntity.PublishStatusCode == PublishStatusCode.Published)
            {
                // only thing we can do with a published version is update the date
                await _dbContext.SaveChangesAsync();

                await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(version));
            }
            else
            {
                await ValidateTitleAsync(version, definition, executionContext);

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

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

                    await _dbContext.SaveChangesAsync();

                    await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId);

                    scope.QueueCompletionTask(() => OnTransactionComplete(version));

                    await scope.CompleteAsync();
                }
            }
        }
        public async Task ExecuteAsync(AddPageDraftVersionCommand command, IExecutionContext executionContext)
        {
            int newVersionId;

            try
            {
                newVersionId = await _pageStoredProcedures.AddDraftAsync(
                    command.PageId,
                    command.CopyFromPageVersionId,
                    executionContext.ExecutionDate,
                    executionContext.UserContext.UserId.Value);
            }
            catch (StoredProcedureExecutionException ex) when(ex.ErrorNumber == StoredProcedureErrorNumbers.Page_AddDraft.DraftAlreadyExists)
            {
                throw ValidationErrorException.CreateWithProperties("A draft cannot be created because this page already has one.", nameof(command.PageId));
            }

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(command, newVersionId));

            command.OutputPageVersionId = newVersionId;
        }
Ejemplo n.º 8
0
        public async Task ExecuteAsync(AddUserCommand command, IExecutionContext executionContext)
        {
            var userArea   = _userAreaRepository.GetRequiredByCode(command.UserAreaCode);
            var dbUserArea = await GetUserAreaAsync(userArea);

            var role = await GetAndValidateRoleAsync(command, executionContext);

            var user = new User()
            {
                FirstName              = command.FirstName?.Trim(),
                LastName               = command.LastName?.Trim(),
                RequirePasswordChange  = command.RequirePasswordChange,
                LastPasswordChangeDate = executionContext.ExecutionDate,
                AccountVerifiedDate    = command.IsAccountVerified ? executionContext.ExecutionDate : (DateTime?)null,
                CreateDate             = executionContext.ExecutionDate,
                Role          = role,
                UserArea      = dbUserArea,
                CreatorId     = executionContext.UserContext.UserId,
                SecurityStamp = _securityStampGenerator.Generate()
            };

            if (!command.IsActive)
            {
                user.DeactivatedDate = executionContext.ExecutionDate;
            }

            await _userUpdateCommandHelper.UpdateEmailAndUsernameAsync(command.Email, command.Username, user, executionContext);

            await ValidatePasswordAsync(userArea, user, command, executionContext);

            SetPassword(user, command, userArea);
            SetDisplayName(command, user);

            _dbContext.Users.Add(user);
            await _dbContext.SaveChangesAsync();

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(userArea, user));

            command.OutputUserId = user.UserId;
        }
        public async Task ExecuteAsync(PublishPageCommand command, IExecutionContext executionContext)
        {
            var version = await _dbContext
                          .PageVersions
                          .Include(p => p.Page)
                          .FilterActive()
                          .FilterByPageId(command.PageId)
                          .OrderByLatest()
                          .FirstOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(version, command.PageId);

            UpdatePublishDate(command, executionContext, version);

            if (version.WorkFlowStatusId == (int)WorkFlowStatus.Published &&
                version.Page.PublishStatusCode == PublishStatusCode.Published)
            {
                // only thing we can do with a published version is update the date
                await _dbContext.SaveChangesAsync();

                await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(command));
            }
            else
            {
                version.WorkFlowStatusId       = (int)WorkFlowStatus.Published;
                version.Page.PublishStatusCode = PublishStatusCode.Published;

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

                    await _pageStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.PageId);

                    scope.QueueCompletionTask(() => OnTransactionComplete(command));

                    await scope.CompleteAsync();
                }
            }
        }
Ejemplo n.º 10
0
        public async Task ExecuteAsync(ReOrderCustomEntitiesCommand command, IExecutionContext executionContext)
        {
            var definition = _customEntityDefinitionRepository.GetByCode(command.CustomEntityDefinitionCode) as IOrderableCustomEntityDefinition;

            if (definition == null || definition.Ordering == CustomEntityOrdering.None)
            {
                throw new InvalidOperationException("Cannot re-order a custom entity type with a definition that does not implement IOrderableCustomEntityDefinition (" + definition.GetType().Name + ")");
            }

            if (!definition.HasLocale && command.LocaleId.HasValue)
            {
                throw new ValidationException("Cannot order by locale because this custom entity type is not permitted to have a locale (" + definition.GetType().FullName + ")");
            }

            var affectedIds = await _customEntityStoredProcedures.ReOrderAsync(
                command.CustomEntityDefinitionCode,
                command.OrderedCustomEntityIds,
                command.LocaleId
                );

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(command, affectedIds));
        }
Ejemplo n.º 11
0
        public async Task ExecuteAsync(AddCustomEntityDraftVersionCommand command, IExecutionContext executionContext)
        {
            var definitionCode = await QueryVersionAndGetDefinitionCode(command).FirstOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(definitionCode, command.CustomEntityId);

            _permissionValidationService.EnforceIsLoggedIn(executionContext.UserContext);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(definitionCode, executionContext.UserContext);

            var newVersionId = await _customEntityStoredProcedures.AddDraftAsync(
                command.CustomEntityId,
                command.CopyFromCustomEntityVersionId,
                executionContext.ExecutionDate,
                executionContext.UserContext.UserId.Value);

            command.OutputCustomEntityVersionId = newVersionId;

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(
                                                                        command,
                                                                        definitionCode,
                                                                        newVersionId)
                                                                    );
        }
Ejemplo n.º 12
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);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdateUrlPermission>(entity.CustomEntityDefinitionCode, executionContext.UserContext);

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

            EntityNotFoundException.ThrowIfNull(definition, entity.CustomEntityDefinitionCode);

            await ValidateIsUniqueAsync(command, definition, executionContext);

            Map(command, entity, definition);

            await _dbContext.SaveChangesAsync();

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(entity));
        }
Ejemplo n.º 13
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);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(dbResult.CustomEntityDefinitionCode, executionContext.UserContext);

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

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(
                                                                        dbResult.CustomEntityDefinitionCode,
                                                                        dbResult.CustomEntityId,
                                                                        dbResult.Block.CustomEntityVersionPageBlockId)
                                                                    );
        }