コード例 #1
0
        public async Task <IActionResult> PostAnswer(string progressId, int lessonId, int stepId, [FromBody] Answer answer)
        {
            if (answer == null)
            {
                return(BadRequest("answer is null"));
            }

            var courseProgress = await progressRepository.GetAsync(progressId);

            if (courseProgress == null)
            {
                return(NotFound($"Invalid progressId = {progressId}"));
            }

            var userId = courseProgress.UserId;

            if (!IdIsValid(userId))
            {
                return(Forbid($"The user with id = {userId} has no rights"));
            }

            var course = await courseRepository.GetAsync(courseProgress.CourceId);

            var question = course.GetQuestion(lessonId, stepId, answer.QuestionId);

            if (question == null)
            {
                return(NotFound($"Invalid combination of lessonId = {lessonId}, stepId = {stepId}, questionId = {answer.QuestionId} doesn't exist"));
            }

            var questionState = courseProgress
                                .LessonProgresses[lessonId]
                                .StepProgresses[stepId]
                                .QuestionStates[answer.QuestionId];

            var result = questionState.Update(question, answer.SelectedAnswers);

            if (question.TotalAttemptsCount < result.CurrentAttemptsCount)
            {
                return(InvalidOperation("No available attempts"));
            }

            courseProgress.Update(lessonId, stepId, answer.QuestionId, result);
            await progressRepository.ReplaceAsync(courseProgress.Id, courseProgress);

            return(Ok(result));
        }