示例#1
0
        public async Task <Response <int> > Handle(UpdateContentCommand command, CancellationToken cancellationToken)
        {
            Item item = await _storageRepository.GetByIdAsync(command.Id);

            if (item == null)
            {
                throw new ApiException($"Content Not Found.");
            }
            else
            {
                item.Name           = command.Name ?? Path.GetFileNameWithoutExtension(command.File.FileName);
                item.ContentType    = command.File.GetContentType();
                item.LastModifiedBy = _httpContext.HttpContext.User.Identity.Name;
                item.Description    = command.Description;
                item.Size           = command.File.Length;
                item.VerifiedHash   = await command.File
                                      .CalculateMD5FileHashAsync(cancellationToken);

                item.Url = await _storageFileSystemProvider
                           .StoreAsync(command.File, cancellationToken, overwrite : true).ConfigureAwait(false);

                if (!string.IsNullOrWhiteSpace(item.Url))
                {
                    await _storageRepository.UpdateAsync(item);
                }

                _logger.LogInformation("storage item {Name} successfully updated in {Url}", item.Name, item.Url);
                return(new Response <int>
                {
                    Data = item.Id,
                    Succeeded = true,
                    Message = $"storage item {item.Name} successfully updated."
                });
            }
        }
示例#2
0
        public async Task <Response <ItemDto> > Handle(GetContentByIdQuery query, CancellationToken cancellationToken)
        {
            Item content = await _storageRepository.GetByIdAsync(query.Id);

            if (content == null)
            {
                throw new ApiException($"Content Not Found.");
            }
            ItemDto contentDto = _mapper.Map <ItemDto>(content);
            // Raising newlly content created Event ...
            await _mediator.Publish(new ContentRequestedEvent(DateTime.Now, contentDto.Id, _httpContext.HttpContext.User.Identity.Name), cancellationToken);

            return(new Response <ItemDto>(contentDto));
        }
        public async Task <Response <int> > Handle(DeleteContentByIdCommand command, CancellationToken cancellationToken)
        {
            Item content = await _storageRepository.GetByIdAsync(command.Id);

            if (content is null)
            {
                throw new ApiException(_configuration["Storage:Messages:NotFound"], command.Id);
                //return null;
            }
            Response <int> storageResult = _storageFileSystemProvider.DeleteAsync(content.Url, cancellationToken);

            if (storageResult.Succeeded is true)
            {
                await _storageRepository.DeleteAsync(content);
            }

            storageResult.Data = command.Id;
            return(storageResult);
        }