Пример #1
0
        public ActionResult AllQuestion(string sort, string search, int?page)
        {
            int    DiscussionCategoryId = 0;
            string s = sort;
            int    result;

            if (int.TryParse(s, out result))
            {
                DiscussionCategoryId = Convert.ToInt32(sort);
            }
            else
            {
            }

            ViewBag.NewestSort         = sort == "newestsearch" ? "newest_desc" : "newest_search";
            ViewBag.OldestSort         = sort == "oldestsearch" ? "oldest_asec" : "oldest_search";
            ViewBag.DiscussionCategory = sort == "discussioncategory" ? "discuss_cat" : "discussion_category";

            ViewBag.CurrentSort   = sort;
            ViewBag.CurrentSearch = search;

            AllQuestionListViewModel mainModel = new AllQuestionListViewModel();
            var listModel  = new List <QuestionListWithOwnerAndLastParticipantViewModel>();
            var reportList = new List <TopTenReport>();
            var topQList   = new List <TopSevenQuestions>();
            var likeList   = new List <MostLikeQuestion>();



            if (!String.IsNullOrEmpty(search))
            {
                var questions = _applicationDbContext.Questions.Include(dc => dc.DiscussionCategory).Where(d => d.Title.ToLower().Contains(search) ||
                                                                                                           d.QuestionText.ToLower().Contains(search) || d.Tag.ToLower().Contains(search)).ToList();


                foreach (var q in questions)
                {
                    var answers = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == q.QuestionId).ToList();
                    QuestionListWithOwnerAndLastParticipantViewModel model = new QuestionListWithOwnerAndLastParticipantViewModel();
                    model.QuestionId         = q.QuestionId;
                    model.Title              = q.Title;
                    model.DiscussionCategory = q.DiscussionCategory.Name;
                    model.DateAsked          = q.DateAsked.ToString("d,MMMM yy");

                    DateTime commentDate        = DateTime.UtcNow.Date;
                    DateTime answerDate         = DateTime.UtcNow.Date;
                    var      userName           = _applicationDbContext.Users.Find(q.UserId);
                    var      getQuestionComment = _applicationDbContext.QuestionComments.Where(id => id.QuestionId == q.QuestionId).OrderByDescending(p => p.QuestionCommentId).FirstOrDefault();
                    var      getAnswerComment   = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == q.QuestionId).OrderByDescending(p => p.AnswerToQuestionId).FirstOrDefault();
                    model.QuestionOwner = userName.NameExtension;
                    if (answers != null)
                    {
                        model.AnswerCount = answers.Count;
                    }
                    else
                    {
                        model.AnswerCount = 0;
                    }

                    if (getQuestionComment == null)
                    {
                        model.LastParticipantOwner = userName.NameExtension;
                        model.LastParticipantDate  = q.DateAsked.ToString("d,MMMM yy");
                        commentDate = q.DateAsked;
                    }
                    else
                    {
                        commentDate = getQuestionComment.DateCommented;
                    }
                    if (getAnswerComment == null)
                    {
                        model.LastParticipantOwner = userName.NameExtension;
                        model.LastParticipantDate  = q.DateAsked.ToString("d,MMM yy");
                        answerDate = q.DateAsked;
                    }
                    else
                    {
                        answerDate = getAnswerComment.DateAnswered;
                    }
                    if (getQuestionComment != null)
                    {
                        if (commentDate >= answerDate)
                        {
                            var commentOwner = _applicationDbContext.Users.Find(getQuestionComment.UserId);
                            model.LastParticipantOwner = commentOwner.NameExtension;
                            model.LastParticipantDate  = getQuestionComment.DateCommented.ToString("d,MMMM yy");
                        }
                    }
                    if (getAnswerComment != null)
                    {
                        if (commentDate <= answerDate)
                        {
                            var questionOwner = _applicationDbContext.Users.Find(getAnswerComment.UserId);
                            model.LastParticipantOwner = questionOwner.NameExtension;
                            model.LastParticipantDate  = getAnswerComment.DateAnswered.ToString("d,MMMM yy");
                        }
                    }

                    listModel.Add(model);
                }
            }
            else
            {
                var questions = _applicationDbContext.Questions.Include(dc => dc.DiscussionCategory).OrderByDescending(d => d.DateAsked).ToList();

                foreach (var q in questions)
                {
                    var answers = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == q.QuestionId).ToList();
                    QuestionListWithOwnerAndLastParticipantViewModel model = new QuestionListWithOwnerAndLastParticipantViewModel();
                    model.QuestionId         = q.QuestionId;
                    model.Title              = q.Title;
                    model.DiscussionCategory = q.DiscussionCategory.Name;
                    model.DateAsked          = q.DateAsked.ToString("d,MMMM yy");

                    DateTime commentDate        = DateTime.UtcNow.Date;
                    DateTime answerDate         = DateTime.UtcNow.Date;
                    var      userName           = _applicationDbContext.Users.Find(q.UserId);
                    var      getQuestionComment = _applicationDbContext.QuestionComments.Where(id => id.QuestionId == q.QuestionId).OrderByDescending(p => p.QuestionCommentId).FirstOrDefault();
                    var      getAnswerComment   = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == q.QuestionId).OrderByDescending(p => p.AnswerToQuestionId).FirstOrDefault();
                    model.QuestionOwner = userName.NameExtension;
                    if (answers != null)
                    {
                        model.AnswerCount = answers.Count;
                    }
                    else
                    {
                        model.AnswerCount = 0;
                    }

                    if (getQuestionComment == null)
                    {
                        model.LastParticipantOwner = userName.NameExtension;
                        model.LastParticipantDate  = q.DateAsked.ToString("d,MMMM yy");
                        commentDate = q.DateAsked;
                    }
                    else
                    {
                        commentDate = getQuestionComment.DateCommented;
                    }
                    if (getAnswerComment == null)
                    {
                        model.LastParticipantOwner = userName.NameExtension;
                        model.LastParticipantDate  = q.DateAsked.ToString("d,MMM yy");
                        answerDate = q.DateAsked;
                    }
                    else
                    {
                        answerDate = getAnswerComment.DateAnswered;
                    }
                    if (getQuestionComment != null)
                    {
                        if (commentDate >= answerDate)
                        {
                            var commentOwner = _applicationDbContext.Users.Find(getQuestionComment.UserId);
                            model.LastParticipantOwner = commentOwner.NameExtension;
                            model.LastParticipantDate  = getQuestionComment.DateCommented.ToString("d,MMMM yy");
                        }
                    }
                    if (getAnswerComment != null)
                    {
                        if (commentDate <= answerDate)
                        {
                            var questionOwner = _applicationDbContext.Users.Find(getAnswerComment.UserId);
                            model.LastParticipantOwner = questionOwner.NameExtension;
                            model.LastParticipantDate  = getAnswerComment.DateAnswered.ToString("d,MMMM yy");
                        }
                    }

                    listModel.Add(model);
                }
            }

            HtmlToText convert = new HtmlToText();

            var topReport = _applicationDbContext.Reports
                            .OrderByDescending(dt => dt.DateCreated)
                            .Take(7);

            foreach (var top in topReport)
            {
                TopTenReport rmodel = new TopTenReport();
                rmodel.ReportText = convert.Convert(top.ReportText);
                rmodel.ReportId   = top.ReportId;
                rmodel.Title      = top.Title;
                rmodel.ReportDate = top.DateCreated;
                reportList.Add(rmodel);
            }

            var mostLikedQuestion = _applicationDbContext.QuestionLikes.ToList();
            var itemsWithTotals   = mostLikedQuestion
                                    .GroupBy(item => item.QuestionId)

                                    .Select(groupByQId => new
            {
                QuestionId = groupByQId.Key,
                Total      = groupByQId.Count()
            })
                                    .OrderByDescending(t => t.Total)
                                    .Take(7);

            foreach (var item in itemsWithTotals)
            {
                var getQuestion         = this._applicationDbContext.Questions.SingleOrDefault(id => id.QuestionId == item.QuestionId);
                MostLikeQuestion mModel = new MostLikeQuestion();
                mModel.QuestionId        = item.QuestionId;
                mModel.QuestionLikeCount = item.Total;
                mModel.QuestionTitle     = getQuestion.Title;
                mModel.QuestionText      = getQuestion.QuestionText;
                mModel.DateAsked         = getQuestion.DateAsked;
                likeList.Add(mModel);
            }

            var topSevenQuestion = _applicationDbContext.Questions
                                   .OrderByDescending(dt => dt.DateAsked)
                                   .Take(7);

            foreach (var qs in topSevenQuestion)
            {
                TopSevenQuestions qmodel = new TopSevenQuestions();
                qmodel.QuestionId    = qs.QuestionId;
                qmodel.QuestionTitle = qs.Title;
                qmodel.QuestionText  = convert.Convert(qs.QuestionText);
                qmodel.DateAsked     = qs.DateAsked;
                topQList.Add(qmodel);
            }

            int pageSize   = 50;
            int pageNumber = page ?? 1;

            ViewBag.DiscussionCategory  = new SelectList(_applicationDbContext.DiscussionCategories, "DiscussionCategoryId", "Name");
            mainModel.TopTenReports     = reportList;
            mainModel.TopSevenQuestions = topQList;
            mainModel.MostLikeQuestions = likeList;
            mainModel.QuestionListWithOwnerAndLastParticipantViewModels = listModel.ToPagedList <QuestionListWithOwnerAndLastParticipantViewModel>(pageNumber, pageSize);
            return(View(mainModel));
        }
Пример #2
0
        public ActionResult QuestionList(int q, string sort, string search, int?page)
        {
            QuestionAndReportViewModel mainModel = new QuestionAndReportViewModel();
            HtmlToText convert = new HtmlToText();

            ViewBag.NewestSort = sort == "newestsearch" ? "newest_desc" : "newest_search";
            ViewBag.OldestSort = sort == "oldestsearch" ? "oldest_asec" : "oldest_search";

            ViewBag.CurrentSort   = sort;
            ViewBag.CurrentSearch = search;

            var questModel = _applicationDbContext.Questions.Where(catId => catId.DiscussionCategoryId == q).Include(d => d.DiscussionCategory)
                             .OrderByDescending(dt => dt.DateAsked).ToList();

            var questList  = new List <QuestionListViewModel>();
            var reportList = new List <TopTenReport>();
            var topQList   = new List <TopSevenQuestions>();
            var likeList   = new List <MostLikeQuestion>();


            foreach (var quest in questModel)
            {
                if (!String.IsNullOrEmpty(search))
                {
                    if (quest.Title.ToLower().Contains(search) || quest.Title.ToLower().Contains(search) || quest.Tag.ToLower().Contains(search))
                    {
                        QuestionListViewModel model = new QuestionListViewModel();

                        var answers = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == quest.QuestionId).ToList();
                        model.QuestionId         = quest.QuestionId;
                        model.Title              = quest.Title;
                        model.DiscussionCategory = quest.DiscussionCategory.Name;
                        model.DateAsked          = quest.DateAsked.ToString("d,MMMM yy");

                        DateTime commentDate        = DateTime.UtcNow.Date;
                        DateTime answerDate         = DateTime.UtcNow.Date;
                        var      userName           = _applicationDbContext.Users.Find(quest.UserId);
                        var      getQuestionComment = _applicationDbContext.QuestionComments.Where(id => id.QuestionId == quest.QuestionId).OrderByDescending(p => p.QuestionCommentId).FirstOrDefault();
                        var      getAnswerComment   = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == quest.QuestionId).OrderByDescending(p => p.AnswerToQuestionId).FirstOrDefault();
                        model.QuestionOwner = userName.NameExtension;
                        if (answers != null)
                        {
                            model.AnswerCount = answers.Count;
                        }
                        else
                        {
                            model.AnswerCount = 0;
                        }

                        if (getQuestionComment == null)
                        {
                            model.LastParticipantOwner = userName.NameExtension;
                            model.LastParticipantDate  = quest.DateAsked.ToString("d,MMMM yy");
                            commentDate = quest.DateAsked;
                        }
                        else
                        {
                            commentDate = getQuestionComment.DateCommented;
                        }
                        if (getAnswerComment == null)
                        {
                            model.LastParticipantOwner = userName.NameExtension;
                            model.LastParticipantDate  = quest.DateAsked.ToString("d,MMM yy");
                            answerDate = quest.DateAsked;
                        }
                        else
                        {
                            answerDate = getAnswerComment.DateAnswered;
                        }
                        if (getQuestionComment != null)
                        {
                            if (commentDate >= answerDate)
                            {
                                var commentOwner = _applicationDbContext.Users.Find(getQuestionComment.UserId);
                                model.LastParticipantOwner = commentOwner.NameExtension;
                                model.LastParticipantDate  = getQuestionComment.DateCommented.ToString("d,MMMM yy");
                            }
                        }
                        if (getAnswerComment != null)
                        {
                            if (commentDate <= answerDate)
                            {
                                var questionOwner = _applicationDbContext.Users.Find(getAnswerComment.UserId);
                                model.LastParticipantOwner = questionOwner.NameExtension;
                                model.LastParticipantDate  = getAnswerComment.DateAnswered.ToString("d,MMMM yy");
                            }
                        }

                        questList.Add(model);
                        string title     = quest.DiscussionCategory.Description;
                        string PageTitle = Regex.Replace(title, "[^A-Za-z0-9]", "-");
                        ViewBag.PageTitle = PageTitle;
                    }
                    else
                    {
                    }
                }
                else
                {
                    QuestionListViewModel model = new QuestionListViewModel();

                    var answers = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == quest.QuestionId).ToList();
                    model.QuestionId         = quest.QuestionId;
                    model.Title              = quest.Title;
                    model.DiscussionCategory = quest.DiscussionCategory.Name;
                    model.DateAsked          = quest.DateAsked.ToString("d,MMMM yy");

                    DateTime commentDate        = DateTime.UtcNow.Date;
                    DateTime answerDate         = DateTime.UtcNow.Date;
                    var      userName           = _applicationDbContext.Users.Find(quest.UserId);
                    var      getQuestionComment = _applicationDbContext.QuestionComments.Where(id => id.QuestionId == quest.QuestionId).OrderByDescending(p => p.QuestionCommentId).FirstOrDefault();
                    var      getAnswerComment   = _applicationDbContext.AnswerToQuestions.Where(id => id.QuestionId == quest.QuestionId).OrderByDescending(p => p.AnswerToQuestionId).FirstOrDefault();
                    model.QuestionOwner = userName.NameExtension;
                    if (answers != null)
                    {
                        model.AnswerCount = answers.Count;
                    }
                    else
                    {
                        model.AnswerCount = 0;
                    }

                    if (getQuestionComment == null)
                    {
                        model.LastParticipantOwner = userName.NameExtension;
                        model.LastParticipantDate  = quest.DateAsked.ToString("d,MMMM yy");
                        commentDate = quest.DateAsked;
                    }
                    else
                    {
                        commentDate = getQuestionComment.DateCommented;
                    }
                    if (getAnswerComment == null)
                    {
                        model.LastParticipantOwner = userName.NameExtension;
                        model.LastParticipantDate  = quest.DateAsked.ToString("d,MMM yy");
                        answerDate = quest.DateAsked;
                    }
                    else
                    {
                        answerDate = getAnswerComment.DateAnswered;
                    }
                    if (getQuestionComment != null)
                    {
                        if (commentDate >= answerDate)
                        {
                            var commentOwner = _applicationDbContext.Users.Find(getQuestionComment.UserId);
                            model.LastParticipantOwner = commentOwner.NameExtension;
                            model.LastParticipantDate  = getQuestionComment.DateCommented.ToString("d,MMMM yy");
                        }
                    }
                    if (getAnswerComment != null)
                    {
                        if (commentDate <= answerDate)
                        {
                            var questionOwner = _applicationDbContext.Users.Find(getAnswerComment.UserId);
                            model.LastParticipantOwner = questionOwner.NameExtension;
                            model.LastParticipantDate  = getAnswerComment.DateAnswered.ToString("d,MMMM yy");
                        }
                    }

                    questList.Add(model);
                    string title     = quest.DiscussionCategory.Description;
                    string PageTitle = Regex.Replace(title, "[^A-Za-z0-9]", "-");
                    ViewBag.PageTitle = PageTitle;
                }
            }
            if (questList.Count == 0)
            {
                mainModel.QuestionCount = 0;
            }
            if (questList.Count > 0)
            {
                mainModel.QuestionCount = 1;
            }
            var CategoryName = _applicationDbContext.DiscussionCategories.SingleOrDefault(d => d.DiscussionCategoryId == q);


            var topReport = _applicationDbContext.Reports
                            .OrderByDescending(dt => dt.DateCreated)
                            .Take(7);

            foreach (var top in topReport)
            {
                TopTenReport rmodel = new TopTenReport();
                rmodel.ReportText = convert.Convert(top.ReportText);
                rmodel.ReportId   = top.ReportId;
                rmodel.Title      = top.Title;
                rmodel.ReportDate = top.DateCreated;
                reportList.Add(rmodel);
            }

            var mostLikedQuestion = _applicationDbContext.QuestionLikes.ToList();
            var itemsWithTotals   = mostLikedQuestion
                                    .GroupBy(item => item.QuestionId)

                                    .Select(groupByQId => new
            {
                QuestionId = groupByQId.Key,
                Total      = groupByQId.Count()
            })
                                    .OrderByDescending(t => t.Total)
                                    .Take(7);

            foreach (var item in itemsWithTotals)
            {
                var getQuestion         = this._applicationDbContext.Questions.SingleOrDefault(id => id.QuestionId == item.QuestionId);
                MostLikeQuestion mModel = new MostLikeQuestion();
                mModel.QuestionId        = item.QuestionId;
                mModel.QuestionLikeCount = item.Total;
                mModel.QuestionTitle     = getQuestion.Title;
                mModel.QuestionText      = getQuestion.QuestionText;
                mModel.DateAsked         = getQuestion.DateAsked;
                likeList.Add(mModel);
            }

            var topSevenQuestion = _applicationDbContext.Questions
                                   .OrderByDescending(dt => dt.DateAsked)
                                   .Take(7);

            foreach (var qs in topSevenQuestion)
            {
                TopSevenQuestions qmodel = new TopSevenQuestions();
                qmodel.QuestionId    = qs.QuestionId;
                qmodel.QuestionTitle = qs.Title;
                qmodel.QuestionText  = convert.Convert(qs.QuestionText);
                topQList.Add(qmodel);
            }
            int pageSize   = 50;
            int pageNumber = page ?? 1;

            ViewBag.Category = CategoryName.Name;
            ViewBag.q        = q;

            mainModel.TopTenReports          = reportList;
            mainModel.TopSevenQuestions      = topQList;
            mainModel.MostLikeQuestions      = likeList;
            mainModel.QuestionListViewModels = questList.ToPagedList <QuestionListViewModel>(pageNumber, pageSize);
            return(View(mainModel));
        }