public async Task <IActionResult> DeleteUserProfilePhoto(int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User user = await _repo.GetUser(userId); Photo photo = user.ProfilePhoto; if (photo == null) { return(BadRequest("Photo doesn't exist")); } _repo.Delete(photo); if (await _repo.SaveAllAsync()) { PhotoForReturnDto photoForReturnDto = _mapper.Map <PhotoForReturnDto>(photo); return(Ok(photoForReturnDto)); } return(BadRequest("Fail in removing photo")); }
public async Task <IActionResult> AddPhotoToProfile(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User user = await _repo.GetUser(userId); if (user.ProfilePhoto != null) { Photo trashPhoto = await _repo.GetPhotoById(user.ProfilePhoto.Id); _repo.Delete(trashPhoto); } photoForCreationDto.Width = 500; photoForCreationDto.Height = 500; photoForCreationDto.UserId = userId; photoForCreationDto.IsMain = true; Photo photo = UploadPhotoToCloudinary(photoForCreationDto); _repo.Add(photo); if (await _repo.SaveAllAsync()) { PhotoForReturnDto photoForReturnDto = _mapper.Map <PhotoForReturnDto>(photo); return(Ok(photoForReturnDto)); } return(BadRequest("Adding photo to user's profile could not be possible")); }
public async Task <IActionResult> SetMainPhoto(int id, int hotelId, [FromBody] PhotoForReturnDto photoDto) { var photoFromRepo = await _hotelRepo.GetPhoto(id); if (photoFromRepo == null) { return(NotFound()); } if (photoFromRepo.IsMain) { return(BadRequest("This is already the main photo")); } var currentMainPhoto = await _hotelRepo.GetMainPhotoForType(hotelId, photoDto.PhotoType, photoDto.PhotoTypeId); if (currentMainPhoto != null) { currentMainPhoto.IsMain = false; } photoFromRepo.IsMain = true; if (await _unitOfWork.CompleteAsync()) { return(NoContent()); } return(BadRequest("Could not set photo to main")); }
public async Task GetPhoto_InvalidIdPassed_ReturnsOkResult() { var photo = new Photo() { Id = 1 }; var photoToReturn = new PhotoForReturnDto() { Id = 1 }; var mapperMock = new Mock <IMapper>(); mapperMock.Setup(m => m.Map <PhotoForReturnDto>(photo)).Returns(photoToReturn); var cloudinaryConfigMock = Options.Create <CloudinarySettings>(new CloudinarySettings { ApiKey = "test", ApiSecret = "test", CloudName = "test" }); var repositoryMock = new Mock <IRepositoryWrapper>(); repositoryMock.Setup(r => r.PhotoRepository.GetPhotoAsync(It.IsAny <int>())).ReturnsAsync(photo); var controllerMock = new PhotosController(repositoryMock.Object, mapperMock.Object, cloudinaryConfigMock); var result = await controllerMock.GetPhoto(2); Assert.IsType <OkObjectResult>(result); }
public async Task <IActionResult> GetPhotoById(int photoId) { Photo photo = await _repo.GetPhotoById(photoId); PhotoForReturnDto photoForReturnDto = _mapper.Map <PhotoForReturnDto>(photo); return(Ok(photoForReturnDto)); }
public async Task <IActionResult> GetPhoto(int id) { Photo photoFromRepo = await _datingRepository.GetPhoto(id); PhotoForReturnDto photo = _mapper.Map <PhotoForReturnDto>(photoFromRepo); return(Ok(photo)); }
public static PhotoForReturnDto MapPhotoToPhotoForReturnDto(Photo photo) { var photoForReturnDto = new PhotoForReturnDto { DateAdded = photo.DateAdded, Description = photo.Description, Url = photo.Url, IsMain = photo.IsMain, PublicId = photo.PublicId, Id = photo.Id }; return(photoForReturnDto); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User userFromRepo = await _repo.GetUser(userId); IFormFile file = photoForCreationDto.File; ImageUploadResult uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { ImageUploadParams uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; Photo photo = _mapper.Map <Photo>(photoForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { PhotoForReturnDto photoForReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoForReturn)); } return(BadRequest("Could not add the photo.")); }