public void Initialize() { DbContextHelpers contextHelpers = new DbContextHelpers(); search = new TypeSearch(); db = contextHelpers.getDbContext(); adminCon = new AdminTypesController(db.Object) { // sign in as admin ControllerContext = MockContext.AuthenticationContext("admin") }; }
/// <summary> /// Sets the session search parameters based on the current search values /// </summary> /// <param name="search">The WorkoutSearch object containing the values to set in the session</param> private void setSessionFromSearch(TypeSearch search) { if (Session != null) { if (!String.IsNullOrEmpty(search.name)) Session["NameSearchParam"] = search.name; else Session["NameSearchParam"] = ""; if (!String.IsNullOrEmpty(search.measure)) Session["MeasureSearchParam"] = search.measure; else Session["MeasureSearchParam"] = ""; } }
/// <summary> /// Sets the TypeSearch object with the stored session search variables if they exist /// </summary> /// <param name="search">The TypeSearch object to set</param> /// <returns>The TypeSearch object set with the session search variables if the session exists, else the passed in TypeSearch object</returns> private TypeSearch setSearchFromSession(TypeSearch search) { if (Session != null) { search.name = Session["NameSearchParam"] as String; search.measure = Session["MeasureSearchParam"] as String; } return search; }
/// <summary> /// Private helper method to perform a new search or maintain a previous search through /// pagination and filter changes /// </summary> /// <param name="types">The base type query result</param> /// <param name="sortBy">The passed sort string if it exists, else null</param> /// <param name="page">The passed page param if it exists, else null</param> /// <returns>The searched types</returns> private IQueryable<type> doSearch(IQueryable<type> types, TypeSearch search, String filterString, string sortBy, int? page) { if (page != null || !String.IsNullOrEmpty(sortBy) || !String.IsNullOrEmpty(filterString)) { search = setSearchFromSession(search); } else setSessionFromSearch(search); if (!String.IsNullOrEmpty(search.name)) types = types.Where(t => t.name.Contains(search.name)); if (!String.IsNullOrEmpty(search.measure)) types = types.Where(t => t.measure.Contains(search.measure)); return types; }
// GET: AdminTypes public ActionResult Index(string filterString, string sortBy, int? page, TypeSearch typeSearch) { //return View(db.types.ToList()); var types = from t in db.types select t; types = this.doSearch(types, typeSearch, filterString, sortBy, page); types = this.doSort(types, sortBy); int pageNumber = (page ?? 1); var view = View("Index", types.ToPagedList(pageNumber, pageSize)); return view; }