public async Task SubmitTestAsync(TestModel test) { await _testValidator.ValidateAsync(test); test.DateTimePassed = DateTime.Now; var score = 0; List <QuestionResultDto> results = new(); foreach (var question in test.Questions) { var all = true; foreach (var answer in question.Answers) { if (answer.IsCorrect.Equals((await _answerService.GetByIdAsync(answer.Id)).IsCorrect)) { continue; } all = false; break; } QuestionResultDto result = new() { Id = Guid.NewGuid().ToString(), QuestionId = question.Id, Result = false }; if (all) { result.Result = true; score++; } results.Add(result); } test.Score = score; AttemptDto attemptDto = new() { Id = Guid.NewGuid().ToString(), StudentId = test.Student.Id, TopicId = test.Topic.Id, Score = test.Score, DateTime = test.DateTimePassed }; results.ForEach(qr => qr.AttemptId = attemptDto.Id); await _attemptService.CreateEntityAsync(attemptDto); results.ForEach(async qr => await _questionResultService.CreateEntityAsync(qr)); } } }
public async Task <TestModel> GenerateTestForTopicAsync(UserDto student, string topicId) { await _userValidator.ValidateAsync(student); var topic = await _topicService.GetByIdAsync(topicId); if (topic is null) { throw new TestCreationException("There are no topic with this id"); } var attemptsCount = (await GetTestResultsForStudentAsync(student, topicId)).Count(); if (attemptsCount >= topic.MaxAttemptCount) { throw new TestCreationException("You have no available attempts"); } var topicQuestions = await _questionService.GetEntitiesByPrincipalId(topicId).ToListAsync(); if (topicQuestions.Count < topic.QuestionsPerAttempt) { throw new TestCreationException("Topic don't have enough questions for test"); } List <QuestionDto> testQuestions = new(); while (testQuestions.Count < topic.QuestionsPerAttempt) { topicQuestions.Shuffle(); var question = topicQuestions.First(); if (testQuestions.Contains(question)) { continue; } var answers = await _answerService.GetEntitiesByPrincipalId(question.Id).ToListAsync(); answers.ForEach(a => a.IsCorrect = false); answers.Shuffle(); question.Answers = answers; testQuestions.Add(question); } TestModel test = new (){ Student = student, Topic = topic, Questions = testQuestions }; return(test); }
public async Task <ActionResult <SubjectDto> > GetById(string id) { return(await _subjectService.GetByIdAsync(id)); }
public async Task <ActionResult <CategoryDto> > GetById(int id) => Ok(await _categoryService.GetByIdAsync(id));
public async Task <ActionResult <QuestionResultDto> > GetById(string id) { return(await _questionResultService.GetByIdAsync(id)); }
public async Task <ActionResult <AttemptDto> > GetById(string id) { return(await _attemptService.GetByIdAsync(id)); }
public async Task <ActionResult <AnswerDto> > GetById(string id) { return(await _answerService.GetByIdAsync(id)); }
public async Task <ActionResult <ProductDto> > GetById(int id) => Ok(await _productService.GetByIdAsync(id));
public async Task <ActionResult <TopicDto> > GetById(string id) { return(await _topicService.GetByIdAsync(id)); }