public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { // verify that the user is updating its own profile. if (IsUserAuthorizedAndSelf(userId) == false) { return(Unauthorized()); } var userFromRepo = await repo.GetUser(userId); var file = photoForCreationDto.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); } } else { return(BadRequest()); } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = mapper.Map <Photo>(photoForCreationDto); //verify is the user don't have a 'main photo' to set the current upload as main. if (!userFromRepo.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await repo.SaveAll()) { var photoToReturn = mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized("User not authorized")); } var userFromRepo = await _repo.GetUser(userId); var file = photoForCreationDto.File; var uploadResults = 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") }; uploadResults = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResults.Url.ToString(); photoForCreationDto.PublicId = uploadResults.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add photo")); }
public async Task <IActionResult> GetUsers([FromQuery] PageRequestUserParams pageRequestPrms) { var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var userFromRepo = await repo.GetUser(currentUserId); pageRequestPrms.UserId = currentUserId; if (string.IsNullOrEmpty(pageRequestPrms.Gender)) { pageRequestPrms.Gender = (userFromRepo.Gender == "male" ? "female":"male"); } var users = await repo.GetUsers(pageRequestPrms); var usersToReturn = mapper.Map <IEnumerable <UserForListDto> >(users); Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages); return(Ok(usersToReturn)); }