コード例 #1
0
ファイル: ForumController.cs プロジェクト: CincyGeeks/CGWeb
        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);
        }
コード例 #2
0
ファイル: ForumController.cs プロジェクト: CincyGeeks/CGWeb
        public ActionResult ViewTopic(ForumTopicRequestModel requestModel)
        {
            ViewTopicModel viewTopicModel = new ViewTopicModel(){
                Threads = new List<ForumThreadModel>()
            };

            using (CGWebEntities entities = new CGWebEntities())
            {
                ForumTopic currentTopic = entities.ForumTopics.Where(T => T.TopicId.Equals(requestModel.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(requestModel.TopicId));

                int currentTopicViewLimit = Convert.ToInt32(ConfigurationManager.AppSettings["ForumTopicPagingLimit"]);
                viewTopicModel.CurrentPage = requestModel.CurrentPage;
                viewTopicModel.MaxPages = (int)Math.Ceiling((double)currentTopicGroup.Count() / (double)currentTopicViewLimit);

                foreach (ForumThread thread in currentTopicGroup.OrderByDescending(FT => FT.CreatedOn).OrderByDescending(FT => FT.IsSticky).Skip(currentTopicViewLimit * requestModel.CurrentPage).Take(currentTopicViewLimit))
                {
                    viewTopicModel.Threads.Add(thread.ConvertToForumThreadModel(false));
                }
            }

            return View(viewTopicModel);
        }