public async Task AddPhotoForUser(PhotoForUserDto photoForUser) { var photo = _mapper.Map <Photo>(photoForUser); await _repository.AddAsync(photo); await _unitOfWork.CompleteAsync(); }
public async Task UpdatePhotoForUser(PhotoForUserDto photoForUser) { using (var scope = _serviceScopeFactory.CreateScope()) { var photoService = _photoServiceFactory(scope); await photoService.UpdatePhotoForUser(photoForUser); } }
public async Task UpdatePhotoForUser(PhotoForUserDto photoForUser) { var photo = await GetPhoto(photoForUser.UserId, photoForUser.FileId); photo = _mapper.Map(photoForUser, photo); _repository.Update(photo); await _unitOfWork.CompleteAsync(); }
public async Task <IActionResult> UploadPhotosForUser(int userid, [FromForm] PhotoForUserDto photoForUserDto) { if (userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userid); var file = photoForUserDto.File; var uploadResult = new ImageUploadResult(); if (file != null && file.Length > 0) { using (var filestream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, filestream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } else { return(BadRequest()); } photoForUserDto.Url = uploadResult.Uri.ToString(); photoForUserDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForUserDto); if (!userFromRepo.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoForReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { userid = userid, id = photo.Id }, photoForReturn)); } return(BadRequest($"Error uploading photo for user {userid}")); }
public async Task Handle_WhenCalled_AttemptToUpdatePhotoForUserAndRemoveItFromFilesStorageExpected() { // ARRANGE var filesStorageProvider = Substitute.For <IFilesStorageProvider>(); var fileDownloadResult = FileDownloadResult.Create(new MemoryStream(), "aa37acdc7bbf4260922a25066948db9e"); filesStorageProvider.DownloadAsync(fileDownloadResult.FileId, Arg.Any <string>()).Returns(fileDownloadResult); var photoStorageProvider = Substitute.For <IPhotoStorageProvider>(); var imageUploadResult = ImageUploadResult.Create(new Uri(@"https:\\veileye.com"), new Uri(@"https:\\veileye.com"), "jpg", DateTime.Now, "publicId", 1, "signature", "version"); photoStorageProvider.Upload(fileDownloadResult.FileId, fileDownloadResult.FileStream) .Returns(imageUploadResult); var photoService = Substitute.For <IPhotoServiceScoped>(); var userService = Substitute.For <IUserService>(); userService.GetUser(Arg.Any <int>()).Returns(new UserForDetailedDto()); var sut = new PhotoUploadedNotificationHandler(userService, photoStorageProvider, photoService, filesStorageProvider); var photoUploadedNotificationEvent = new PhotoUploadedNotificationEvent(fileDownloadResult.FileId, 99); var expectedPhotoForUser = new PhotoForUserDto { FileId = photoUploadedNotificationEvent.FileId, PublicId = imageUploadResult.PublicId, UserId = photoUploadedNotificationEvent.UserId, Url = imageUploadResult.Uri, Title = photoUploadedNotificationEvent.Title, Subtitle = photoUploadedNotificationEvent.Subtitle, Description = photoUploadedNotificationEvent.Description }; PhotoForUserDto actualPhotoForUser = null; photoService.When(x => x.UpdatePhotoForUser(Arg.Any <PhotoForUserDto>())) .Do(x => actualPhotoForUser = x.ArgAt <PhotoForUserDto>(0)); // ACT await sut.Handle(photoUploadedNotificationEvent, new CancellationToken()); // ASSERT actualPhotoForUser.Should().BeEquivalentTo(expectedPhotoForUser); await filesStorageProvider.Received().Remove(photoUploadedNotificationEvent.FileId); }
public async Task Handle(PhotoUploadedNotificationEvent notification, CancellationToken cancellationToken) { var user = await _userService.GetUser(notification.UserId); var downloadResult = await _filesStorageProvider.DownloadAsync(notification.FileId, user.PendingUploadPhotosFolderName); var imageUploadResult = await _photoStorageProvider.Upload(notification.FileId, downloadResult.FileStream); var photoForUser = new PhotoForUserDto { FileId = notification.FileId, PublicId = imageUploadResult.PublicId, UserId = notification.UserId, Url = imageUploadResult.Uri, Title = notification.Title, Subtitle = notification.Subtitle, Description = notification.Description }; await _photoService.UpdatePhotoForUser(photoForUser); await _filesStorageProvider.Remove(notification.FileId, user.PendingUploadPhotosFolderName); }
public async Task <IActionResult> UploadStreamFile() { if (!Helpers.MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { return(BadRequest($"Expected a multipart request, but got {Request.ContentType}")); } var file = await GetFileStream(HttpContext.Request.Body); if (!_fileFormatInspectorProvider.ValidateFileFormat(file.stream)) { return(BadRequest("Given file format is not supported")); } var fileMetadata = new PhotoForStreamUploadMetadataDto(); var bindingSuccessful = await TryUpdateModelAsync(fileMetadata, prefix : string.Empty, valueProvider : file.formValueProvider); if (!bindingSuccessful && !ModelState.IsValid) { return(BadRequest(ModelState)); } var user = GetUser(); var fileUploadResult = await _filesStorageProvider.UploadAsync(file.stream, fileMetadata, user.PendingUploadPhotosFolderName); var photoForUser = new PhotoForUserDto { FileId = fileUploadResult.FileId, UserId = user.Id, Url = fileUploadResult.Uri }; await _photoService.AddPhotoForUser(photoForUser); return(NoContent()); }