private CustomEntity MapEntity(AddCustomEntityCommand command, CustomEntityDefinitionSummary definition, IExecutionContext executionContext)
        {
            var entity = new CustomEntity();

            _entityAuditHelper.SetCreated(entity, executionContext);

            entity.Locale  = GetLocale(command.LocaleId);
            entity.UrlSlug = command.UrlSlug;
            entity.CustomEntityDefinitionCode = definition.CustomEntityDefinitionCode;

            var version = new CustomEntityVersion();

            version.Title          = command.Title.Trim();
            version.SerializedData = _dbUnstructuredDataSerializer.Serialize(command.Model);
            version.DisplayVersion = 1;

            if (command.Publish)
            {
                entity.SetPublished(executionContext.ExecutionDate, command.PublishDate);
                version.WorkFlowStatusId = (int)WorkFlowStatus.Published;
            }
            else
            {
                entity.PublishStatusCode = PublishStatusCode.Unpublished;
                version.WorkFlowStatusId = (int)WorkFlowStatus.Draft;
            }

            _entityAuditHelper.SetCreated(version, executionContext);
            entity.CustomEntityVersions.Add(version);

            return(entity);
        }
示例#2
0
        private async Task <Page> MapPage(AddPageCommand command, IExecutionContext executionContext)
        {
            // Create Page
            var page = new Page();

            page.PageTypeId    = (int)command.PageType;
            page.Locale        = GetLocale(command.LocaleId);
            page.PageDirectory = await  GetPageDirectoryAsync(command.PageDirectoryId);

            _entityAuditHelper.SetCreated(page, executionContext);
            _entityTagHelper.UpdateTags(page.PageTags, command.Tags, executionContext);

            var pageTemplate = await GetTemplateAsync(command);

            if (command.PageType == PageType.CustomEntityDetails)
            {
                var definition = await GetCustomEntityDefinitionAsync(pageTemplate.CustomEntityDefinitionCode, executionContext);

                var rule = await GetAndValidateRoutingRuleAsync(command, definition, executionContext);

                page.CustomEntityDefinitionCode = pageTemplate.CustomEntityDefinitionCode;
                page.UrlPath = rule.RouteFormat;
            }
            else
            {
                page.UrlPath = command.UrlPath;
            }

            var pageVersion = new PageVersion();

            pageVersion.Title = command.Title;
            pageVersion.ExcludeFromSitemap   = !command.ShowInSiteMap;
            pageVersion.MetaDescription      = command.MetaDescription ?? string.Empty;
            pageVersion.OpenGraphTitle       = command.OpenGraphTitle;
            pageVersion.OpenGraphDescription = command.OpenGraphDescription;
            pageVersion.OpenGraphImageId     = command.OpenGraphImageId;
            pageVersion.PageTemplate         = pageTemplate;
            pageVersion.DisplayVersion       = 1;

            if (command.Publish)
            {
                page.PublishStatusCode       = PublishStatusCode.Published;
                page.PublishDate             = command.PublishDate ?? executionContext.ExecutionDate;
                pageVersion.WorkFlowStatusId = (int)WorkFlowStatus.Published;
            }
            else
            {
                page.PublishStatusCode       = PublishStatusCode.Unpublished;
                page.PublishDate             = command.PublishDate;
                pageVersion.WorkFlowStatusId = (int)WorkFlowStatus.Draft;
            }
            _entityAuditHelper.SetCreated(pageVersion, executionContext);
            page.PageVersions.Add(pageVersion);

            return(page);
        }
        public async Task ExecuteAsync(AddDocumentAssetCommand command, IExecutionContext executionContext)
        {
            var documentAsset = new DocumentAsset();

            documentAsset.Title             = command.Title;
            documentAsset.Description       = command.Description ?? string.Empty;
            documentAsset.FileName          = FilePathHelper.CleanFileName(command.Title);
            documentAsset.VerificationToken = _randomStringGenerator.Generate(6);

            if (string.IsNullOrWhiteSpace(documentAsset.FileName))
            {
                throw ValidationErrorException.CreateWithProperties("Document title is empty or does not contain any safe file path characters.", nameof(command.Title));
            }

            _entityTagHelper.UpdateTags(documentAsset.DocumentAssetTags, command.Tags, executionContext);
            _entityAuditHelper.SetCreated(documentAsset, executionContext);
            documentAsset.FileUpdateDate = executionContext.ExecutionDate;

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                _dbContext.DocumentAssets.Add(documentAsset);

                await _documentAssetCommandHelper.SaveFile(command.File, documentAsset);

                command.OutputDocumentAssetId = documentAsset.DocumentAssetId;

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

                await scope.CompleteAsync();
            }
        }
示例#4
0
        private void AddRules <TAccessRule>(
            IEntityAccessRestrictable <TAccessRule> entity,
            IEnumerable <AddOrUpdateAccessRuleCommandBase> accessRules,
            Dictionary <int, Role> roles,
            Dictionary <string, IUserAreaDefinition> userAreas,
            IExecutionContext executionContext
            )
            where TAccessRule : IEntityAccessRule, new()
        {
            foreach (var addRuleCommand in accessRules.Where(r => !r.GetId().HasValue))
            {
                var dbRule = new TAccessRule();

                var userAreaDefinition = userAreas.GetOrDefault(addRuleCommand.UserAreaCode);
                EntityNotFoundException.ThrowIfNull(userAreaDefinition, addRuleCommand.RoleId);
                dbRule.UserAreaCode = userAreaDefinition.UserAreaCode;

                if (addRuleCommand.RoleId.HasValue)
                {
                    var role = roles.GetOrDefault(addRuleCommand.RoleId.Value);
                    EntityNotFoundException.ThrowIfNull(role, addRuleCommand.RoleId);
                    ValidateRoleIsInUserArea(userAreaDefinition, role);
                    dbRule.Role = role;
                }

                _entityAuditHelper.SetCreated(dbRule, executionContext);

                entity.AccessRules.Add(dbRule);
            }
        }
示例#5
0
        public async Task ExecuteAsync(AddPageDirectoryCommand command, IExecutionContext executionContext)
        {
            Normalize(command);
            var parentDirectory = await GetParentDirectoryAsync(command);

            await ValidateIsUniqueAsync(command, executionContext);

            var pageDirectory = new PageDirectory();

            pageDirectory.Name                = command.Name;
            pageDirectory.UrlPath             = command.UrlPath;
            pageDirectory.ParentPageDirectory = parentDirectory;
            _entityAuditHelper.SetCreated(pageDirectory, executionContext);

            _dbContext.PageDirectories.Add(pageDirectory);

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

                await _pageDirectoryStoredProcedures.UpdatePageDirectoryClosureAsync();

                scope.QueueCompletionTask(() => OnTransactionComplete(pageDirectory.PageDirectoryId));
                await scope.CompleteAsync();
            }

            command.OutputPageDirectoryId = pageDirectory.PageDirectoryId;
        }
示例#6
0
        public async Task ExecuteAsync(AddRedirectRuleCommand command, IExecutionContext executionContext)
        {
            var rule = new RewriteRule();

            rule.WriteFrom = command.WriteFrom;
            rule.WriteTo   = command.WriteTo;
            _entityAuditHelper.SetCreated(rule, executionContext);

            // TODO: Should be checking uniqueness?

            _dbContext.RewriteRules.Add(rule);
            await _dbContext.SaveChangesAsync();

            command.OutputRedirectRuleId = rule.RewriteRuleId;
        }
示例#7
0
        public async Task ExecuteAsync(AddPageDirectoryCommand command, IExecutionContext executionContext)
        {
            // Custom Validation
            await ValidateIsUniqueAsync(command, executionContext);

            var pageDirectory = new PageDirectory();

            pageDirectory.Name    = command.Name;
            pageDirectory.UrlPath = command.UrlPath;
            pageDirectory.ParentPageDirectoryId = command.ParentPageDirectoryId;
            _entityAuditHelper.SetCreated(pageDirectory, executionContext);

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

            _transactionScopeFactory.QueueCompletionTask(_dbContext, _cache.Clear);

            command.OutputPageDirectoryId = pageDirectory.PageDirectoryId;
        }
        public async Task ExecuteAsync(AddImageAssetCommand command, IExecutionContext executionContext)
        {
            ValidateFileType(command);

            var imageAsset = new ImageAsset();

            imageAsset.Title    = command.Title;
            imageAsset.FileName = SlugFormatter.ToSlug(command.Title);
            imageAsset.DefaultAnchorLocation = command.DefaultAnchorLocation;
            imageAsset.FileUpdateDate        = executionContext.ExecutionDate;
            imageAsset.FileNameOnDisk        = "file-not-saved";
            imageAsset.FileExtension         = "unknown";
            imageAsset.VerificationToken     = _randomStringGenerator.Generate(6);

            var fileStamp = AssetFileStampHelper.ToFileStamp(imageAsset.FileUpdateDate);

            _entityTagHelper.UpdateTags(imageAsset.ImageAssetTags, command.Tags, executionContext);
            _entityAuditHelper.SetCreated(imageAsset, executionContext);

            _dbContext.ImageAssets.Add(imageAsset);

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                // Save first to get an Id
                await _dbContext.SaveChangesAsync();

                // Update the disk filename
                imageAsset.FileNameOnDisk = $"{imageAsset.ImageAssetId}-{fileStamp}";

                await _imageAssetFileService.SaveAsync(command.File, imageAsset, nameof(command.File));

                command.OutputImageAssetId = imageAsset.ImageAssetId;

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

                await scope.CompleteAsync();
            }
        }
        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;
        }