public async Task <CommandResult> Handle(SetAvatarImageCommand request, CancellationToken cancellationToken) { StorageValidatorResult storageValidator = new ImageStorageValidator().Validate(request.AvatarImage); if (!storageValidator.Success) { return(FailureDueToFileValidationFailure(storageValidator.Errors.ToList())); } Profile profile = await _profileRepository.GetByIdAsync(request.ProfileId); if (!FoundValidProfile(profile)) { return(FailureDueToProfileNotFound()); } Image image = null; using (MemoryStream stream = new MemoryStream()) { string containerName = _containerOptions.Value.ProfileImageContainerName; await request.AvatarImage.CopyToAsync(stream); StorageResult result = await _storageService.UploadFileToStorageAsync(containerName, stream, Guid.NewGuid().ToString()); if (!result.Success) { return(FailureDueToUploadFailure()); } image = new Image(result.FileUrl, result.FileName); } profile.SetAvatarImage(image); await _profileRepository.UpdateAsync(profile); return(await CommitAndPublishDefaultAsync()); }
public async Task <IActionResult> SetAvatarImageAsync(Guid id, [FromBody] SetAvatarImageCommand command) { command.ProfileId = id; return(await CreateCommandResponse(command)); }