示例#1
0
        public ActionResult GetQuestion(int id)
        {
            //Manager objects
            var manager = new QuestionManager();
            var ansman = new AnswerManager();
            var usman = new UserManager();
            var tagman = new TagManager();

            var actQues = manager.GetQuestion(id);
            var model = new GetQuestionModels();
            model.CurrentQuestion = actQues;

            //We gets the answers which belong to this question
            model.Answers = ansman.GetAllAnswerToOneQuestion(id);
            //We gets the vote of the question
            model.Vote = manager.GetVote(model.CurrentQuestion.Id);
            //We lists the tags of the question
            model.QuestionTags = tagman.GetAllTagToOneQuestion(id);

            //We gets the votes of the answers
            Dictionary<Answer, int> ansvotes = new Dictionary<Answer, int>();
            Dictionary<Answer, UserProfile> answeruser = new Dictionary<Answer, UserProfile>();
            foreach (var item in model.Answers)
            {
                int vote = ansman.GetVote(item.Id);
                UserProfile userprof = usman.GetUserById(item.UserId);
                answeruser.Add(item, userprof);
                ansvotes.Add(item, vote);
            }
            //We creates the viewmodel
            model.AnswerVotes = ansvotes;
            model.AnswerUser = answeruser;
            model.QuestionUser = usman.GetUserById(model.CurrentQuestion.UserId);
            model.Answers = model.Answers.OrderByDescending(d => model.AnswerVotes[d]).ToList();
            return View(model);
        }
示例#2
0
        /// <summary>
        /// Votes for a questions
        /// </summary>
        /// <param name="questionid">Question Id</param>
        /// <param name="vote">Vote: +1 or -1</param>
        /// <returns></returns>
        public ActionResult VoteForQuestion(int questionid, int vote)
        {
            if (!Request.IsAuthenticated)
            {
                //If the user is not logged in, we create an error message
                return Json(Resources.Global.YouHaveToLoginBeforeVote);
            }
            else
            {
                //We check if the user already voted for this question
                var vman = new VoteManager();
                bool isvoted = vman.IsVotedForQuestion(questionid, WebSecurity.GetUserId(User.Identity.Name));
                if (isvoted)
                {
                    return Json(Resources.Global.YouAlreadyVotedForThisQuestion);
                }
                //We check if the user want to vote for his own question
                var qman = new QuestionManager();
                if (qman.GetQuestion(questionid).UserId == WebSecurity.GetUserId(User.Identity.Name))
                {
                    return Json(Resources.Global.YouCantVoteForYourOwnQuestion);
                }
                //We store the vote
                vman.Vote(questionid, WebSecurity.GetUserId(User.Identity.Name), vote);

                //We returns the new vote of the question
                return Json(qman.GetVote(questionid));

            }
        }
示例#3
0
        public ActionResult EditQuestion(string actId)
        {
            var manager = new QuestionManager();
            int actualId = Int32.Parse(actId);
            //We get the actual question
            var model = manager.GetQuestion(actualId);

            if (model.UserId == WebSecurity.CurrentUserId)
            {
                return View(model);
            }
            //If the user not eligble to edit the question, we returns to home page
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }