Example #1
0
        public async Task ExecuteAsync(UpdateImageAssetCommand command, IExecutionContext executionContext)
        {
            bool hasNewFile = command.File != null;

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

            imageAsset.FileDescription       = 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())
            {
                if (hasNewFile)
                {
                    await _imageAssetCommandHelper.SaveFile(command.File, imageAsset);
                }

                await _dbContext.SaveChangesAsync();

                scope.Complete();
            }

            if (hasNewFile)
            {
                _imageAssetFileCache.Clear(imageAsset.ImageAssetId);
            }
            _imageAssetCache.Clear(imageAsset.ImageAssetId);
        }
        public async Task ExecuteAsync(AddImageAssetCommand command, IExecutionContext executionContext)
        {
            var imageAsset = new ImageAsset();

            imageAsset.FileDescription       = command.Title;
            imageAsset.FileName              = SlugFormatter.ToSlug(command.Title);
            imageAsset.DefaultAnchorLocation = command.DefaultAnchorLocation;
            _entityTagHelper.UpdateTags(imageAsset.ImageAssetTags, command.Tags, executionContext);
            _entityAuditHelper.SetCreated(imageAsset, executionContext);

            using (var scope = _transactionScopeFactory.Create())
            {
                // if adding, save this up front
                _dbContext.ImageAssets.Add(imageAsset);

                await _imageAssetCommandHelper.SaveFile(command.File, imageAsset);

                command.OutputImageAssetId = imageAsset.ImageAssetId;
                scope.Complete();
            }
        }