public async Task <IActionResult> AddPhoto(int id, [FromForm] PhotoForCreationDto photoForCreationDto) { if (checkUser(id)) { return(Unauthorized()); } var userFromRepo = await _usersRepository.GetUser(id); var uploadResult = UploadFileToCloudinary(photoForCreationDto.File, userFromRepo.Id); photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); photo.UserId = userFromRepo.Id; photo.IsApproved = false; if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } await _photosRepository.Add <Photo>(photo); if (await _photosRepository.SaveAll()) { return(CreatedAtRoute("GetPhoto", new { id, photoId = photo.Id }, _mapper.Map <PhotoForReturnDto>(photo))); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> ModeratePhoto(int photoId, PhotoForModeration photo) { var photoFromRepo = await this._photoRepo.GetPhoto(photoId); if (photoFromRepo == null) { return(BadRequest("The photo doesn't exist")); } if (photo.Moderation) { photoFromRepo.IsApproved = photo.Moderation; } else { if (photoFromRepo.PublicId != null) { var result = DeleteFileInCloudinary(photoFromRepo.PublicId); if (result) { this._photoRepo.Delete(photoFromRepo); } } else { this._photoRepo.Delete(photoFromRepo); } } if (await _photoRepo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to moderate the photo")); }