コード例 #1
0
ファイル: StateService.cs プロジェクト: Lern-PFR/lern-api
        public async Task <bool> UpdateCompletion(User user, Guid subjectId, CancellationToken token = default)
        {
            var subject = await AvailableSubjects.FirstOrDefaultAsync(x => x.Id == subjectId, token);

            if (subject == null)
            {
                return(false);
            }

            if (!await _progressionService.Exists(user, subject, token))
            {
                var firstModule  = subject.Modules.First(x => x.Order == subject.Modules.Min(module => module.Order));
                var firstConcept = firstModule.Concepts.First(x => x.Order == firstModule.Concepts.Min(concept => concept.Order));

                await _progressionService.Create(user, subject, firstConcept, token);
            }

            var entry = await _progressionService.Get(user, subject, token);

            var results = (await _resultService.GetAll(user, subject, token)).ToList();

            var validResultsCount = results.Count(result => result.Answer.Valid);
            var resultsCount      = results.Count;

            var questionsCount =
                subject.Modules.Sum(module =>
                                    module.Concepts.Sum(concept =>
                                                        concept.Exercises.Sum(exercise => exercise.Questions.Count) + concept.Lessons.Sum(lesson => lesson.Exercises.Sum(exercise => exercise.Questions.Count))));

            var completion = (resultsCount * 100d) / questionsCount;
            var score      = (validResultsCount * 100d) / questionsCount;

            return(await _progressionService.ExecuteTransaction(set =>
            {
                entry.Completion = completion;
                entry.Completed = resultsCount == questionsCount;

                if (entry.Completed)
                {
                    entry.Score = score;
                }
                else
                {
                    entry.Score = 0;
                }
            }, token));
        }
コード例 #2
0
        public async Task <ActionResult <ProgressionResponse> > GetProgression(Guid subjectId)
        {
            var subject = await _subjects.Get(subjectId, HttpContext.RequestAborted);

            if (subject == null)
            {
                return(NotFound());
            }

            var progression = await _progression.Get(HttpContext.GetUser(), subject, HttpContext.RequestAborted);

            if (progression == null)
            {
                return(NoContent());
            }

            return(new ProgressionResponse(progression));
        }