public ActionResult ViewTopic(Guid topicId, string threadTitle, string threadContent, bool isSticky) { ViewTopicModel viewTopicModel = new ViewTopicModel() { Threads = new List <ForumThreadModel>() }; using (CGWebEntities entities = new CGWebEntities()) { UserProfile currentUserProfile = entities.UserProfiles.Where(P => P.UserName.Equals(User.Identity.Name)).Single(); ForumThread newThread = new ForumThread() { CreatedBy = currentUserProfile.UserId, CreatedOn = DateTime.UtcNow, ForumTopic = topicId, IsSticky = isSticky, ModifiedOn = null, ThreadContent = threadContent, ThreadId = Guid.NewGuid(), ThreadTitle = HtmlSanitizerUtility.SanitizeInputStringNoHTML(threadTitle) }; entities.ForumThreads.Add(newThread); entities.SaveChanges(); ModelState.Clear(); ForumTopic currentTopic = entities.ForumTopics.Where(T => T.TopicId.Equals(topicId)).Single(); viewTopicModel.CurrentTopic = currentTopic.ConvertToForumTopicModel(); viewTopicModel.ParentForum = currentTopic.Forum.ConvertToViewForumModel(false, false); if (!currentTopic.IsPublic && !Request.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } var currentTopicGroup = entities.ForumThreads.Where(FT => FT.ForumTopic.Equals(topicId)); int currentTopicViewLimit = Convert.ToInt32(ConfigurationManager.AppSettings["ForumTopicPagingLimit"]); viewTopicModel.MaxPages = (int)Math.Ceiling((double)currentTopicGroup.Count() / (double)currentTopicViewLimit); viewTopicModel.CurrentPage = viewTopicModel.MaxPages - 1; foreach (ForumThread thread in currentTopicGroup.OrderByDescending(FT => FT.CreatedOn).OrderByDescending(FT => FT.IsSticky).Skip(currentTopicViewLimit * viewTopicModel.CurrentPage).Take(currentTopicViewLimit)) { viewTopicModel.Threads.Add(thread.ConvertToForumThreadModel(false)); } } return(View(viewTopicModel)); }
public ActionResult ViewForum(Guid forumId, string topicTitle, string topicDescription, bool isPublic) { using (CGWebEntities entities = new CGWebEntities()) { UserProfile currentUserProfile = entities.UserProfiles.Where(P => P.UserName.Equals(User.Identity.Name)).Single(); ForumTopic newTopic = new ForumTopic() { CreatedBy = currentUserProfile.UserId, CreatedOn = DateTime.UtcNow, ForumId = forumId, IsPublic = isPublic, TopicDescription = topicDescription, TopicId = Guid.NewGuid(), TopicTitle = HtmlSanitizerUtility.SanitizeInputStringNoHTML(topicTitle) }; entities.ForumTopics.Add(newTopic); entities.SaveChanges(); } return(ViewForum(forumId)); }