public async Task <IActionResult> GetPhoto(int id) { Photo photoFromRepo = await _repo.GetPhoto(id); PhotosForReturnDto photo = _mapper.Map <PhotosForReturnDto>(photoFromRepo); return(Ok(photo)); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photosForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User userFromRepo = await _repo.GetUser(userId); IFormFile file = photosForCreationDto.File; ImageUploadResult uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (Stream 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); } } photosForCreationDto.Url = uploadResult.Uri.ToString(); photosForCreationDto.PublicId = uploadResult.PublicId; Photo photo = _mapper.Map <Photo>(photosForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } photo.IsApproved = false; userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { PhotosForReturnDto photosForReturn = _mapper.Map <PhotosForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photosForReturn)); } return(BadRequest("Could not add the photo")); }