/// <summary> /// @Author: Patrick Tseng /// Grabs all the threads given a subforum ID /// </summary> /// <param name="id">subforum ID</param> /// <returns>View</returns> public ActionResult Thread(int id) { if(SessionHandler.Logon) loginTimer = new HeartbeatMonitor(SessionHandler.UID, HttpContext); Subforum sf = _db.Subforums.SingleOrDefault(i => i.id == id); if (sf == null) { return View("Error", new ArgumentException("Subforum does not exist!")); } if (sf.user_access == "admin" && SessionHandler.Role != "admin") { return View("Error", new ArgumentException("You do not have sufficient credentials to view this page.")); } ViewBag.SubforumName = _db.Subforums.Single(i => i.id == id).title; //gets the subforum name according to the id ViewBag.SubforumID = id; ViewBag.UserID = SessionHandler.UID; var threads = from d in _db.ForumThreads where d.subforum_id == id select d; int userID; //required because the author ID isn't a foreign key to the User table, D'oh! Hashtable postAuthors = new Hashtable(); Hashtable lastPost = new Hashtable(); Hashtable replies = new Hashtable(); Hashtable users = new Hashtable(); //gets all required information for threads foreach (var thread in threads) { postAuthors.Add(thread.id, _db.Users.Single(i => i.id == thread.author_id).username); var posts = from p in _db.ForumPosts where p.thread_id == thread.id orderby p.datetime_posted descending select p; lastPost.Add(thread.id, posts.FirstOrDefault()); if (posts.FirstOrDefault() != null) { userID = posts.FirstOrDefault().author_id; if (!users.ContainsKey(userID)) users.Add(userID, _db.Users.SingleOrDefault(j => j.id == userID).username); } replies.Add(thread.id, posts.Count() - 1); } ViewBag.PostAuthors = postAuthors; ViewBag.LastPost = lastPost; ViewBag.Replies = replies; ViewBag.Users = users; return View(threads.ToList()); }
/// <summary> /// @Author: Patrick Tseng /// Returns all the posts from a thread /// </summary> /// <param name="threadID">ID of thread</param> /// <returns>View</returns> public ActionResult Index(int threadID) { if (SessionHandler.Logon) loginTimer = new HeartbeatMonitor(SessionHandler.UID, HttpContext); ForumPost test = _db.ForumPosts.FirstOrDefault(i => i.thread_id == threadID); if (test == null) { return View("Error", new ArgumentException("Thread does not exist!")); } //gets the thread title and all the posts ViewBag.ThreadID = threadID; ViewBag.ThreadTitle = _db.ForumThreads.First(i => i.id == threadID).title; var posts = _db.ForumPosts.Where(i => i.thread_id == threadID); //post author and post author's avatar //here because the author_id isn't a foreign key to the users table, oops Hashtable postAuthors = new Hashtable(); Hashtable postAuthorsAvatar = new Hashtable(); //grabs the author's name and avatar given the ID foreach (var post in posts) { postAuthors.Add(post.id, _db.Users.Single(i => i.id == post.author_id).username); postAuthorsAvatar.Add(post.id, _db.Users.Single(i => i.id == post.author_id).image_url); } var subforumID = _db.ForumThreads.Single(i => i.id == threadID); var parentSubforum = _db.Subforums.First(j => j.id == subforumID.subforum_id); //sets view parameters ViewBag.SubforumName = parentSubforum.title; ViewBag.SubforumID = parentSubforum.id; ViewBag.PostAuthors = postAuthors; ViewBag.PostAuthorsAvatar = postAuthorsAvatar; ViewBag.HeadPost = posts.FirstOrDefault().id; _db.ForumThreads.SingleOrDefault(i => i.id == threadID).num_hits += 1; _db.SaveChanges(); return View(posts.ToList()); }