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

            var userFromRepo = await _repo.GetUser(userId);

            var file = photosForCreationDTO.File;

            //Store result returned from cloudinary
            var uploadResults = new ImageUploadResult();

            //And if the file's length > 0 then will read this file into memory.
            //Because this is going to be a file stream we'll use using so that we can dispose of what the file inside memory
            //once we've completed this method.
            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(file.Name, stream),
                        //Crop the image arround the user's face
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResults = _cloudinary.Upload(uploadParams);
                }
            }
            photosForCreationDTO.Url      = uploadResults.Uri.ToString();
            photosForCreationDTO.PublicId = uploadResults.PublicId;

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

            //If the user doesn't have a main photo set isMain to true
            if (!userFromRepo.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }

            //Save photo
            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotosForReturnDTO>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); // Http post should return created at route (201)
            }

            return(BadRequest("Could not add the photo"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotosForCreationDTO photoForCreationDTO)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await repository.GetUserWithUnapprovedPhotos(userId);

            // var values = await repository.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);

            photo.IsApproved = false;
            photo.IsMain     = false;

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

            userFromRepo.Photos.Add(photo);

            if (await repository.SaveAll())
            {
                var photoForReturn = mapper.Map <PhotosForReturnDTO>(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoForReturn));
                // return CreatedAtRoute("GetPhoto", new {userId = userId, photo.Id}, photoForReturn );
            }

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