Beispiel #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());
        }
Beispiel #2
0
        public IActionResult Index()
        {
            ViewData["isLogin"] = _sessionDao.GetUsername(Logic.Logic.GetSessionId(Request)) != null;
            var sessionId = Logic.Logic.GetSessionId(Request);
            var title     = Logic.Logic.GetValue(Request, "title");
            var content   = Logic.Logic.GetValue(Request, "body");
            var tags      = Logic.Logic.GetValue(Request, "tags");

            var username = _sessionDao.GetUsername(sessionId);
            var user     = (User)_userDao.GetUserByUsername(username);

            if (user == null)
            {
                return(RedirectToAction("Index", "Login"));
            }
            if (title != null && content != null && tags != null)
            {
                var ts         = tags.Split(' ');
                var questionId = _questionDao.CreateQuestion(title, content, user.Id);


                foreach (string tagtmp in ts)
                {
                    if (_tagDao.GetTag(tagtmp) == null)
                    {
                        //create tag
                        _tagDao.CreatTag(tagtmp);
                    }
                    //get tagId
                    var tag   = (Tag)_tagDao.GetTag(tagtmp);
                    var tagId = tag.Id;

                    //create question tag
                    _tagQuestionDao.CreateTagQuestion(questionId, tagId);
                }
            }
            else if (Request.Method == "POST")
            {
                ViewData["errorAddQuestion"] = "title and body are requirement";
            }



            return(View());
        }
        public IActionResult Index()
        {
            string error     = null;
            string message   = null;
            var    query     = Logic.Logic.GetValue(Request, "islike", "");
            var    answerId  = int.Parse(Logic.Logic.GetValue(Request, "answerId", ""));
            var    sessionId = Logic.Logic.GetSessionId(Request);

            var answer   = (Answer)_answerDAO.GetAnswerById(answerId);
            int id       = 0;
            var userName = (string)_sessionDAO.GetUsername(sessionId);
            var user     = (User)_userDAO.GetUserByUsername(userName);

            if (user != null)
            {
                if ((answer.UserId != user.Id) && !(_likeAnswerDAO.beforeLikedByThisUser(answerId, user.Id)))
                {
                    if (query == "1")
                    {
                        message = "liked";
                        id      = _likeAnswerDAO.likeTheAnswer(answerId, user.Id, true);
                    }
                    else if (query == "0")
                    {
                        message = "disliked";
                        id      = _likeAnswerDAO.likeTheAnswer(answerId, user.Id, false);
                    }
                    else
                    {
                        error = "query is wrong";
                    }
                }
                else
                {
                    error = "you cannot like this";
                }
            }
            else
            {
                error = "you are not login";
            }

            var result = new Dictionary <string, object> {
                ["message"] = message, ["error"] = error
            };

            return(Json(result));
        }
        public IActionResult Index()
        {
            ViewData["isLogin"] = _sessionDao.GetUsername(Logic.Logic.GetSessionId(Request)) != null;
            var username = Logic.Logic.GetValue(Request, "username");
            var password = Logic.Logic.GetValue(Request, "password");
            var usr      = (User)_userDao.GetUserByUsernameAndPassword(username, password);

            if (usr != null)
            {
                _sessionDao.Login(Logic.Logic.GetSessionId(Request), username);
                return(RedirectToAction("Index", "Home"));
            }
            else if (Request.Method == "POST")
            {
                ViewData["error"] = "your password or username is incorrect";
            }

            return(View());
        }
Beispiel #5
0
        public IActionResult Index()
        {
            var sessionId = Logic.Logic.GetSessionId(Request);
            var username  = _sessionDAO.GetUsername(sessionId);

            if (username != null)
            {
                var user = _userDAO.GetUserByUsername(username);
                if (user != null)
                {
                    var questions = _questionDAO.GetQuestionsOfUser(user.Id);
                    var answers   = _answerDAO.GetAnswersOfUser(user.Id);
                    var dic       = new Dictionary <string, object>
                    {
                        ["id"]       = user.Id,
                        ["name"]     = user.Name,
                        ["password"] = user.Password,
                        ["username"] = user.Username,
                        ["email"]    = user.Email
                    };
                    if (questions != null)
                    {
                        dic["questions"] = questions;
                    }
                    if (answers != null)
                    {
                        dic["answers"] = answers;
                    }
                    ViewData["dataOfUser"] = dic;
                }
                else
                {
                    return(RedirectToAction("index", "Login"));
                }
            }
            else
            {
                return(RedirectToAction("index", "Login"));
            }
            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());
        }