public async Task <IActionResult> DeleteMessage(int id, string userId)
        {
            if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value)
            {
                return(Unauthorized());
            }
            var messageFromRepo = await _rebo.GetMessage(id);

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

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

            if (messageFromRepo.SenderDeleted && messageFromRepo.RecipientDeleted)
            {
                _rebo.Delete(messageFromRepo);
            }

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

            throw new Exception("Error Deleting the Message");
        }
Exemple #2
0
        public async Task <IActionResult> DeletePhoto(string userId, int id)
        {
            if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value)
            {
                return(Unauthorized());
            }
            //get user data
            var userFromRepo = await _repo.GetUser(userId);

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

            var photoFromRepo = await _repo.GetPhoto(id);

            if (photoFromRepo.IsMain)
            {
                return(BadRequest(" Cannot Delete The Main Photo ! "));
            }

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

                var result = _cloudinary.Destroy(deletParam);
                if (result.Result == "ok")
                {
                    _repo.Delete(photoFromRepo);
                }
            }
            if (photoFromRepo.PublicId == null)
            {
                _repo.Delete(photoFromRepo);
            }

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

            return(BadRequest("Fail To Delete a Photo Please Try Agin ! "));
        }