public async Task <ApiModels.AnswerResult> SaveAnswer(ApiModels.Answer answer)
        {
            if (string.IsNullOrEmpty(answer.AnswerText))
            {
                throw new ArgumentNullException("Answer text is null");
            }

            var question = QuestionCache.Where(q => q.Id == answer.QuestionID).FirstOrDefault();

            var result = CheckAnswer(question, answer);


            UpdateUserPairScoreRepository(question, true, answer.UserId, answer.DictionaryId);

            if (result)
            {
                await UpdateQuestionRepository(question, answer);
            }

            var answerResult = new ApiModels.AnswerResult()
            {
                IsCorrect = result
            };

            await RefreshCaches(true);

            return(answerResult);
        }
        private async Task <Question> UpdateQuestionRepository(Question question, ApiModels.Answer answer)
        {
            question.Answer           = answer.AnswerText;
            question.DateOfAnswer     = DateTime.UtcNow;
            question.WasAnswerCorrect = true;

            var result = QuestionDatabaseProvider.Update(question);

            return(await Task.FromResult(result));
        }
        private bool CheckAnswer(Question question, ApiModels.Answer answer)
        {
            var pair = PairCache.Where(p => p.Id == question.PairID).FirstOrDefault();

            if (question.QuestionWordNumber == 1)
            {
                return(string.Compare(pair.InLanguage2, answer.AnswerText, true) == 0);
            }
            else
            {
                return(string.Compare(pair.InLanguage1, answer.AnswerText, true) == 0);
            }
        }
示例#4
0
        public async Task <ActionResult <ApiModels.AnswerResult> > SaveAnswer([FromBody] ApiModels.Answer answer)
        {
            var value = _manager.SaveAnswer(answer);

            return(await value);
        }