Ejemplo n.º 1
0
        public async Task <IActionResult> GetPhoto(int id)
        {
            Photo photoFromRepo = await _repo.GetPhoto(id);

            PhotoForReturnDto photo = _mapper.Map <PhotoForReturnDto>(photoFromRepo);

            return(Ok(photo));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetPhoto(int id)
        {
            Photo photo = await _photoRepository.GetPhotoAsync(id);

            PhotoForReturnDto ret = _mapper.Map <PhotoForReturnDto>(photo);

            return(Ok(ret));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotosForCreationDto photosForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            User userFromRepo = await _repo.GetUser(userId);

            var file = photosForCreationDto.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);
                }
            }

            photosForCreationDto.Url      = uploadResult.Uri.ToString();
            photosForCreationDto.PublicID = uploadResult.PublicId;
            Photo photo = _mapper.Map <Photo>(photosForCreationDto);

            if (!userFromRepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }

            userFromRepo.Photos.Add(photo);


            if (await _repo.SaveAll())
            {
                PhotoForReturnDto photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new {
                    id = photo.Id
                }, photoToReturn));
            }

            return(BadRequest("Could not add the photo"));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            //1. Make sure that user trying to update the profile is matching the profile their trying to update
            Claim claim = User.FindFirst(ClaimTypes.NameIdentifier);

            if (claim == null)
            {
                return(UnprocessableEntity("Unable to find NameIdentifier on Claim"));
            }

            if (userId != int.Parse(claim.Value))
            {
                return(Unauthorized());
            }

            if (photoForCreationDto.File == null)
            {
                return(BadRequest("No picture uploaded"));
            }

            Photo photo = await _photosHosting.StorePhoto(photoForCreationDto);

            User user = await _userRepository.GetUserPhotoAsync(userId);

            if (!user.Photos.Any())
            {
                photo.IsMain = true;
            }

            user.Photos.Add(photo);


            if (await _datingRepository.SaveAllAsync())
            {
                PhotoForReturnDto ret = _mapper.Map <PhotoForReturnDto>(photo);

                //Because HTTPPost; supposed to return CreatedAtRoute() so we return:
                //a) route to location of object we created
                //b) the id of the photo we return
                //c) the actual photo object
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, ret));
            }

            return(BadRequest("Could not add the photo"));
        }