Example #1
0
        public IActionResult Index()
        {
            var sessionId = Logic.Logic.GetSessionId(Request);
            var username  = _sessionDao.GetUsername(sessionId);
            var isLogin   = username != null;
            var questions = _questionDao.GetLastQuestions();
            var ans       = new List <Dictionary <string, object> >();

            foreach (var question in questions)
            {
                var tags         = new List <Tag>();
                var tagQuestions = _tagQuestionDao.GetTagsOfQuestion(question.Id);
                foreach (var tagQuestion in tagQuestions)
                {
                    var tag = _tagDao.GetTagById(tagQuestion.TagId);
                    tags.Add(tag);
                }

                var likeCount = _likeQuestionDao.GetLikeCount(question.Id);
                var dic       = new Dictionary <string, object>
                {
                    ["title"]  = question.Title, ["answerCount"] = _answerDao.GetAnswerCount(question.Id)
                    , ["tags"] = tags, ["id"] = question.Id, ["likeCount"] = likeCount
                };

                ans.Add(dic);
            }

            ViewData["ans"]     = ans;
            ViewData["isLogin"] = isLogin;
            return(View());
        }
        public IActionResult Index(int id)
        {
            HttpContext.Session.SetString("init", "0");
//            HttpContext.Session.SetString("test", "test session");
            var sid       = HttpContext.Session.Id;
            var sessionId = Logic.Logic.GetSessionId(Request);
            var username  = _sessionDao.GetUsername(sessionId);
            var isLogin   = username != null;

            //my code
            var question          = (Question)_questionDao.GetQuestionById(id);
            var questionLikeCount = (int)_likequestionDao.GetLikeCount(id);
            var tagQuestion       = (List <TagQuestion>)_tagquestionDao.GetTagsOfQuestion(id);
            var tags             = new List <Tag>();
            var answers          = (List <Answer>)_answerDao.GetAnswers(question.Id);
            var answerCount      = (int)_answerDao.GetAnswerCount(question.Id);
            var questionComments = (List <QuestionComment>)_questioncommentDao.GetCommentsOfQuestion(id);

            foreach (var tq in tagQuestion)
            {
                var tag = _tagDao.GetTagById(tq.TagId);
                tags.Add(tag);
            }

            ViewData["questionTitle"]     = question.Title;
            ViewData["questionContent"]   = question.Content;
            ViewData["questionDay"]       = question.Day;
            ViewData["questionHour"]      = question.Hour;
            ViewData["questionUserId"]    = question.UserId;
            ViewData["questionId"]        = question.Id;
            ViewData["questionLikeCount"] = questionLikeCount;
            ViewData["tags"]              = tags;
            ViewData["answers"]           = answers;
            ViewData["answer_count"]      = answerCount;
            ViewData["question_comments"] = questionComments;
            var ans = new List <Dictionary <string, object> >();

            foreach (var answer in answers)
            {
                if (answer != null)
                {
                    var answerComments  = (List <AnswerComment>)_answercommentDao.GetCommentsOfanswer(answer.Id);
                    var answerLikeCount = (int)_likeanswerDao.GetLikeCountAnswer(answer.Id);

                    var dic = new Dictionary <string, object>
                    {
                        ["answerContent"]    = answer.Content,
                        ["answerDay"]        = answer.Day,
                        ["answerHour"]       = answer.Hour,
                        ["answerUserId"]     = answer.UserId,
                        ["answerId"]         = answer.Id,
                        ["answerquestionId"] = answer.QuestionId,
                        ["answerLikeCount"]  = answerLikeCount,
                        ["answer_comments"]  = answerComments
                    };
                    ans.Add(dic);
                }
            }
            ViewData["ans"] = ans;

            var _sessionId = Logic.Logic.GetSessionId(Request);
            //var sessionId = HttpContext.Session.Id;
            var userName = (string)_sessionDao.GetUsername(_sessionId);
            var user     = (User)_userDao.GetUserByUsername(userName);

            ViewData["user"] = user;
            var content    = Logic.Logic.GetValue(Request, "content");
            var yourAnswer = Logic.Logic.GetValue(Request, "yourAnswer");

            if (yourAnswer != null)
            {
                var createdanswer = (int)_answerDao.CreateAnswer(yourAnswer, user.Id, question.Id);
            }
            if (Request.Method == "POST")
            {
                ViewData["error"] = "Error";
            }
            ViewData["isLogin"] = isLogin;
            ViewData["id"]      = sid;
            return(View());
        }