Exemplo n.º 1
0
 public ViewAnswerModel(Answer answer, ViewQuestionModel question = null)
     : base(answer)
 {
     if (question == null)
     {
         this.Question = new ViewQuestionModel(answer.Question);
     }
     else
     {
         this.Question = question;
     }
     this.Accepted = answer.Accepted;
 }
Exemplo n.º 2
0
        public ActionResult Create(AnswerModel answer)
        {
            User user = ControllerHelper.GetAuthenticatedUser(dataContext);
            if (user != null && answer != null && ModelState.IsValid)
            {
                Question question = dataContext.Questions.SingleOrDefault(a => a.Id == answer.QuestionId.Value);
                if (question != null && question.Answers.All(a => a.Author.OpenId != user.OpenId))
                {
                    Answer a = new Answer(user, answer.Body, question);

                    dataContext.Answers.Add(a);
                    dataContext.SaveChanges();
                    return RedirectToAction("Question", "Questions", new { id = answer.QuestionId });
                }
                else
                {
                    Answer a = dataContext.Answers.SingleOrDefault(ans => ans.Author.OpenId == user.OpenId);
                    if (a != null)
                    {
                        //Apply the edit
                        a.SetBody(answer.Body, user);
                        dataContext.SaveChanges();
                    }
                }
            }
            return ControllerHelper.RedirectBack(Request, Redirect, true);
        }