Esempio n. 1
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] Dtos.PhotoForCreationDto photoForCreationDto)
        {
            // check if the user that is logged in is attempting to update his profile via HttpPut(id)
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repository.GetUser(userId);

            var file = photoForCreationDto.File;
            // get the user from the repo
            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);
                }
            }

            photoForCreationDto.Url      = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;

            // perform the mapping (shallow copy) to the Photo object
            var photo = _mapper.Map <Photo>(photoForCreationDto);

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

            userFromRepo.Photos.Add(photo);

            // persist to the DB
            if (await _repository.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);

                // return a route specified by the HttpGet GetPhoto method
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Could not add the photo"));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddPhotoForUser(int userId,
                                                          [FromForm] Dtos.PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _reppy.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);
                }
            }

            photoForCreationDto.Url      = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;

            var photo = _mapper.Map <Photo>(photoForCreationDto);

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

            userFromRepo.Photos.Add(photo);

            if (await _reppy.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
                // later work on returning a CreatedAtRoute response; object that produces a http status repsonse (201)
                // has three overloads
                // alternative is return Ok()
            }

            return(BadRequest("Couldn't add the picture."));
        }