예제 #1
0
        public async Task <IActionResult> SetUserMainPhoto(int userId, int photoId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            Models.User userFromRepo = await _datingRepo.GetUser(userId);

            if (!userFromRepo.Photos.Any(photo => photo.Id == photoId))
            {
                return(Unauthorized());
            }
            Models.Photo photoFromRepo = await _datingRepo.GetPhoto(photoId);

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("This photo already the main photo"));
            }
            Models.Photo userMainPhoto = await _datingRepo.GetUserMainPhoto(userId);

            userMainPhoto.IsMain = false;
            photoFromRepo.IsMain = true;

            if (await _datingRepo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Error occurred while setting main photo"));
        }
예제 #2
0
        public async Task <IActionResult> DeleteUserImage(int userId, int photoId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            Models.User userFromRepo = await _datingRepo.GetUser(userId);

            if (!userFromRepo.Photos.Any(p => p.Id == photoId))
            {
                return(Unauthorized());
            }

            Models.Photo photoFromRepo = await _datingRepo.GetPhoto(photoId);

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("You cannot delete your main photo"));
            }

            if (photoFromRepo.PublicId != null)
            {
                DeletionParams deleteParams = new DeletionParams(photoFromRepo.PublicId);
                var            result       = _cloudinary.Destroy(deleteParams);

                if (result.Result.ToLower() == "ok")
                {
                    _datingRepo.Delete(photoFromRepo);
                }
            }
            else
            {
                _datingRepo.Delete(photoFromRepo);
            }
            if (await _datingRepo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Error occurred while deleting photo"));
        }