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();
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public async Task ExecuteAsync(UpdateImageAssetCommand command, IExecutionContext executionContext)
        {
            bool hasNewFile = command.File != null;

            if (hasNewFile)
            {
                _assetFileTypeValidator.ValidateAndThrow(command.File.FileName, command.File.MimeType, nameof(command.File));
            }

            var imageAsset = await _dbContext
                             .ImageAssets
                             .Include(a => a.ImageAssetTags)
                             .ThenInclude(a => a.Tag)
                             .FilterById(command.ImageAssetId)
                             .SingleOrDefaultAsync();

            imageAsset.Title    = command.Title;
            imageAsset.FileName = SlugFormatter.ToSlug(command.Title);
            imageAsset.DefaultAnchorLocation = command.DefaultAnchorLocation;

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

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                if (hasNewFile)
                {
                    var deleteOldFileCommand = new QueueAssetFileDeletionCommand()
                    {
                        EntityDefinitionCode = ImageAssetEntityDefinition.DefinitionCode,
                        FileNameOnDisk       = imageAsset.FileNameOnDisk,
                        FileExtension        = imageAsset.FileExtension
                    };

                    imageAsset.FileUpdateDate = executionContext.ExecutionDate;
                    var fileStamp = AssetFileStampHelper.ToFileStamp(imageAsset.FileUpdateDate);
                    imageAsset.FileNameOnDisk = $"{imageAsset.ImageAssetId}-{fileStamp}";

                    await _commandExecutor.ExecuteAsync(deleteOldFileCommand);

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

                await _dbContext.SaveChangesAsync();

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

                await scope.CompleteAsync();
            }
        }
        public async Task ExecuteAsync(UpdateDocumentAssetCommand command, IExecutionContext executionContext)
        {
            bool hasNewFile = command.File != null;

            var documentAsset = await _dbContext
                                .DocumentAssets
                                .Include(a => a.DocumentAssetTags)
                                .ThenInclude(a => a.Tag)
                                .FilterById(command.DocumentAssetId)
                                .SingleOrDefaultAsync();

            documentAsset.Title       = command.Title;
            documentAsset.Description = command.Description ?? string.Empty;
            documentAsset.FileName    = FilePathHelper.CleanFileName(command.Title);

            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.SetUpdated(documentAsset, executionContext);

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                if (hasNewFile)
                {
                    var deleteOldFileCommand = new QueueAssetFileDeletionCommand()
                    {
                        EntityDefinitionCode = DocumentAssetEntityDefinition.DefinitionCode,
                        FileNameOnDisk       = documentAsset.FileNameOnDisk,
                        FileExtension        = documentAsset.FileExtension
                    };

                    await _commandExecutor.ExecuteAsync(deleteOldFileCommand);

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

                    documentAsset.FileUpdateDate = executionContext.ExecutionDate;
                }

                await _dbContext.SaveChangesAsync();

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

                await scope.CompleteAsync();
            }
        }
        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();
            }
        }
 private void MapPage(UpdatePageCommand command, IExecutionContext executionContext, Page page)
 {
     _entityTagHelper.UpdateTags(page.PageTags, command.Tags, executionContext);
 }