public async Task <IEnumerable <ImageViewModel> > Handle(UploadImagesCommand message, CancellationToken cancellationToken) { var viewModel = new List <ImageViewModel>(); foreach (var file in message.Files) { var image = await CreateImage(file); await SaveImage(image); var viewModelItem = CreateViewModel(image); viewModel.Add(viewModelItem); } return(viewModel); }
public async Task <IActionResult> UploadImages(IList <IFormFile> formFiles) { var uploadImagesCommand = new UploadImagesCommand(); foreach (var formFile in formFiles) { var file = new FileDto { Content = formFile.OpenReadStream(), Name = formFile.FileName, ContentType = formFile.ContentType, }; uploadImagesCommand.Files.Add(file); } var response = await Mediator.Send(uploadImagesCommand); return(Ok(response)); }
public async Task ShouldUploadImages() { var imageFactory = TestContext.CreateImageService(); var handlerContext = TestContext.CreateHandlerContext <IEnumerable <ImageViewModel> >(RequestDbContext, CreateMapper()); var handler = new UploadImagesCommandHandler(handlerContext, imageFactory); var file1 = TestContext.CreateFileMock().Object; var file2 = TestContext.CreateFileMock().Object; var files = new List <IFormFile>() { file1, file2 }; var message = new UploadImagesCommand() { Files = files }; var result = await handler.Handle(message, CancellationToken.None); Assert.NotNull(result); Assert.Equal(2, RequestDbContext.Images.Count()); }