public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repo.GetById(userId); var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { uploadResult = _Cloudinary.Upload(new ImageUploadParams { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }); } } photoForCreationDto.Url = uploadResult.Url.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; if (!user.Photos.Any()) { photoForCreationDto.IsMain = true; } var addedPhoto = _mapper.Map <Photo>(photoForCreationDto); user.Photos.Add(addedPhoto); if (await _repo.SaveAll()) { var returnedPhoto = _mapper.Map <PhotoForDetailsDto>(addedPhoto); return(CreatedAtRoute("GetUserPhoto", new { userId = userId, photoId = returnedPhoto.Id }, returnedPhoto)); } return(BadRequest("Could Not Save Photo!")); }
public async Task <IActionResult> GetUserById(int id) { return(Ok(_mapper.Map <UserForDetailsDto>(await _userRepository.GetById(id)))); }