public async Task <IActionResult> GetPhoto(Guid id) { Photo photo = await _repository.GetPhoto(id); PhotoReturnDto photoReturnDto = _mapper.Map <PhotoReturnDto>(photo); return(Ok(photoReturnDto)); }
public async Task <IActionResult> AddPhotoForUser(Guid userId, [FromForm] PhotoCreationDto photoCreationDto) { if (userId != Guid.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User user = await _repository.GetUser(userId, true); IFormFile file = photoCreationDto.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); } } photoCreationDto.Url = uploadResult.Uri.ToString(); photoCreationDto.PublicId = uploadResult.PublicId; Photo photo = _mapper.Map <Photo>(photoCreationDto); if (!user.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); if (await _repository.SaveAll()) { PhotoReturnDto photoReutrnDto = _mapper.Map <PhotoReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoReutrnDto)); } return(BadRequest("Could not add the photo")); }