Пример #1
0
        public async Task <IActionResult> DeleteMessage(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var messages = await _repo.GetMessage(id);

            if (messages.SenderId == userId)
            {
                messages.SenderDeleted = true;
            }
            if (messages.RecipientId == userId)
            {
                messages.RecipientDeleted = true;
            }
            if (messages.SenderDeleted && messages.RecipientDeleted)
            {
                _repo.Delete(messages);
            }
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception("Error deleting Message");
        }
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var photoFromRepo = await _repo.GetPhoto(id);

            if (photoFromRepo == null)
            {
                return(NotFound());
            }

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

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

                var result = _cloudinary.Destroy(deleteParams);

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

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

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

            return(BadRequest("Failed to delete the photo"));
        }
Пример #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 _dating.GetUser(userId);

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

            var PhotoFromRepo = await _dating.GetPhoto(id);

            if (PhotoFromRepo.IsMain)
            {
                return(BadRequest("you can not delete the main photo"));
            }

            if (PhotoFromRepo.PublicID != null)
            {
                var deletionparam = new DeletionParams(PhotoFromRepo.PublicID);
                var result        = _cloudinary.Destroy(deletionparam);
                if (result.Result == "ok")
                {
                    _dating.Delete(PhotoFromRepo);
                }
            }
            if (PhotoFromRepo.PublicID == null)
            {
                _dating.Delete(PhotoFromRepo);
            }

            if (await _dating.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest());
        }