public async Task <IActionResult> GetUser(int id) { var user = await _repo.GetUserAsync(id); var userToReturn = _mapper.Map <UserForDetailedDto> (user); return(Ok(userToReturn)); }
public async Task <IActionResult> AddPhotoForUser(int userId, PhotoForCreationDto photoDto) { var user = await _repo.GetUserAsync(userId); if (user == null) { return(BadRequest("CouldNot find user")); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != user.Id) { return(Unauthorized()); } var file = photoDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var 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; var photo = _mapper.Map <Photo> (photoDto); photo.User = user; if (!user.Photos.Any(m => m.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); var photoToReturn = _mapper.Map <PhotoFromReturnDto> (photo); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }