public virtual Question GetQuestion(ViewQuestion viewQuestion) { Question mappedQuestion = _questionsRepository.GetById(viewQuestion.Id); mappedQuestion.SourceCode = viewQuestion.SourceCode; return(mappedQuestion); }
public IActionResult Test(int id) { ViewBag.TestId = id; List <TestQuestion> testQuestions = db.TestQuestions.Where(t => t.TestId == id).ToList(); List <int> questionsId = testQuestions.Select(t => t.QuestionId).ToList(); List <Question> questions = db.Questions.Where(t => questionsId.Contains(t.Id)).ToList(); List <ViewQuestion> viewQuestions = new List <ViewQuestion>(); foreach (var i in questions) { ViewQuestion viewQuestion = new ViewQuestion(); viewQuestion.Id = i.Id; viewQuestion.Name = i.Name; viewQuestion.QuestionContent = i.QuestionContent; List <string> answers = new List <string>(); answers.Add(i.CorrectAnswer); answers.Add(i.WrongAnswer1); answers.Add(i.WrongAnswer2); answers.Add(i.WrongAnswer3); viewQuestion.Answers.AddRange(answers.OrderBy(x => RandomClass.Rnd.Next())); viewQuestions.Add(viewQuestion); } return(View(viewQuestions)); }
public void GetViewQuestion_MapsTextQuestionProperty() { var mockQuestion = MockRepository.GenerateMock <Question>(); mockQuestion.TextQuestion = "textQuestion"; var viewQuestionMapper = new ViewQuestionMapper(); ViewQuestion result = viewQuestionMapper.GetViewQuestion(mockQuestion); Assert.AreEqual(mockQuestion.TextQuestion, result.TextQuestion); }
public void GetViewQuestion_MapsIdProperty() { var mockQuestion = MockRepository.GenerateMock <Question>(); mockQuestion.Id = 1; var viewQuestionMapper = new ViewQuestionMapper(); ViewQuestion result = viewQuestionMapper.GetViewQuestion(mockQuestion); Assert.AreEqual(mockQuestion.Id, result.Id); }
public async Task <ActionResult <ViewQuestion> > UpdQuestion(int id, [FromBody] ViewQuestion value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (var transaction = ctx.Database.BeginTransaction()) { try { var question = new VtQuestion { Id = value.Id, Question = value.Question, ImgName = value.ImgName, ImgSource = value.ImgSource, Answer = value.Answer, CreateBy = value.CreateBy, CreateDate = DateTime.Now }; ctx.VtQuestion.Update(question); await ctx.SaveChangesAsync(); var choices = value.Choice; choices.ForEach(item => { var c = new VtChoice { Id = item.Id, Choice = item.Choice, IdQuestion = question.Id, ImgName = item.ImgName, ImgSource = item.ImgSource, CreateBy = question.CreateBy, CreateDate = DateTime.Now }; ctx.VtChoice.Update(c); }); await ctx.SaveChangesAsync(); transaction.Commit(); return(CreatedAtAction(nameof(GetQuestionByCon), new { id = value.Id }, value)); } catch (System.Exception) { transaction.Rollback(); return(StatusCode(500)); } } }
public ViewQuestion PostQuestion([FromBody] Question question) { if (!ModelState.IsValid) { throw new Exception("No valido"); } db.Question.Add(question); db.SaveChanges(); ViewQuestion viewQuestion = question; return(viewQuestion); }
public ActionResult Post(int?page) { //是否需要回答问题后才能查看内容 string PostQuestion = ConfigurationManager.AppSettings["PostQuestion"]; ViewBag.PostQuestion = PostQuestion; //若是管理员登录,则默认不需要回答问题 if (Session["username"] != null) { ViewBag.PostQuestion = "false"; } //若需要回答问题 if (PostQuestion == "true") { string jsonFile = Server.MapPath("~/Files/") + "questions.json"; Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader file = System.IO.File.OpenText(jsonFile); JsonTextReader reader = new JsonTextReader(file); JObject obj = (JObject)JToken.ReadFrom(reader); file.Close(); //防止占用文件 var list = obj["questions"].ToString(); JArray ja = (JArray)JsonConvert.DeserializeObject(list); var selected = GetRandom(0, ja.Count, 3); List <ViewQuestion> vqlist = new List <ViewQuestion>(); foreach (var num in selected) { ViewQuestion vq = new ViewQuestion(); vq.Content = ja[num]["content"].ToString(); vq.Answer = ja[num]["answer"].ToString(); vqlist.Add(vq); } ViewBag.vqlist = vqlist; } System.Linq.IQueryable <PersonalWebsite.Models.Post> post; //进行查询 post = from p in db4.Post select p; //分页显示 post = post.OrderByDescending(p => p.PostID); const int pageItems = 10; int currentPage = (page ?? 1); IPagedList <Post> pagePost = post.ToPagedList(currentPage, pageItems); ViewPost vpost = new ViewPost(); vpost.PostList = pagePost; return(View(vpost)); }
public void GetViewQuestion_MapsSourceCodeProperty() { var question = new Question(new Definition { ClassName = "stubClass", MethodName = "stubMethod", ReturnValue = "stubReturnValue" }); var viewQuestionMapper = new ViewQuestionMapper(); ViewQuestion result = viewQuestionMapper.GetViewQuestion(question); Assert.AreEqual(question.GetContractDefinitionSourceCode(), result.SourceCode); }
public ActionResult Resolve(ViewQuestion viewQuestion) { Question questionWithAnswer = _viewQuestionMapper.GetQuestion(viewQuestion); var correctionResult = _correctionProcess.CheckAnswer(questionWithAnswer); if (correctionResult.HasErrors()) { foreach (var error in correctionResult.Errors) { ModelState.AddModelError("", error); } } else { return(RedirectToAction("Result")); } return(View(viewQuestion)); }
public ActionResult View(int id, string shortName) { // Get question from database. var question = Current.DB.Questions.Get(id); // Question not found. if (question == null) { return(NotFound()); } // Session key. string passwordKey = "Question:" + id; // Question has a password and the session doesn't exist. if (question.Password.HasValue()) { if (Session[passwordKey] == null) { return(RedirectToAction("password", new { id = id, shortName = shortName })); } } // Increment view count for question. question.Views = question.Views + 1; // Update question. Current.DB.Questions.Update(id, new { question.Views }); var md = new Markdown(); question.Answer = md.Transform(question.Answer); var vq = new ViewQuestion { Question = question, User = Current.DB.Users.Get(question.OwnerUserID) }; return(View(vq)); }
public ActionResult View(int id, string shortName) { // Get question from database. var question = Current.DB.Questions.Get(id); // Question not found. if (question == null) { return NotFound(); } // Session key. string passwordKey = "Question:" + id; // Question has a password and the session doesn't exist. if (question.Password.HasValue()) { if (Session[passwordKey] == null) { return RedirectToAction("password", new { id = id, shortName = shortName }); } } // Increment view count for question. question.Views = question.Views + 1; // Update question. Current.DB.Questions.Update(id, new { question.Views }); var md = new Markdown(); question.Answer = md.Transform(question.Answer); var vq = new ViewQuestion { Question = question, User = Current.DB.Users.Get(question.OwnerUserID) }; return View(vq); }