using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Domain.Entities; using Domain.Repositories; using FilmsRUsOnline.Models; namespace FilmsRUsOnline.Controllers { public class FilmsController : Controller { // The controller needs a films repository to access films. private IFilmsRepository filmsRepository; // Number of films to display on each page. public int PageSize = 3; // Constructor - approach #1, creates a repository manually. public FilmsController() { filmsRepository = new DummyFilmsRepository(); } // Constructor - approach #2, gets given a repository by Dependency Injection (DI). public FilmsController(IFilmsRepository filmsRepository) { this.filmsRepository = filmsRepository; } // Action method to list a page of films (page-by-page). // Note the default value for the pageNumber - this is a C# 4 language feature. // If you're using C# 3.x, do this instead: [DefaultValue(1)]int pageNumber public ViewResult List(string genre, int pageNumber = 1) { // If the URL specifies a genre, only show films in that genre. var filmsToShow = (genre == null) ? filmsRepository.Films : filmsRepository.Films.Where(x => x.Genre == genre); FilmsViewModel vm = new FilmsViewModel() { // Pass "list of films" info to view. Films = filmsToShow.Skip((pageNumber - 1) * PageSize) .Take(PageSize) .ToList(), // Pass "page information" info to view. PaginationInfo = new PaginationInfo { CurrPage = pageNumber, ItemsPerPage = PageSize, NumItems = filmsToShow.Count() }, // Pass "current genre" info to view. CurrentGenre = genre }; return View(vm); } } }