コード例 #1
0
        public ActionResult NewShelf(ShelvesViewModel model)
        {
            if (String.IsNullOrEmpty(model.Name))
            {
                return(RedirectToAction("Index"));
            }

            UserProfile profile = null;

            //save
            using (var db = new SDCContext())
            {
                profile = db.UserProfiles.Find(((UserProfile)Session["UserInfo"]).UserId);

                Shelf newShelf = new Shelf()
                {
                    CreationDate = DateTime.Now,
                    Name         = model.Name,
                    IsVisible    = model.IsVisible,
                    Owner        = profile
                };

                db.Shelves.Add(newShelf);
                db.SaveChanges();
                Session["UserInfoEx"] = profile.GetExtendedInfo(db);
            }

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public ActionResult EditShelf(ShelvesViewModel model)
        {
            if (String.IsNullOrEmpty(model.Name))
            {
                return(RedirectToAction("Index"));
            }

            int id = model.EditShelfId;

            using (var db = new SDCContext())
            {
                var shelf = db.Shelves.Find(id);
                if (shelf == null)
                {
                    return(RedirectToAction("Index"));
                }

                var userProfile = (UserProfile)this.Session["UserInfo"];
                if (shelf.CanBeEdited(userProfile))
                {
                    shelf.Name      = model.Name;
                    shelf.IsVisible = model.IsVisible;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(RedirectToAction("Index"));
                }
            }
        }
コード例 #3
0
        public ActionResult ShelvesList()
        {
            ShelvesViewModel shelvesData = new ShelvesViewModel();

            shelvesData.Shelf     = new Shelf();
            shelvesData.ShelfList = db.Shelves.ToList();

            return(View("Shelves", shelvesData));
        }
コード例 #4
0
        public ActionResult DeleteShelf(ShelvesViewModel model)
        {
            int id = model.DeleteShelfId;

            using (var db = new SDCContext())
            {
                //sanity check: this shelf exists.
                var shelf = db.Shelves.Find(id);
                if (shelf == null)
                {
                    return(RedirectToAction("Index"));
                }

                var userProfile = (UserProfile)this.Session["UserInfo"];

                if (shelf.CanBeEdited(userProfile))
                {
                    //we allow deletion

                    //delete all books in this shelf.
                    //todo: delete all other entities that are linked
                    var books = (from b in db.Books
                                 where b.Shelf.Id == shelf.Id
                                 select b).ToList();
                    db.Books.RemoveRange(books);
                    db.Shelves.Remove(shelf);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                else
                {
                    //bad user, bad!
                    return(RedirectToAction("Index"));
                }
            }
        }