Exemple #1
0
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(userId);

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

            if (Photo.IsMain)
            {
                return(BadRequest("لايمكن حذف الصورة الأساسية"));
            }
            _repo.Delete(Photo);


            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("فشل حذف الصورة"));
        }
        public async Task <IActionResult> DeleteMessage(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var message = await _repo.GetMessage(id);

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

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

            if (message.SenderDeleted && message.RecipientDeleted)
            {
                _repo.Delete(message);
            }

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

            throw new Exception("has error in delete message");
        }
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var UserFromRepo = await _repo.GetUser(userId, true);

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

            var Photo = await _repo.GetPhoto(id);

            if (Photo.IsMain)
            {
                return(BadRequest("هده الصوره الرئيسية"));
            }

            if (Photo.PublicId != null)
            {
                var deletionParams = new DeletionParams(Photo.PublicId);
                var result         = _cloudinary.Destroy(deletionParams);
                if (result.Result == "ok")
                {
                    _repo.Delete(Photo);
                }
            }
            if (Photo.PublicId == null)
            {
                _repo.Delete(Photo);
            }

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Error Delete Photo"));
        }