Esempio n. 1
0
        public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                this.AddNotification("No user ID provided.", NotificationType.ERROR);
                return(RedirectToAction("Index"));
            }

            using (var db = new GalleryDbContext())
            {
                //Get user from database
                var user = db.Users
                           .FirstOrDefault(u => u.Id == id);

                if (user == null)
                {
                    this.AddNotification("Such a user doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("Index"));
                }

                //Get user's pictures form database
                var userPictures = db.Pictures
                                   .Where(p => p.PicUploader.Id == user.Id)
                                   .ToList();

                //Delete user's pictures
                foreach (var picture in userPictures)
                {
                    //Adjust the positional boolean variables of the other pics before the deletion
                    AdjustCategoryPositions.Delete(db, picture);

                    //Delete the picture from the server
                    var physicalPath = Server.MapPath(picture.ImagePath);
                    System.IO.File.Delete(physicalPath);

                    db.Pictures.Remove(picture);
                    db.SaveChanges();
                }

                // Delete the user's directory and the pic in there from ~/Content/images/profilePics:
                var userIdFolder  = user.Id;
                var userDirectory = Server.MapPath($"~/Content/images/profilePics/{userIdFolder}");
                if (Directory.Exists(userDirectory))
                {
                    Directory.Delete(userDirectory, true);
                }

                //Delete user and save changes
                db.Users.Remove(user);
                db.SaveChanges();

                this.AddNotification("The user was deleted.", NotificationType.SUCCESS);
                return(RedirectToAction("Index"));
            }
        }
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                this.AddNotification("No picture ID provided.", NotificationType.ERROR);
                return(RedirectToAction("ListCategories", "Home"));
            }

            using (var db = new GalleryDbContext())
            {
                // Get picture from database
                var picture = db.Pictures
                              .Where(p => p.Id == id)
                              .Include(p => p.PicUploader)
                              .Include(p => p.Category)
                              .FirstOrDefault();

                // Check if picture exists
                if (picture == null)
                {
                    this.AddNotification("Such a picture doesn't exist.", NotificationType.ERROR);
                    return(RedirectToAction("ListCategories", "Home"));
                }

                // Adjust the positional boolean variables of the other pics before the deletion
                AdjustCategoryPositions.Delete(db, picture);

                // Getting the category of the pic before the deletion in order to redirect to Home/ListPictures after that
                var picCategoryId = (int?)picture.CategoryId;

                // Delete the picture from the ~/Content/images/astroPics folder:
                var physicalPath = Server.MapPath(picture.ImagePath);
                System.IO.File.Delete(physicalPath);

                // Delete the picture from the database
                db.Pictures.Remove(picture);
                db.SaveChanges();

                // Redirect to the page with all pics in the current category
                this.AddNotification("The picture was deleted.", NotificationType.SUCCESS);
                return(RedirectToAction("ListPictures", "Home", new { id = picCategoryId }));
            }
        }