コード例 #1
0
        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.RecipientId == userId)
            {
                message.RecipientDeleted = true;
            }

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

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

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

            throw new Exception("Il y à une erreur lors de suppression message");
        }
コード例 #2
0
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            //--> Vérifier id de user avec id dans token
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            //--> Récupérer les donner de user
            var userFromRepo = await _repo.GetUser(userId, true);

            //--> Vérifier que photo appartient aux photos user
            if (!userFromRepo.Photos.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }

            var Photo = await _repo.GetPhoto(id);

            if (Photo.IsMain)
            {
                return(BadRequest("On ne peut pas supprimer photo principale"));
            }

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

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

            return(BadRequest("Échec de la suppression de l'image"));
        }