// --------------------------------------------------------------------------------------------- public static void CopyValues(TestQuestionDTO source, test_question target) { target.id = source.id; target.module_id = source.module_id; target.position = source.position; target.question_answer = source.question_answer; }
public void TestCreateTests() { TestDTO test = new TestDTO() { IsActive = true, IsRandomQuestionsOrder = true, Name = "Test of test", }; for (int i = 0; i < 5; i++) { TestQuestionDTO question = new TestQuestionDTO() { Question = $"Question {i}", IsActive = true, IsSingleAnswer = false, TestQuestionAnswers = new System.Collections.Generic.List <TestQuestionAnswerDTO>() { new TestQuestionAnswerDTO() { Answer = $"Answer {i}_1", IsActive = true, IsCorrect = false }, new TestQuestionAnswerDTO() { Answer = $"Answer {i}_2", IsActive = true, IsCorrect = false }, new TestQuestionAnswerDTO() { Answer = $"Answer {i}_3", IsActive = true, IsCorrect = true }, new TestQuestionAnswerDTO() { Answer = $"Answer {i}_4", IsActive = true, IsCorrect = false }, new TestQuestionAnswerDTO() { Answer = $"Answer {i}_5", IsActive = true, IsCorrect = true }, new TestQuestionAnswerDTO() { Answer = $"Answer {i}_6", IsActive = true, IsCorrect = true }, new TestQuestionAnswerDTO() { Answer = $"Answer {i}_7", IsActive = true, IsCorrect = false } } }; } Mock <ITestService> mockService = new Mock <ITestService>(); mockService.Setup(arg => arg.Test_SaveAsync(test)).Returns(Task.FromResult(0)); }
public async Task <TestItemModel> GetTestItemModelAsync(Guid id) { TestItemModel model = new TestItemModel(); var userTest = await DB.UserTestRepository.FindByIdAsync(id); //Get test item var test = await Test_GetAsync(userTest.TestId, true, true); model.ID = id; model.TestId = test.TestId; model.TestName = test.Name; //Get free questions fot this test var userQuestion = _mapper.Map <List <TestQuestion>, List <TestQuestionDTO> >(await DB.TestQuestionRepository.GetTestQuestionsByTest(id)); var freeQuestoins = test.TestQuestions.Where(n => !userQuestion.Select(x => x.TestQuestionId).Contains(n.TestQuestionId)).ToList(); TestQuestionDTO topQuestion = null; if (freeQuestoins.Count > 0) { if (test.IsRandomQuestionsOrder) { topQuestion = freeQuestoins.OrderBy(n => Guid.NewGuid()).FirstOrDefault(); } else { topQuestion = freeQuestoins.FirstOrDefault(); } } //if question exists, fill it if (topQuestion != null) { model.QuestionId = topQuestion.TestQuestionId; model.QuestionName = topQuestion.Question; model.IsSingleAnswer = topQuestion.IsSingleAnswer; model.Answers = topQuestion.TestQuestionAnswers; } else { //is test completed model.IsComplete = true; } return(model); }
// PUBLIC // ============================================================================================= public TestQuestionDTO UpsertQuestion(TestQuestionDTO questionReceived) { var id = questionReceived.id; test_question question; if (id == 0) { question = new test_question(); TestQuestionMapper.CopyValues(questionReceived, question); _questionRepository.Add(question); } else { question = _questionRepository.Get(id); _questionRepository.SetNewValues(questionReceived, question); } return(TestQuestionMapper.GetDTO(question)); }
public string FindQuestion(string login, string password, int id) { using MyContext context = new MyContext(); var user = AuthHelper.FindUser(login, password, context); if (user == null) { return(null); } var question = context.TestQuestions.FirstOrDefault(tq => tq.Id == id); if (question == null) { return(null); } var questionDTO = new TestQuestionDTO() { Id = question.Id, Answers = new List <TestQuestionAnswerDTO>(), AreMultipleAnswersRight = question.AreMultipleAnswersRight, IsOpen = question.IsOpen, TestId = question.TestId, Text = question.Text }; var testQuestionsAnswers = context.TestQuestionAnswers.Where(tqa => tqa.QuestionId == question.Id); foreach (var tqa in testQuestionsAnswers) { { questionDTO.Answers.Add(new TestQuestionAnswerDTO() { Id = tqa.Id, IsRight = tqa.IsRight, QuestionId = tqa.QuestionId, Text = tqa.Text }); } } return(questionDTO.ToJson()); }
// PUBLIC // ============================================================================================= /* Kopiuje dane z TestQuestionDTO do test_question pobranego z bazy i zapisuje zmiany w bazie. */ public void SetNewValues(TestQuestionDTO source, test_question result) { _context.Entry(result).CurrentValues.SetValues(source); _context.SaveChanges(); }