Example #1
0
        public async Task <IActionResult> PracticeDone([FromBody] IEnumerable <Word> words)
        {
            foreach (var word in words)
            {
                var wordLearntFromDb = await _repo.GetOneWithConditionTracking <WordLearnt>(w => w.WordId == word.Id);

                if (wordLearntFromDb == null)
                {
                    var wordLearn = new WordLearnt()
                    {
                        WordId       = word.Id,
                        AccountId    = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value),
                        LastPractice = DateTime.Now
                    };
                    _repo.Create(wordLearn);
                }
                else
                {
                    wordLearntFromDb.LastPractice = DateTime.Now;
                }
            }
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(NoContent());
        }
        public async Task <IActionResult> CheckAnswer(int questionId, [FromBody] AnswerDTO answer)
        {
            if (questionId != answer.Id)
            {
                return(NotFound());
            }
            var questionFromDb = await _repo.GetOneWithCondition <Question>(q => q.Id == questionId);

            if (questionFromDb == null)
            {
                return(NotFound());
            }
            if (questionFromDb.Answer == answer.Answer)
            {
                answer.IsRightAnswer = true;
            }
            if (User.FindFirst(ClaimTypes.NameIdentifier) != null)
            {
                var userId     = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
                var wordFromDb = await _repo.GetOneWithCondition <Word>(w => w.Eng.Equals(questionFromDb.Content) || w.Vie.Equals(questionFromDb.Content) || w.Eng.Equals(questionFromDb.Answer) || w.Vie.Equals(questionFromDb.Answer));

                if (wordFromDb != null)
                {
                    var learntFromDb = await _repo.GetOneWithConditionTracking <WordLearnt>(w => w.WordId == wordFromDb.Id && w.AccountId == userId);

                    if (learntFromDb == null)
                    {
                        var wordLearnt = new WordLearnt()
                        {
                            AccountId    = userId,
                            WordId       = wordFromDb.Id,
                            LastPractice = DateTime.Now
                        };
                        _repo.Create(wordLearnt);
                    }
                    else
                    {
                        learntFromDb.LastPractice = DateTime.Now;
                    }
                }

                await _repo.SaveAll();
            }
            return(Ok(answer));
        }