示例#1
0
        public async Task <IActionResult> DeleteListing(int id)
        {
            var listingfromrepo = await _repo.GetListing(id);

            ICollection <Photo> photos = listingfromrepo.Photos;

            foreach (var photoFromRepo in photos)
            {
                if (photoFromRepo.PublicId != null)
                {
                    var deleteParams = new DeletionParams(photoFromRepo.PublicId);
                    var result       = _cloudinary.Destroy(deleteParams);
                    if (result.Result == "ok")
                    {
                        listingfromrepo.Photos.Remove(photoFromRepo);
                    }
                }
                if (photoFromRepo.PublicId == null)
                {
                    _repo.Delete(photoFromRepo);
                }
            }

            _repo.Delete(listingfromrepo);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete listing"));
        }
示例#2
0
        public async Task <IActionResult> DeletePhoto(int listingId, int id)
        {
            var listing = await _repo.GetListing(listingId);

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

            var photoFromRepo = await _repo.GetPhoto(id);

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

            if (photoFromRepo.PublicId != null)
            {
                var deleteParams = new DeletionParams(photoFromRepo.PublicId);
                var result       = _cloudinary.Destroy(deleteParams);
                if (result.Result == "ok")
                {
                    listing.Photos.Remove(photoFromRepo);
                }
            }
            if (photoFromRepo.PublicId == null)
            {
                _repo.Delete(photoFromRepo);
            }


            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete photo"));
        }