コード例 #1
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 == id)
            {
                messageFromRepo.RecipientDeleted = true;
            }

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

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

            throw new System.Exception("Error deleting the message");
        }
コード例 #2
0
        public async Task <IActionResult> DeletePhoto(int carmodelId, int id)
        {
            var carModelFromRepo = await _repo.GetCarModel(carmodelId);

            // check the Id is the photo of that user(carmodel)
            if (!carModelFromRepo.Photos.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }

            var photoFromRepo = await _repo.GetPhoto(id);

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

            // handle the photo in cloudinary
            if (photoFromRepo.PublicId != null)
            {
                var deleteParams = new DeletionParams(photoFromRepo.PublicId);

                // the response get back from Cloudinary
                var result = _cloudinary.Destroy(deleteParams);

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

            // handle the photo that JSON file generate
            if (photoFromRepo.PublicId == null)
            {
                _repo.Delete(photoFromRepo);
            }


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

            return(BadRequest("Failed to delete the photo"));
        }