public void Initialize() { DbContextHelpers contextHelpers = new DbContextHelpers(); search = new CommentSearch(); db = contextHelpers.getDbContext(); adminCon = new AdminCommentsController(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(CommentSearch search) { if (Session != null) { if (!String.IsNullOrEmpty(search.message)) Session["MessageSearchParam"] = search.message; else Session["MessageSearchParam"] = ""; if (!String.IsNullOrEmpty(search.username)) Session["UsernameSearchParam"] = search.username; else Session["UsernameSearchParam"] = ""; } }
/// <summary> /// Sets the WorkoutSearch object with the stored session search variables if they exist /// </summary> /// <param name="search">The WorkoutSearch object to set</param> /// <returns>The WorkoutSearch object set with the session search variables if the session exists, else the passed in WorkoutSearch object</returns> private CommentSearch setSearchFromSession(CommentSearch search) { if (Session != null) { search.message = Session["MessageSearchParam"] as String; search.username = Session["UsernameSearchParam"] 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="workouts">The base workout 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 workouts</returns> private IQueryable<comment> doSearch(IQueryable<comment> comments, CommentSearch search, String filterString, string sortBy, int? page) { if (page != null || !String.IsNullOrEmpty(sortBy) || !String.IsNullOrEmpty(filterString)) { search = this.setSearchFromSession(search); } else setSessionFromSearch(search); if (!String.IsNullOrEmpty(search.message)) comments = comments.Where(c => c.message.Contains(search.message)); if (!String.IsNullOrEmpty(search.username)) comments = comments.Where(c => c.user.username.Contains(search.username)); return comments; }
// GET: AdminComments public ActionResult Index(string filterString, string sortBy, int? page, CommentSearch commentSearch) { //var comments = db.comments.Include(c => c.user).Include(c => c.workout); //return View(comments.ToList()); var comments = from c in db.comments select c; comments = this.doSearch(comments, commentSearch, filterString, sortBy, page); comments = this.doSort(comments, sortBy); int pageNumber = (page ?? 1); var view = View("Index", comments.ToPagedList(pageNumber, pageSize)); return view; }