public async Task <IActionResult> GetPhoto(int photoId) { Photo photo = await _repository.GetPhoto(photoId); PhotoForDetailDto photoDto = _mapper.Map <PhotoForDetailDto>(photo); return(Ok(photoDto)); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoFormDto photoDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User currentUser = await _repository.GetUser(userId); IFormFile file = photoDto.File; ImageUploadResult uploadResult = new ImageUploadResult(); if (file != null && file.Length > 0) { using (var stream = file.OpenReadStream()) // using allows us to dispose of the file in memory when done { 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); } photoDto.Url = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; Photo photoToSave = _mapper.Map <Photo>(photoDto); if (!currentUser.Photos.Any(photo => photo.IsMain)) { photoToSave.IsMain = true; } currentUser.Photos.Add(photoToSave); if (await _repository.SaveAll()) { PhotoForDetailDto photoForDetail = _mapper.Map <PhotoForDetailDto>(photoToSave); return(CreatedAtRoute("GetPhoto", new { photoId = photoForDetail.Id }, photoForDetail)); } } return(BadRequest("Photo could not be saved")); }