Пример #1
0
        public IActionResult PostAnswer([FromRoute] Guid exerciseId, [FromBody] Answer answer)
        {
            var result = new Result();

            if (exerciseId != answer.ExerciseId)
            {
                return(BadRequest());
            }

            var session = State.Sessions.FirstOrDefault(s => s.Id == answer.SessionId);

            if (session == null)
            {
                return(BadRequest());
            }

            var exercise = State.Exercises.FirstOrDefault(e => e.Id == answer.ExerciseId.Value && e.AssignedSession == answer.SessionId);

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

            result.AnswerCorrect = CheckAnswer(exercise, answer.SubmittedAnswer);

            if (result.AnswerCorrect.Value)
            {
                bool completedAllStages = MoveToNextLevel(session);
                result.AllLevelCompleted = completedAllStages;
            }
            else
            {
                result.AllLevelCompleted = false;
            }

            result.Rank  = session.Rank;
            result.Level = session.Level;

            _exerciseLogic.RemoveExercise(exercise);

            if (!result.AnswerCorrect.Value || result.AllLevelCompleted.Value)
            {
                _sessionLogic.EndSession(session);
            }

            return(Ok(result));
        }