public JsonResult AddNewQuestion(NewQuestionModel model) { var questionAdded = new QuestionAddedViewModel(); questionAdded.Code = -1; try { var username = User.Identity.Name; var user = _unitOfWork.UserRepository.GetByUsername(username); if (user == null) return Json(false, JsonRequestBehavior.AllowGet); var newQuestion = new Question(model.Question, model.Options, user); _unitOfWork.QuestionRepository.Add(newQuestion); _unitOfWork.Save(); questionAdded.Code = 1; questionAdded.Question = new QuestionViewModel(newQuestion); questionAdded.UserPoints = user.Points; return Json(questionAdded, JsonRequestBehavior.AllowGet); } catch (NotEnoughPointsException ex) { throw; } }
public void Add(Question question) { _context.Questions.Add(question); }
public QuestionViewModel(Question question) { Id = question.Id; Question = question.Text; Options = question.Options.Select(option => new OptionViewModel(option)).ToList(); }