예제 #1
0
        public IActionResult Save([FromBody] TeamAnswerViewModel viewModel)
        {
            if (viewModel != null && viewModel.Details != null)
            {
                foreach (var detail in viewModel.Details)
                {
                    var teamAnswer = _TeamAnswerRepository.RetrieveFirstOrDefault(ta => ta.Id == detail.Id);
                    teamAnswer.Score  = detail.Score;
                    teamAnswer.IsDone = true;
                    _TeamAnswerRepository.Update(teamAnswer);
                }
            }

            return(Ok(ServiceResponse.Ok()));
        }
예제 #2
0
        public ActionResult SendAnswer(Guid questionId, string answer)
        {
            var team     = _TeamRepository.RetrieveFirstOrDefault(t => t.Login == User.Identity.Name);
            var question = _QuestionRepository.RetrieveFirstOrDefault(q => q.Id == questionId);

            if (team != null && question != null)
            {
                var teamAnswer = _TeamAnswerRepository.RetrieveFirstOrDefault(ta => ta.QuestionId == questionId && ta.TeamId == team.Id);

                if (teamAnswer == null)
                {
                    teamAnswer = new TeamAnswer
                    {
                        QuestionId    = questionId,
                        Answer        = answer,
                        TeamId        = team.Id,
                        IsDone        = false,
                        Score         = question.Score,
                        AttemptNumber = 1
                    };

                    _TeamAnswerRepository.Create(teamAnswer);
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(teamAnswer.Answer))
                    {
                        teamAnswer.Answer = answer;
                    }
                    else
                    {
                        teamAnswer.Answer = string.Join(';', teamAnswer.Answer, answer);
                    }
                }

                // не обновляем уже отвеченный вопрос
                if (teamAnswer.IsDone)
                {
                    return(Ok(ServiceResponse.Ok()));
                }

                var answers = question.Answers.Split(';');
                foreach (var ans in answers)
                {
                    if (string.Compare(ans?.Trim(), answer?.Trim(), true) == 0)
                    {
                        teamAnswer.IsDone = true;
                        _TeamAnswerRepository.Update(teamAnswer);
                        return(Ok(ServiceResponse.Ok()));
                    }
                }

                // отнимаем балл за ошибки
                teamAnswer.Score -=
                    teamAnswer.AttemptNumber == 1
                    ? 3
                    : 2;

                teamAnswer.AttemptNumber++;

                if (teamAnswer.Score <= 0)
                {
                    teamAnswer.Score  = 0;
                    teamAnswer.IsDone = true;
                }

                _TeamAnswerRepository.Update(teamAnswer);
            }

            return(Ok(ServiceResponse.Ok()));
        }