コード例 #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"));
            }
        }
コード例 #2
0
        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 }));
            }
        }
コード例 #3
0
        public ActionResult Upload(PictureViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                using (var db = new GalleryDbContext())
                {
                    // Get uploader's ID
                    var uploaderId = db.Users
                                     .First(u => u.UserName == User.Identity.Name)
                                     .Id;

                    var picture = new Picture(uploaderId, model.PicTitle, model.PicDescription, model.CategoryId);

                    // Getting the name of the category to add it to the current picture's property:
                    var picCategoryName = db.Categories
                                          .Where(c => c.Id == picture.CategoryId)
                                          .Select(c => c.Name)
                                          .ToArray();

                    picture.CategoryName = picCategoryName[0];

                    SetPictureTags(picture, model, db);

                    if (image != null && image.ContentLength > 0)
                    {
                        var imagesOfThatCategoryDir = $"~/Content/images/astroPics/{picture.CategoryName}/";
                        var picFileName             = image.FileName;
                        var uploadPath   = imagesOfThatCategoryDir + picFileName;
                        var physicalPath = Server.MapPath(uploadPath);

                        // In case the picture already exists a notification is shown:
                        if (System.IO.File.Exists(physicalPath))
                        {
                            this.AddNotification("Picture with this file name already exists in that category.", NotificationType.ERROR);

                            model.Categories = db.Categories
                                               .OrderBy(c => c.Name)
                                               .ToList();

                            return(View(model));
                        }

                        image.SaveAs(physicalPath);

                        if (ImageValidator.IsImageValid(physicalPath))
                        {
                            picture.ImagePath = uploadPath;

                            AdjustCategoryPositions.Upload(db, picture);

                            return(RedirectToAction("Details", new { id = picture.Id }));
                        }
                        else
                        {
                            // Deleting the file from ~/Content/images/astroPics/:
                            System.IO.File.Delete(physicalPath);

                            this.AddNotification("Invalid picture format.", NotificationType.ERROR);

                            model.Categories = db.Categories
                                               .OrderBy(c => c.Name)
                                               .ToList();

                            return(View(model));
                        }
                    }
                    else
                    {
                        this.AddNotification("No picture selected for upload or empty file chosen.", NotificationType.ERROR);

                        model.Categories = db.Categories
                                           .OrderBy(c => c.Name)
                                           .ToList();

                        return(View(model));
                    }
                }
            }

            using (var db = new GalleryDbContext())
            {
                model.Categories = db.Categories
                                   .OrderBy(c => c.Name)
                                   .ToList();

                return(View(model));
            }
        }
コード例 #4
0
        public ActionResult Edit(PictureViewModel model)
        {
            // Check if model state is valid
            if (ModelState.IsValid)
            {
                using (var db = new GalleryDbContext())
                {
                    // Get picture form database
                    var picture = db.Pictures
                                  .Include(u => u.PicUploader)
                                  .FirstOrDefault(p => p.Id == model.Id);

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

                    // Set picture properties
                    picture.PicTitle       = model.PicTitle;
                    picture.PicDescription = model.PicDescription;

                    // Getting the category of the pic in case it is being changed
                    var previousCategoryIdOfPic = picture.CategoryId;

                    picture.CategoryId = model.CategoryId;

                    var picCategoryName = db.Categories
                                          .Where(c => c.Id == model.CategoryId)
                                          .Select(c => c.Name)
                                          .ToArray();

                    picture.CategoryName = picCategoryName[0];

                    SetPictureTags(picture, model, db);

                    // Save pic state in database
                    db.Entry(picture).State = EntityState.Modified;
                    db.SaveChanges();

                    // If the category of the pic is changed
                    if (previousCategoryIdOfPic != picture.CategoryId)
                    {
                        // Adjust the positional boolean variables of the other pics before the edit
                        AdjustCategoryPositions.Edit(db, picture);

                        // Get the IDs of the pictures from the previous category of the edited pic
                        var previousCategoryPicsIdToBeChanged = db.Pictures
                                                                .Where(p => p.CategoryId == previousCategoryIdOfPic)
                                                                .Select(p => p.Id)
                                                                .ToList();

                        // If there are pics from the previous category of the edited pic
                        if (previousCategoryPicsIdToBeChanged.Count > 0)
                        {
                            // The first pic in the category
                            var first = previousCategoryPicsIdToBeChanged.Min();

                            // The last pic in the category
                            var last = previousCategoryPicsIdToBeChanged.Max();

                            // If there is only 1 pic in the category it becomes now first and last one there
                            if (first == last)
                            {
                                var onlyPicture = db.Pictures
                                                  .FirstOrDefault(p => p.Id == first);

                                onlyPicture.IsLastOfCategory  = true;
                                onlyPicture.IsFirstOfCategory = true;

                                db.Entry(onlyPicture).State = EntityState.Modified;
                            }
                            // If there are 2 or more pics in the category before the edit
                            else
                            {
                                var firstPic = db.Pictures.FirstOrDefault(p => p.Id == first);
                                firstPic.IsFirstOfCategory = true;
                                firstPic.IsLastOfCategory  = false;
                                db.Entry(firstPic).State   = EntityState.Modified;

                                var lastPic = db.Pictures.FirstOrDefault(p => p.Id == last);
                                lastPic.IsFirstOfCategory = false;
                                lastPic.IsLastOfCategory  = true;
                                db.Entry(lastPic).State   = EntityState.Modified;
                            }

                            db.SaveChanges();
                        }

                        // Getting the old directory of the pic
                        var picOldDir = Server.MapPath(picture.ImagePath);

                        var picFileName = picture.ImagePath.Substring(picture.ImagePath.LastIndexOf('/') + 1);

                        picture.ImagePath = $"~/Content/images/astroPics/{picture.CategoryName}/{picFileName}";

                        db.Entry(picture).State = EntityState.Modified;
                        db.SaveChanges();

                        var picNewDir = Server.MapPath(picture.ImagePath);
                        System.IO.File.Move(picOldDir, picNewDir);
                    }

                    this.AddNotification("The picture was edited.", NotificationType.SUCCESS);
                    return(View("Details", picture));
                }
            }

            using (var db = new GalleryDbContext())
            {
                model.Categories = db.Categories
                                   .OrderBy(c => c.Name)
                                   .ToList();

                model.ImagePath = db.Pictures.First(p => p.Id == model.Id).ImagePath;

                return(View(model));
            }
        }