public RedirectToActionResult Add(int id) { var book = data.Get(new QueryOptions <Book> { Includes = "BookAuthors.Author, Genre", Where = b => b.BookId == id }); if (book == null) { TempData["message"] = "Unable to add book to cart."; } else { var dto = new BookDTO(); dto.Load(book); CartItem item = new CartItem { Book = dto, Quantity = 1 // default value }; cart.Add(item); cart.Save(); TempData["message"] = $"{book.Title} added to cart"; } var builder = new BooksGridBuilder(HttpContext.Session); return(RedirectToAction("List", "Book", builder.CurrentRoute)); }
public ViewResult List(BooksGridDTO values) { var builder = new BooksGridBuilder(HttpContext.Session, values, defaultSortField: nameof(Book.Title)); var options = new BookQueryOptions { Include = "BookAuthors.Author, Genre", OrderByDirection = builder.CurrentRoute.SortDirection, PageNumber = builder.CurrentRoute.PageNumber, PageSize = builder.CurrentRoute.PageSize }; options.SortFilter(builder); var vm = new BookListViewModel { Books = data.Books.List(options), Authors = data.Authors.List(new QueryOptions <Author> { OrderBy = a => a.FirstName }), Genres = data.Genres.List(new QueryOptions <Genre> { OrderBy = g => g.Name }), CurrentRoute = builder.CurrentRoute, TotalPages = builder.GetTotalPages(data.Books.Count) }; return(View(vm)); }
public RedirectToActionResult PageSize(int pagesize) { var builder = new BooksGridBuilder(HttpContext.Session); builder.CurrentRoute.PageSize = pagesize; builder.SaveRouteSegment(); return(RedirectToAction("List", builder.CurrentRoute)); }
public ViewResult Index() { var builder = new BooksGridBuilder(HttpContext.Session); var vm = new CartViewModel { List = cart.List, Subtotal = cart.Subtotal, BookGridRoute = builder.CurrentRoute }; return(View(vm)); }
public void SortFilter(BooksGridBuilder builder) { //filter if (builder.IsFilterByGenre) { Where = b => b.GenreId == builder.CurrentRoute.GenreFilter; } if (builder.IsFilterByPrice) { if (builder.CurrentRoute.PriceFilter == "under7") { Where = b => b.Price < 7; } else if (builder.CurrentRoute.PriceFilter == "7to14") { Where = b => b.Price >= 7 && b.Price <= 14; } else { Where = b => b.Price > 14; } } if (builder.IsFilterByAuthor) { int id = builder.CurrentRoute.AuthorFilter.ToInt(); // to filter the books by author, use the LINQ Any() method. if (id > 0) { Where = b => b.BookAuthors.Any(ba => ba.Author.AuthorId == id); } } //sort if (builder.IsSortByGenre) { OrderBy = b => b.Genre.Name; } else if (builder.IsSortByPrice) { OrderBy = b => b.Price; } else { OrderBy = b => b.Title; } }
public RedirectToActionResult Filter(string[] filter, bool clear = false) { var builder = new BooksGridBuilder(HttpContext.Session); if (clear) { builder.ClearFilterSegments(); } else { var author = data.Authors.Get(filter[0].ToInt()); builder.LoadFilterSegments(filter, author); } builder.SaveRouteSegments(); return(RedirectToAction("List", builder.CurrentRoute)); }
public RedirectToActionResult Filter(string[] filter, bool clear = false) { // get current route segments from session var builder = new BooksGridBuilder(HttpContext.Session); // clear or update filter route segment values. If update, get author data // from database so can add author name slug to author filter value. if (clear) { builder.ClearFilterSegments(); } else { var author = data.Authors.Get(filter[0].ToInt()); builder.CurrentRoute.PageNumber = 1; builder.LoadFilterSegments(filter, author); } // save route data back to session and redirect to Book/List action method, // passing dictionary of route segment values to build URL builder.SaveRouteSegments(); return(RedirectToAction("List", builder.CurrentRoute)); }
// dto has properties for the paging, sorting, and filtering route segments defined in the Startup.cs file public ViewResult List(BooksGridDTO values) { // get grid builder, which loads route segment values and stores them in session var builder = new BooksGridBuilder(HttpContext.Session, values, defaultSortField: nameof(Book.Title)); // create a BookQueryOptions object to build a query expression for a page of data var options = new BookQueryOptions { Includes = "BookAuthors.Author, Genre", OrderByDirection = builder.CurrentRoute.SortDirection, PageNumber = builder.CurrentRoute.PageNumber, PageSize = builder.CurrentRoute.PageSize }; // call the SortFilter() method of the BookQueryOptions object and pass it the builder // object. It uses the route information and the properties of the builder object to // add sort and filter options to the query expression. options.SortFilter(builder); // create view model and add page of book data, data for drop-downs, // the current route, and the total number of pages. var vm = new BookListViewModel { Books = data.Books.List(options), Authors = data.Authors.List(new QueryOptions <Author> { OrderBy = a => a.FirstName }), Genres = data.Genres.List(new QueryOptions <Genre> { OrderBy = g => g.Name }), CurrentRoute = builder.CurrentRoute, TotalPages = builder.GetTotalPages(data.Books.Count) }; // pass view model to view return(View(vm)); }