public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDTO photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await this._repo.GetUser(userId); var file = photoForCreationDto.File; var uploadedResult = 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") }; uploadedResult = this._cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadedResult.Uri.ToString(); photoForCreationDto.PublicId = uploadedResult.PublicId; var photo = this._mapper.Map <Photo>(photoForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = this._mapper.Map <PhotoForReturnDTO>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo!")); }
public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await this._repo.GetUser(id); this._mapper.Map(userForUpdateDto, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} failed in save"); }