public ActionResult AddAnswer(GetQuestionModels data, string questionID)
        {
            //Id of the actual question
            int actid = Int32.Parse(questionID);
            var manager = new AnswerManager();
            int userid = 0;
            var model_state = ModelState.IsValid;
            if (Request.IsAuthenticated)
                userid = WebSecurity.CurrentUserId;
            else
                model_state = false;
            if (model_state)
            {
                manager.AddAnswer(data.ActualAnswer, actid, userid);
                return RedirectToAction("GetQuestion", new { id = actid });
            }

            return RedirectToAction("Index");
        }
        /// <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));

            }
        }
        /// <summary>
        /// Shows the page of a user
        /// </summary>
        /// <param name="id">User id</param>
        /// <returns></returns>
        public ActionResult GetUser(int id, int? questionpage, int? answerpage)
        {
            var usermanager = new UserManager();
            var questionmanager = new QuestionManager();
            var answermanager = new AnswerManager();
            var tagmanager = new TagManager();
            var ucm = new UserControllerModel();

            //If the pages are not given, set it to 1
            var actquestionpage = questionpage ?? 1;
            var actanswerpage = answerpage ?? 1;

            //Get the questions for the actual page
            int totalquestions;
            var retrievedQuestions = questionmanager.AllQuestionToOneUserToPagedList(id, actquestionpage, 5, out totalquestions);
            //Convert it to a StaticPagedList for the pager
            var actualQuestions = new StaticPagedList<Question>(retrievedQuestions, actquestionpage, 5, totalquestions);

            //Add it to the model
            ucm.Questions = actualQuestions;

            //Set the actual pagenumbers
            ViewBag.QuestionPage = actquestionpage;
            ViewBag.AnswerPage = actanswerpage;

            //Add the actual user to the model
            ucm.UserProfile = usermanager.GetUserById(id);

            //Get the answered questions by the user for the actual page
            int totalanswers;
            var retrievedAnsweredQuestions = questionmanager.AnsweredQuestionsToPagedList(id, actanswerpage, 5, out totalanswers);
            //Convert it to a StaticPagedList for the pager
            var actualAnsweredQuestions = new StaticPagedList<Question>(retrievedAnsweredQuestions, actanswerpage, 5, totalanswers);

            //Add it to the model
            ViewBag.AnsweredQuestions = actualAnsweredQuestions;

            //Add the reputation of the user to the model
            ucm.UserRating = usermanager.GetUserRating(id);

            //Create a list with the answered questions
            var answeredquestions = new List<GetQuestionModels>();
            foreach (var item in actualAnsweredQuestions)
            {

                var actansweredquestion = new GetQuestionModels();
                //Add the current question to the model
                actansweredquestion.CurrentQuestion = item;
                //Add the answers by the user of the current question to the model
                actansweredquestion.Answers = answermanager.GetAllAnswerToOneQuestionFromOneUser(item.Id, ucm.UserProfile.UserId);
                //Add the vote of current question to the model
                actansweredquestion.Vote = questionmanager.GetVote(item.Id);
                //Add the tags of the current question to the model
                actansweredquestion.QuestionTags = tagmanager.GetAllTagToOneQuestion(item.Id);

                //Create a dictionary for the answers and the votes of them
                actansweredquestion.AnswerVotes = new Dictionary<Answer, int>();

                //Upload the dictionary with the votes
                foreach (var answer in actansweredquestion.Answers)
                {
                    actansweredquestion.AnswerVotes.Add(answer, answermanager.GetVote(answer.Id));
                }

                //Add the author of the question to the model
                actansweredquestion.QuestionUser = usermanager.GetUserById(item.UserId);

                //Sort the answers by the votes of them
                actansweredquestion.Answers = actansweredquestion.Answers.OrderByDescending(d => actansweredquestion.AnswerVotes[d]).ToList();

                //Add this QuestionModel to the list
                answeredquestions.Add(actansweredquestion);
            }

            //Add the list of the questionmodels to the model
            ucm.AnsweredQuesitions = answeredquestions;

            return View(ucm);
        }
        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);
        }
 public ActionResult EditAnswer(Answer data, string AnsId)
 {
     data.Id = Int32.Parse(AnsId);
     data.UserId = WebSecurity.CurrentUserId;
     var manager = new AnswerManager();
     if (ModelState.IsValid)
     {
         manager.EditAnswer(data);
     }
     return RedirectToAction("Index");
 }
        public ActionResult EditAnswer(string actId)
        {
            int actualId = Int32.Parse(actId);

            var manager = new AnswerManager();
            var model = manager.GetAnswer(actualId);
            //If the user is authorized to edit this answer, it returns the edit page
            if (model.UserId == WebSecurity.CurrentUserId)
            {
                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }