public async Task <IActionResult> AddPhotoForUser(int UserId, [FromForm] PhotoCreationDto ObjPhotoCreationDto)
        {
            List <UsersDto> listUserDto  = new List <UsersDto>();
            UsersDto        objUserDto   = new UsersDto();
            PhotoDto        objPhotoDto  = new PhotoDto();
            var             file         = ObjPhotoCreationDto.File;
            var             uploadResult = new ImageUploadResult();


            if (UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            listUserDto = await objDattingDAL.GetUserList();

            objUserDto = listUserDto.FirstOrDefault(U => U.id == UserId);

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadsParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };

                    uploadResult = _cloudinary.Upload(uploadsParams);
                }
            }

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

            objPhotoDto.PublicId    = ObjPhotoCreationDto.PublicId;
            objPhotoDto.IsMain      = false;
            objPhotoDto.Description = ObjPhotoCreationDto.Description;
            objPhotoDto.AddDate     = ObjPhotoCreationDto.DateAdded;
            objPhotoDto.url         = ObjPhotoCreationDto.Url;
            objPhotoDto.FkUserId    = UserId;

            if (!objUserDto.Photos.Any(x => x.IsMain))
            {
                objPhotoDto.IsMain = true;
            }
            PhotoDto objPhoto = new PhotoDto();

            objPhoto = await objPhotoDAL.InsertPhoto(objPhotoDto);

            if (objPhoto != null && objPhoto.Id > 0)
            {
                return(Ok(objPhoto));
            }

            return(BadRequest("Could not add the photo"));
        }
Exemple #2
0
        public async Task <IActionResult> AddPhoto(int userId, PhotoCreationDto photoDto)
        {
            User user = await this._connectRepository.GetUser(userId);

            if (user == null)
            {
                return(BadRequest("could not find user"));
            }

            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != user.Id)
            {
                return(Unauthorized());
            }

            IFormFile         file         = photoDto.File;
            ImageUploadResult uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (Stream stream = file.OpenReadStream())
                {
                    ImageUploadParams uploadPrams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation()
                                         .Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResult = this._cloudinary.Upload(uploadPrams);
                }
            }
            photoDto.Url      = uploadResult.Uri.ToString();
            photoDto.PublicId = uploadResult.PublicId;

            Photo photo = this._mapper.Map <Photo>(photoDto);

            photo.User = user;

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

            user.Photos.Add(photo);

            if (await this._connectRepository.SaveAll())
            {
                var returnPhoto = this._mapper.Map <PhotoFetchDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, returnPhoto));
            }
            else
            {
                return(BadRequest());
            }
        }
Exemple #3
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId, true);

            var file = photoForCreationDto.File;

            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadPramas = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResult = _cloudinary.Upload(uploadPramas);
                }
            }

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

            //var photo = new Photo
            //{
            //    DateAdded = DateTime.Now,
            //    PublicId = uploadResult.PublicId,
            //    Url = uploadResult.Uri.ToString(),


            //};

            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 { id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Could not add the photo"));
        }
Exemple #4
0
        public async Task <IActionResult> AddPhotoForUser(Guid userId, [FromForm] PhotoCreationDto photoCreationDto)
        {
            if (userId != Guid.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            User user = await _repository.GetUser(userId, true);

            IFormFile file = photoCreationDto.File;

            ImageUploadResult uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (Stream stream = file.OpenReadStream())
                {
                    ImageUploadParams uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500)
                                         .Height(500)
                                         .Crop("fill")
                                         .Gravity("face")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

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

            Photo photo = _mapper.Map <Photo>(photoCreationDto);

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

            user.Photos.Add(photo);

            if (await _repository.SaveAll())
            {
                PhotoReturnDto photoReutrnDto = _mapper.Map <PhotoReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoReutrnDto));
            }

            return(BadRequest("Could not add the photo"));
        }
Exemple #5
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoCreationDto photoCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var dbUser = await _repository.GetUser(userId, true);

            var file = photoCreationDto.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(600).Height(600).Crop("fill").Gravity("auto")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoCreationDto.Url      = uploadResult.SecureUri.ToString();
            photoCreationDto.PublicId = uploadResult.PublicId;

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

            if (!dbUser.Photos.Any(p => p.IsProfilePhoto))
            {
                photo.IsProfilePhoto = true;
            }

            dbUser.Photos.Add(photo);

            if (await _repository.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoDetailsDto>(photo);

                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Error with adding the photo."));
        }
Exemple #6
0
        public async Task <IActionResult> UploadUserPhoto(PhotoCreationDto photoDto, int UserId)
        {
            var userFromDb = await _repo.GetUser(UserId);

            if (userFromDb == null)
            {
                return(BadRequest());
            }
            var userToCheck = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userFromDb.Id != userToCheck)
            {
                return(Unauthorized());
            }
            var file         = photoDto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.FileName, stream),
                        Transformation = new Transformation().Height(500).Width(500).Crop("fill").Gravity("face")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoDto.Url      = uploadResult.Uri.ToString();
            photoDto.PublicId = uploadResult.PublicId;
            var photo = _mapper.Map <Photo>(photoDto);

            photo.User = userFromDb;
            if (!userFromDb.Photos.Any(x => x.IsMain))
            {
                photo.IsMain = true;
            }
            userFromDb.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoToReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }
            return(BadRequest("Could not add the photo"));
        }
        private void UploadPhotoToCloudinary(PhotoCreationDto photoCreationDto)
        {
            ImageUploadResult result = new ImageUploadResult();

            if (photoCreationDto.File.Length > 0)
            {
                using (Stream stream = photoCreationDto.File.OpenReadStream())
                {
                    ImageUploadParams uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(photoCreationDto.File.Name, stream),
                        Transformation = new Transformation().Width(250).Height(250).Gravity("faces").Crop("fill")
                    };
                    result = _cloudinary.Upload(uploadParams);
                }
            }

            photoCreationDto.Url      = result.Url.ToString();
            photoCreationDto.PublicId = result.PublicId;
        }
Exemple #8
0
        public IActionResult AddPhoto(int cityId, [FromForm] PhotoCreationDto photoCreationDto)
        {
            var city = _context.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(BadRequest("Could not find the city"));
            }

            var file         = photoCreationDto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams
                    {
                        File = new FileDescription(file.Name, stream)
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoCreationDto.Url      = uploadResult.Url.ToString();
            photoCreationDto.PublicId = uploadResult.PublicId;

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

            photo.Description = photoCreationDto.Description;
            photo.City        = city;
            if (!city.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }

            city.Photos.Add(photo);
            _context.SaveChanges();
            // var photoReturn = _mapper.Map<PhotoReturnDto>(photo);
            return(Ok("lorem"));
        }
        public async Task <IActionResult> AddPhotoForUser(string username, [FromForm] PhotoCreationDto photoCreationDto)
        {
            string currentUser = User.FindFirstValue(ClaimTypes.Name);

            if (username != currentUser)
            {
                return(Unauthorized());
            }

            if (photoCreationDto.File == null)
            {
                return(BadRequest("No photo file received for request."));
            }

            User user = await _userRepository.GetUser(username);

            UploadPhotoToCloudinary(photoCreationDto);

            Photo photo = _mapper.Map <Photo>(photoCreationDto);

            bool hasMainPhoto = user.Photos.Any(p => p.IsMain);

            if (!hasMainPhoto)
            {
                photo.IsMain = true;
            }

            user.Photos.Add(photo);

            int numChanges = await _userRepository.Save();

            if (numChanges == 0)
            {
                throw new System.Exception($"Could not save your photos!");
            }


            return(CreatedAtRoute("GetPhoto", new { id = photo.Id, username = username }, _mapper.Map <PhotoCloudinaryResponseDto>(photo)));
        }