Пример #1
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));

            }
        }
Пример #2
0
        /// <summary>
        /// Votes for answers
        /// </summary>
        /// <param name="answerid">Answer Id</param>
        /// <param name="vote">Vote</param>
        /// <returns></returns>
        public ActionResult VoteForAnswer(int answerid, int vote)
        {
            if (!Request.IsAuthenticated)
            {
                //Check if the user logged in
                return Json(Resources.Global.YouHaveToLoginBeforeVote);
            }
            else
            {
                var vman = new VoteManager();
                //Check if the user already voted for this answer
                bool isVoted = vman.IsVotedForAnswer(answerid, WebSecurity.GetUserId(User.Identity.Name));
                if (isVoted)
                {
                    return Json(Resources.Global.YouAlreadyVotedForThisAnswer);
                }

                //Check if the user want to vote for his own question
                var aman = new AnswerManager();
                if (aman.GetAnswer(answerid).UserId == WebSecurity.GetUserId(User.Identity.Name))
                {
                    return Json(Resources.Global.YouCantVoteForYourOwnAnswer);
                }
                //Store the vote
                vman.VoteAnswer(answerid, WebSecurity.GetUserId(User.Identity.Name), vote);

                return Json(aman.GetVote(answerid));

            }
        }