public ActionResult ViewThread(ThreadViewModel model) { using (ForumRespository db = new ForumRespository()) { Forum_Thread Thread = db.GetThreadByID(model.Id); if (Thread == null) { return NotFoundView("Thread"); } if (model.Page < 1) return RedirectToAction("ViewThread", new { id = model.Id, page = 1}); // page less than 0 for existing thread equals redirect to valid page. model.AddNavigation(Thread); Forum_User ThreadViewUser = GetCurrentUser(db); if (!db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => P.AllowView)) return AuthenticationHelper.AccessDeniedView(model); model.AllowEditThread = db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => (P.AllowDeleteOwnThread && Thread.Forum_Posts[0].PosterID == ThreadViewUser.UserID && Thread.Forum_Posts[0].PosterID != (int)BuildInUser.Guest) || P.AllowDeleteAllThread || P.AllowMoveThread || P.AllowLockThread); model.Locked = Thread.Locked; model.ThreadTitle = Thread.Title; int UserID = 0; Forum_User U = GetCurrentUser(db); if (U != null) { UserID = U.UserID; db.SetLastPost(Thread, U, Math.Min(model.Page * POSTS_PER_PAGE, Thread.Posts)); db.Save(); } model.LastPage = (Thread.Posts - 1) / POSTS_PER_PAGE + 1; if (model.Page > model.LastPage) return RedirectToAction("ViewThread", new { id = model.Id, page = model.LastPage }); // page greater than what exists equals redirect to last page. IEnumerable<Forum_Post> Posts = Thread.Forum_Posts.Skip((model.Page - 1)* POSTS_PER_PAGE).Take(POSTS_PER_PAGE); int PostNumber = 0; foreach (Forum_Post Post in Posts) { PostViewModel PostModel = new PostViewModel(); PostModel.Locked = model.Locked; PostModel.PostNumber = ++PostNumber; PostModel.ThreadID = model.Id; PostModel.PostText = PostParser.Parse(Post.PostText); PostModel.PostTime = Post.TimeStamp; PostModel.Poster = new UserViewModel(); PostModel.PostID = Post.PostID; PostModel.Poster.Name = Post.Forum_User.Username; PostModel.Poster.UserID = Post.PosterID; PostModel.AllowDelete = (PostNumber > 1 || model.Page > 1) && db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => (P.AllowDeleteOwnPost && Post.PosterID == ThreadViewUser.UserID && Post.PosterID != (int)BuildInUser.Guest) || P.AllowDeleteAllPosts); PostModel.AllowEdit = db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => (P.AllowEditOwnPost && Post.PosterID == ThreadViewUser.UserID && Post.PosterID != (int)BuildInUser.Guest) || P.AllowEditAllPosts); model.PostList.Add(PostModel); } return View(model); } }
public ActionResult NewThread(WritePostViewModel model, string button) { using (ForumRespository db = new ForumRespository()) { Forum_Category Category = db.GetCategoryByID(model.id); if (Category == null) return NotFoundView("Category"); model.AddNavigation(Category); model.AddNavigation("New thread"); Forum_User Poster = GetCurrentUser(db); if (!db.CheckCategoryPermissions(Category, Poster, P => P.AllowNewThread)) return AuthenticationHelper.AccessDeniedView(model); if (String.Equals(button, "preview", StringComparison.InvariantCultureIgnoreCase)) { model.ShowPost = true; model.PostHtml = PostParser.Parse(model.PostText); ModelState.Clear(); } else if (IsHttpPost) { if (!AntiForgeryTokenValid) { ModelState.AddModelError("AntiForgery", "The antiforgery token was invalid."); } if (String.IsNullOrEmpty(model.ThreadTitle)) { ModelState.AddModelError("ThreadTitle", "A thread title is required."); } if (ModelState.IsValid) { Forum_Thread NewThread = new Forum_Thread(); NewThread.Title = model.ThreadTitle; NewThread.PosterID = Poster.UserID; Forum_Post InitialPost = new Forum_Post(); InitialPost.TimeStamp = DateTime.Now; InitialPost.PosterID = NewThread.PosterID; InitialPost.PostText = model.PostText; NewThread.Forum_Posts.Add(InitialPost); NewThread.Posts = 1; NewThread.LastPostTime = InitialPost.TimeStamp; NewThread.CategoryID = model.id; // Save and add thread to database db.AddThread(NewThread); db.SetLastPost(NewThread, Poster, 1); db.Save(); return RedirectToAction("ViewCategory", new { id = model.id }); } } else { ModelState.Clear(); } model.EditTitle = true; model.Title = "Post new Thread"; return View("WritePost", model); } }