public async Task UpdateQuestionAsync(string questionId, string userId) { var newRequest = new QuestionRequestModel { DifficultyLevelId = "ce705f44-07e0-45c6-b51d-3b1af6256848", ShuffleOptions = true, ScoreValue = 10, SubjectId = "f4eb2f0a-ef7f-4d16-abc2-dfabf6b660c0", QuestionType = "SingleChoice", Text = "Test Question", Options = new List <QuestionOption> { new QuestionOption { IsAnswer = true, Text = "Option text" }, new QuestionOption { IsAnswer = false, Text = "Option text" } }, }; _questionRepoMock.Setup(u => u.UpdateQuestionAsync(questionId, newRequest, userId)) .Returns(() => Task.FromResult(questionResponse)); var response = await _questionManager.UpdateQuestionAsync(questionId, newRequest, userId); _questionValidator.Verify(u => u.ValidateQuestion(It.IsAny <QuestionRequestModel>(), out validatorMessage), Times.Once); Assert.NotNull(response.Id); }
public static Question Map(this QuestionRequestModel model, string userId, string questionId = "") { if (model == null) { return(null); } Question question = new Question { ScoreValue = model.ScoreValue.Value, Text = model.Text, QuestionType = model.QuestionType, ShuffleOptions = model.ShuffleOptions.Value, SubjectId = model.SubjectId, DifficultyLevelId = model.DifficultyLevelId, UserId = userId, Options = model.Options.Select(u => new Option { IsAnswer = u.IsAnswer.Value, Text = u.Text }).ToList() }; if (!string.IsNullOrEmpty(questionId)) { question.Id = questionId; } return(question); }
private bool IsModelValid(QuestionRequestModel model) { if (model == null) { return(false); } if (string.IsNullOrEmpty(model.Category)) { return(false); } if (string.IsNullOrEmpty(model.Text)) { return(false); } if (string.IsNullOrEmpty(model.Difficulty)) { return(false); } if (model.CorrectAnswers == null || model.CorrectAnswers.Count() < 1) { return(false); } if (model.WrongAnswers == null || model.WrongAnswers.Count() < 1) { return(false); } return(true); }
public void QuestionBcTests_GetAllQuestions() { var random = TestUtils.RandomString(10); var question = new QuestionRequestModel { Question = $"Random question {random} with choice (GetAll)", ImageUrl = "https://www.google.pt/search?q=image", ThumbUrl = "https://www.google.pt/search?q=thumb", Choices = new[] { $"choice1_{random}", $"choice2_{random}", $"choice3_{random}" } }; questionBc.CreateQuestion(question); var search = new BaseSearchModel { Limit = 1, Offset = 0, Filter = random }; var result = questionBc.GetSearchQuery(search); Assert.IsTrue(result.Count() >= 1); var choices = result.FirstOrDefault().Choices; Assert.IsNotNull(choices); Assert.IsTrue(choices.Count() > 1); }
public QuestionResponseModel AddQuestion(QuestionRequestModel model) { this.SetCorrectContentType(); if (!this.IsModelValid(model)) { throw new WebFaultException(HttpStatusCode.BadRequest); } var question = new Question(); question.Text = model.Text; question.Category = this.LoadOrCreateCategory(model.Category); question.DifficultyLevel = (DifficultyLevel)Enum.Parse(typeof(DifficultyLevel), model.Difficulty); question.Answers = this.GetAnswersFrom(model); this.data.Get <Question>() .Add(question); this.data.SaveChanges(); WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Created; return(QuestionResponseModel.FromQuestion .Compile().Invoke(question)); }
public QuestionResponseModel CreateQuestion(QuestionRequestModel requestModel) { var newQuestion = new Question { QuestionDescription = requestModel.Question, ImageUrl = requestModel.ImageUrl, ThumbUrl = requestModel.ThumbUrl, PublishedAt = DateTime.Now }; foreach (var choice in requestModel.Choices.Distinct()) // Distinct - If there are choices with same name { newQuestion.QuestionChoices.Add(new QuestionChoice { Name = choice, Votes = 0 }); } var dbQuestion = questionRepo.Add(newQuestion); questionRepo.Save(); return(MapToQuestionModel(dbQuestion)); }
public bool ValidateQuestion(QuestionRequestModel model, out string message) { bool success = false; success = ValidateQuestionType(model, out message); if (!success) { return(false); } success = ValidateNumberOfOptions(model, out message); if (!success) { return(false); } success = ValidateQuestionOptions(model, out message); if (!success) { return(false); } return(true); }
public async Task <IActionResult> UpdateQuestion(string id, QuestionRequestModel model) { var userId = User.Claims.First(u => u.Type.Equals("sub")).Value; var newQuestion = await _questionManager.UpdateQuestionAsync(id, model, userId); var response = new ResponseModel <QuestionResponseModel>(newQuestion, true, "Question created successfully"); return(Ok(response)); }
public async Task <QuestionResponseModel> CreateQuestionAsync(QuestionRequestModel model, string userId) { var question = model.Map(userId); _dbContext.Add(question); await _dbContext.SaveChangesAsync(); return(await GetQuestionAsync(question.Id)); }
public async Task CreateQuestion(QuestionRequestModel question) { _questionRepoMock.Setup(u => u.CreateQuestionAsync(It.IsAny <QuestionRequestModel>(), It.IsAny <string>())) .Returns(() => Task.FromResult(questionResponse)); var response = await _questionManager.CreateQuestionAsync(question, "333"); _questionRepoMock.Verify(u => u.CreateQuestionAsync(It.IsAny <QuestionRequestModel>(), It.IsAny <string>()), Times.Once); Assert.NotNull(response.Id); }
public IHttpActionResult Post(QuestionRequestModel model) { var newQuestions = Mapper.Map <Question>(model); var id = this.questions.AddNew(newQuestions, this.User.Identity.GetUserId()); var result = this.questions .GetById(id) .ProjectTo <ListedQuestionResponseModel>() .FirstOrDefault(); return(this.Created($"/api/Questions/{id}", result)); }
public IHttpActionResult Post(QuestionRequestModel model) { var newQuestions = Mapper.Map<Question>(model); var id = this.questions.AddNew(newQuestions, this.User.Identity.GetUserId()); var result = this.questions .GetById(id) .ProjectTo<ListedQuestionResponseModel>() .FirstOrDefault(); return this.Created($"/api/Questions/{id}", result); }
public bool ValidateQuestionType(QuestionRequestModel model, out string message) { message = string.Empty; if (!Enum.GetNames(typeof(QuestionType)).Contains(model.QuestionType)) { message = "Invalid question type"; return(false); } return(true); }
// GET: Questions public async Task <ActionResult> Index(string courseId, string levelId, string contentId, string keyword = "", string orderBy = "", string isAscending = "") { ControllerHelper c = new ControllerHelper(courseId, levelId, contentId, keyword, orderBy, isAscending); c.SetViewBagValues(ViewBag); var dbSet = db.Questions; var queryable = dbSet.ByCourse(c.CourseId).ByLevel(c.LevelId).ByContent(c.ContentId); var requestModel = new QuestionRequestModel(keyword, orderBy, isAscending); var list = await requestModel.GetOrderedData(queryable).Include(x => x.Content).ToListAsync(); return(View(list)); }
public bool ValidateNumberOfOptions(QuestionRequestModel model, out string message) { if (model.Options.Count > 1) { message = "Options are more than one"; return(true); } else { message = $"Question options must be greater than one. You currently have only { model.Options.Count}"; return(false); } }
public async Task <IHttpActionResult> CreateQuestion( QuestionRequestModel requestModel, CancellationToken cancellationToken = default(CancellationToken)) { return(await ExecuteAsync <IHttpActionResult>(() => { if (!ModelState.IsValid) { throw new BlissException(CommonExceptionResources.AllFieldsMandatory); } return Ok(questionBc.CreateQuestion(requestModel)); }, cancellationToken)); }
private ICollection <Answer> GetAnswersFrom(QuestionRequestModel model) { var answers = new List <Answer>(); model.CorrectAnswers .Select(text => new Answer(text, true)) .ForEach(answers.Add); model.WrongAnswers .Select(text => new Answer(text)) .ForEach(answers.Add); return(answers); }
public void QuestionBcTests_GetQuestion() { var random = TestUtils.RandomString(10); var question = new QuestionRequestModel { Question = $"Random question {random} with choice (Get)", ImageUrl = "https://www.google.pt/search?q=image", ThumbUrl = "https://www.google.pt/search?q=thumb", Choices = new[] { $"choice1_{random}", $"choice2_{random}", $"choice3_{random}" } }; var dbQuestion = questionBc.CreateQuestion(question); Assert.IsNotNull(questionBc.GetById(dbQuestion.Id)); }
public async Task <QuestionResponseModel> CreateQuestionAsync(QuestionRequestModel model, string userId) { string message; bool success = _questionValidator.ValidateQuestion(model, out message); if (!success) { throw new ProcessException(message); } var newQuestion = await _questionRepo.CreateQuestionAsync(model, userId); return(newQuestion); }
// GET: Questions public async Task <ActionResult> Index(string courseId, string levelId, string contentId, string keyword = "", string orderBy = "", string isAscending = "") { ControllerHelper c = new ControllerHelper(courseId, levelId, contentId, keyword, orderBy, isAscending); ViewBag.CourseId = c.CoursesSelectList; ViewBag.LevelId = c.LevelsSelectList; ViewBag.ContentId = c.ContentsSelectList; ViewBag.OrderBy = c.OrderBySelectList; ViewBag.IsAscending = c.IsAscendingSelectList; ViewBag.Keyword = keyword; var queryable = db.Questions.ByCourse(c.CourseId).ByLevel(c.LevelId).ByContent(c.ContentId); var requestModel = new QuestionRequestModel(keyword, orderBy, isAscending); var list = await requestModel.GetOrderedData(queryable).Include(x => x.Content).ToListAsync(); return(View(list)); }
public bool ValidateQuestionOptions(QuestionRequestModel model, out string message) { if (model.QuestionType.Equals(QuestionType.SingleChoice.ToString())) { int countAnswers = model.Options.Where(u => u.IsAnswer.Value).Count(); if (countAnswers == 0) { message = $"You must have a least one answer for single choice questions"; return(false); } if (countAnswers > 1) { message = $"Number of answers must be only one for a single choice question"; return(false); } message = $"Validation successful"; return(true); } else if (model.QuestionType.Equals(QuestionType.MultipleChoice.ToString())) { int countAnswers = model.Options.Where(u => u.IsAnswer.Value).Count(); if (countAnswers < 1) { message = $"Number of answers must be one or more for a multiple choice question"; return(false); } message = $"Validation successful"; return(true); } else { message = $"Invalid quesiton type"; return(false); } }
public async Task <QuestionResponseModel> UpdateQuestionAsync(string questionId, QuestionRequestModel model, string userId) { using (var transaction = _dbContext.Database.BeginTransaction()) { try { var entity = _dbContext.Questions.First(u => u.Id == questionId); entity.Text = model.Text; entity.SubjectId = model.SubjectId; entity.ScoreValue = model.ScoreValue.Value; entity.QuestionType = model.QuestionType; entity.ShuffleOptions = model.ShuffleOptions.Value; entity.DifficultyLevelId = model.DifficultyLevelId; entity.Options = model.Options.Select(u => new Option { Text = u.Text, IsAnswer = u.IsAnswer.Value }).ToList(); var question = model.Map(userId, questionId); var options = _dbContext.Options.Where(u => u.QuestionId == questionId); _dbContext.RemoveRange(options); await _dbContext.SaveChangesAsync(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception("An error occurred"); } } return(await GetQuestionAsync(questionId)); }
public async Task <ResponseModelBase> CreateQuestion(QuestionRequestModel model) { var response = await _httpClient.PostAsJsonAsync("api/v1/question", model); return(JsonConvert.DeserializeObject <ResponseModelBase>(await response.Content.ReadAsStringAsync())); }
public void ValidateQuestion(QuestionRequestModel requestModel) { bool success = _questionValidator.ValidateQuestion(requestModel, out string message); Assert.True(success, message); }
public async Task <QuestionResponseModel> UpdateQuestionAsync(string questionId, QuestionRequestModel model, string userId) { string message; bool success = _questionValidator.ValidateQuestion(model, out message); if (!success) { throw new ProcessException(message); } return(await _questionRepo.UpdateQuestionAsync(questionId, model, userId)); }
public Question BuildQuestion(QuestionRequestModel questionRequestModel) { return(new Question(questionRequestModel.Title)); }