Exemplo n.º 1
0
        public async Task <IActionResult> DeletePhoto(int userId, int photoId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

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

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

            var photoFromRepo = await _repo.GetPhoto(photoId);

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

            // try delete the image if it has public id(exists in cloudinary)
            if (photoFromRepo.PublicId != null)
            {
                var deleteParams = new DeletionParams(photoFromRepo.PublicId);

                var result = _cloudinary.Destroy(deleteParams);

                if (result.Result == "ok")
                {
                    _repo.Delete(photoFromRepo);
                }
            }

            // does not exist in cloudinary, just remove the image records
            if (photoFromRepo.PublicId == null)
            {
                _repo.Delete(photoFromRepo);
            }

            // save all the changes in DB
            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            // nothing removed then return bad request
            return(BadRequest("Failed to delete the photo"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> DeleteMessage(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var messageFromRepo = await _repo.GetMessage(id);

            if (messageFromRepo.SenderId == userId)
            {
                messageFromRepo.SenderDeleted = true;
            }

            if (messageFromRepo.RecipientId == userId)
            {
                messageFromRepo.RecepientDeleted = true;
            }

            if (messageFromRepo.SenderDeleted && messageFromRepo.RecepientDeleted)
            {
                _repo.Delete(messageFromRepo);
            }

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception("Error deting the message");
        }
Exemplo n.º 3
0
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var user = await _repo.GetUser(userId);

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

            var photoFromRepo = await _repo.GetPhoto(id);

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("You can't delete the main photo"));
            }

            //If the hoto is stored in cloudinary
            if (photoFromRepo.PublicId != null)
            {
                //Delete photo from Cloudinary
                var deleteParams = new DeletionParams(photoFromRepo.PublicId);
                var result       = _cloudinary.Destroy(deleteParams);

                if (result.Result == "ok")
                {
                    //Delete from DB
                    _repo.Delete(photoFromRepo);
                }
            }

            if (photoFromRepo.PublicId == null)
            {
                //Delete from DB
                _repo.Delete(photoFromRepo);
            }

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to delete the photo"));
        }
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = await _socialRepository.GetUser(userId);

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

            var photoFromRepo = await _socialRepository.GetPhoto(id);

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

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

                var result = _cloudinary.Destroy(deleteParams);

                if (result.Result == "ok")
                {
                    _socialRepository.Delete(photoFromRepo);
                }
            }

            if (photoFromRepo.PublicId == null)
            {
                _socialRepository.Delete(photoFromRepo);
            }

            if (await _socialRepository.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to delete photo"));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            //checking to if user is Maining his own photo
            var user = await _repo.GetUser(userId);

            if (!user.Photos.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }
            var photoFromRepo = await _repo.GetPhoto(id);

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("Caan't delete the main photo, You"));
            }
            if (photoFromRepo.PublicId != null)
            {
                var deletionParams = new DeletionParams(photoFromRepo.PublicId);
                var result         = _cloudinary.Destroy(deletionParams);
                if (result.Result == "ok")
                {
                    _repo.Delete(photoFromRepo);
                }
            }
            if (photoFromRepo == null)
            {
                _repo.Delete(photoFromRepo);
            }


            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            else
            {
                return(BadRequest("Fialed to delte the photo"));
            }
        }