Exemplo n.º 1
0
        public async Task <ActionResult <Curiosity> > DeleteCuriosity(int id)
        {
            //var curiosity = await _context.Curiosities.FindAsync(id);

            var curiosity = await _context.Curiosities
                            .Include(c => c.Comments)
                            .Include(c => c.CuriosityLikes)
                            .Include(c => c.Photos)
                            .FirstOrDefaultAsync(c => c.Id == id);

            if (curiosity == null)
            {
                ModelState.AddModelError("", $"curiosity with id : {id} not found");
                return(NotFound(ModelState));
            }

            var tourCuriosities = _context.tourCuriosities.Where(tc => tc.Curiosity == curiosity).ToArray();

            _context.tourCuriosities.RemoveRange(tourCuriosities);

            if (curiosity.Image != null)
            {
                await _AzureFileService.DeleteCuriosityImageAsync(curiosity.Image);

                //System.IO.File.Delete(webHostEnvironment.WebRootPath + "//images//" + curiosity.Image);
            }

            //removing the photos folder from the uploads/gallery folder
            if (curiosity.Photos != null && curiosity.Photos.Count != 0)
            {
                //System.IO.Directory.Delete(webHostEnvironment.WebRootPath + "//uploads//gallery//" + curiosity.Id, true);
                foreach (var photo in curiosity.Photos)
                {
                    await _AzureFileService.DeleteCuriosityPhotoGalleryAsync(photo.PhotoPath);
                }
                _context.Photos.RemoveRange(curiosity.Photos);
            }

            //removing the related records from the child tables
            if (curiosity.Comments != null)
            {
                _context.Comments.RemoveRange(curiosity.Comments);
            }

            if (curiosity.CuriosityLikes != null)
            {
                _context.CuriosityLikes.RemoveRange(curiosity.CuriosityLikes);
            }



            //removing the entity self from the db
            _context.Curiosities.Remove(curiosity);
            await _context.SaveChangesAsync();

            return(curiosity);
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Photo> > DeletePhoto(int id)
        {
            var photo = await _context.Photos.FindAsync(id);

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

            if (photo.PhotoPath != null)
            {
                //System.IO.File.Delete(webHostEnvironment.WebRootPath + "/uploads//gallery/" + photo.CuriosityId + "/" + photo.PhotoPath);
                await _AzureFileService.DeleteCuriosityPhotoGalleryAsync(photo.PhotoPath);
            }


            _context.Photos.Remove(photo);
            await _context.SaveChangesAsync();

            return(photo);
        }