Exemple #1
0
        public ActionResult Details(int id, int page=1, int pagesize = 0)
        {
            if (id == 0)
                return RedirectToAction("Index", "Home");

            var profile = (UserProfile)this.Session["UserInfo"];
            if(profile == null)
                return RedirectToAction("Index", "Home");

            if (pagesize < 1 || pagesize > 100)
                pagesize = profile.PageSize;

            ShelfViewModel vm = null;

            using (var db = new SDCContext())
            {
                profile.UpdatePageSize(db, pagesize);

                ViewBag.Languages = db.Languages.Where(p => p.IsVisible).OrderBy(p => p.Code).ToList();
                ViewBag.Genres = db.Genres.OrderBy(p => p.Name).ToList();

                var shelf = db.Shelves
                    .AsNoTracking()
                    .Include(a => a.Books)
                    .Include(a => a.Books.Select(b => b.Pictures))
                    .Include(a => a.Books.Select(b => b.Authors))
                    .Include(a => a.Books.Select(b => b.Genres))
                    .Include(a => a.Books.Select(b => b.Publisher))
                    .FirstOrDefault(p => p.Id == id);
                if (shelf == null)
                    return RedirectToAction("Index", "Home");

                int totalPages = ((int)Math.Ceiling((double)shelf.Books.Count / pagesize));
                if (page > totalPages)
                    page = totalPages;

                //actual pagination takes place here
                var show_books = shelf.Books
                        .OrderBy(b => b.AddedDate)
                        .Skip((page - 1) * pagesize)
                        .Take(pagesize)
                        .Select(b => AutoMapper.Mapper.Map<BookViewModel>(b));

                vm = new ShelfViewModel()
                {
                    Id = shelf.Id,
                    Name = shelf.Name,
                    IsVisible = shelf.IsVisible,
                    CanEdit = shelf.CanBeEdited(profile),
                    BookCount = shelf.Books.Count(),
                    Books = show_books,
                    Languages = Language.GetAll(db),
                    Genres = Genre.GetAll(db),
                    DefaultLanguage = profile.Country.Language,
                    Pagination = new PaginationViewModel()
                    {
                        Id = shelf.Id,
                        Action = "Details",
                        Controller = "Shelves",
                        Page = page,
                        PageSize=pagesize,
                        TotalPages = totalPages,
                        EntityCount = show_books.Count(),
                        EntityName = "Books"
                    }
                };

                if (profile.UserId == shelf.Owner.UserId)
                {
                    ViewBag.Breadcrumbs = Breadcrumb.Generate(
                        "My shelves", Url.Action("Index", "Shelves"),
                        vm.Name, "");
                }
                else
                {
                    ViewBag.Breadcrumbs = Breadcrumb.Generate("Directory", "", vm.Name, "");
                }

                db.Dispose();
            }

            return View(vm);
        }
Exemple #2
0
        public ActionResult Details(int id, int page = 1, int pagesize = 0)
        {
            if (id == 0)
            {
                return(RedirectToAction("Index", "Home"));
            }

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

            if (profile == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (pagesize < 1 || pagesize > 100)
            {
                pagesize = profile.PageSize;
            }

            ShelfViewModel vm = null;

            using (var db = new SDCContext())
            {
                profile.UpdatePageSize(db, pagesize);

                ViewBag.Languages = db.Languages.Where(p => p.IsVisible).OrderBy(p => p.Code).ToList();
                ViewBag.Genres    = db.Genres.OrderBy(p => p.Name).ToList();


                var shelf = db.Shelves
                            .AsNoTracking()
                            .Include(a => a.Books)
                            .Include(a => a.Books.Select(b => b.Pictures))
                            .Include(a => a.Books.Select(b => b.Authors))
                            .Include(a => a.Books.Select(b => b.Genres))
                            .Include(a => a.Books.Select(b => b.Publisher))
                            .FirstOrDefault(p => p.Id == id);
                if (shelf == null)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                int totalPages = ((int)Math.Ceiling((double)shelf.Books.Count / pagesize));
                if (page > totalPages)
                {
                    page = totalPages;
                }

                //actual pagination takes place here
                var show_books = shelf.Books
                                 .OrderBy(b => b.AddedDate)
                                 .Skip((page - 1) * pagesize)
                                 .Take(pagesize)
                                 .Select(b => AutoMapper.Mapper.Map <BookViewModel>(b));

                vm = new ShelfViewModel()
                {
                    Id              = shelf.Id,
                    Name            = shelf.Name,
                    IsVisible       = shelf.IsVisible,
                    CanEdit         = shelf.CanBeEdited(profile),
                    BookCount       = shelf.Books.Count(),
                    Books           = show_books,
                    Languages       = Language.GetAll(db),
                    Genres          = Genre.GetAll(db),
                    DefaultLanguage = profile.Country.Language,
                    Pagination      = new PaginationViewModel()
                    {
                        Id          = shelf.Id,
                        Action      = "Details",
                        Controller  = "Shelves",
                        Page        = page,
                        PageSize    = pagesize,
                        TotalPages  = totalPages,
                        EntityCount = show_books.Count(),
                        EntityName  = "Books"
                    }
                };

                if (profile.UserId == shelf.Owner.UserId)
                {
                    ViewBag.Breadcrumbs = Breadcrumb.Generate(
                        "My shelves", Url.Action("Index", "Shelves"),
                        vm.Name, "");
                }
                else
                {
                    ViewBag.Breadcrumbs = Breadcrumb.Generate("Directory", "", vm.Name, "");
                }

                db.Dispose();
            }

            return(View(vm));
        }