Beispiel #1
0
            public async Task <LearningListDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var dictionary = await _context.Dictionaries.FindAsync(request.DictionaryId);

                if (dictionary == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.DictionaryNotFound);
                }

                var learningList = await _context.LearningLists
                                   .Where(l => l.DictionaryId == request.DictionaryId)
                                   .Include(l => l.LearningItems)
                                   .FirstOrDefaultAsync();

                if (learningList != null)
                {
                    if (!DateChecker.IsLearningListOutdated(learningList))
                    {
                        return(_mapper.Map <LearningList, LearningListDto>(learningList));
                    }
                    await _learningListRemover.Remove(learningList);
                }

                learningList = await _learningListGenerator.HandleGeneration(dictionary);

                _context.LearningLists.Add(learningList);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(_mapper.Map <LearningList, LearningListDto>(learningList));
                }
                throw new RestException(HttpStatusCode.InternalServerError, ErrorType.SavingChangesError);
            }
Beispiel #2
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var dictionary = await _context.Dictionaries.FindAsync(request.DictionaryId);

                if (dictionary == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.DictionaryNotFound);
                }

                var learningList = await _context.LearningLists
                                   .Where(l => l.DictionaryId == request.DictionaryId)
                                   .Include(l => l.LearningItems)
                                   .FirstOrDefaultAsync();

                if (learningList == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.LearningListNotFound);
                }

                if (DateChecker.IsLearningListOutdated(learningList))
                {
                    throw new RestException(HttpStatusCode.Gone, ErrorType.LearningListOutdated);
                }

                if (learningList.TimesCompleted == 0 || !learningList.IsCompleted)
                {
                    throw new RestException(HttpStatusCode.BadRequest, ErrorType.LearningListNotCompleted);
                }

                if (learningList.TimesCompleted == 2)
                {
                    throw new RestException(HttpStatusCode.BadRequest, ErrorType.LearningListCompletedTwoTimes);
                }

                learningList.IsCompleted         = false;
                learningList.CompletedItemsCount = 0;

                var learningItemsList = learningList.LearningItems.ToList();

                LearningListShuffler.Shuffle(learningItemsList);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new RestException(HttpStatusCode.InternalServerError, ErrorType.SavingChangesError);
            }
Beispiel #3
0
            public async Task <LearningItemDto> Handle(Query request,
                                                       CancellationToken cancellationToken)
            {
                var dictionary = await _context.Dictionaries.FindAsync(request.DictionaryId);

                if (dictionary == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.DictionaryNotFound);
                }

                var learningList = await _context.LearningLists.FindAsync(request.LearningListId);

                if (learningList == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.LearningListNotFound);
                }

                if (DateChecker.IsLearningListOutdated(learningList))
                {
                    throw new RestException(HttpStatusCode.Gone, ErrorType.LearningListOutdated);
                }

                if (learningList.IsCompleted)
                {
                    return(null);
                }

                var learningItem = await _context.LearningItems
                                   .Where(i =>
                                          i.LearningListId == learningList.Id &&
                                          i.NumberInSequence == learningList.CompletedItemsCount)
                                   .Include(i => i.Item)
                                   .FirstAsync();

                var itemToReturn = new LearningItemDto
                {
                    Id = learningItem.Id,

                    NumberInSequence = learningItem.NumberInSequence,
                    LearningMode     = learningItem.LearningMode,

                    Item = TestItemCreator.Create(learningItem)
                };

                return(itemToReturn);
            }
Beispiel #4
0
            public async Task <LearningItemResult> Handle(Command request, CancellationToken cancellationToken)
            {
                var dictionary = await _context.Dictionaries.FindAsync(request.DictionaryId);

                if (dictionary == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.DictionaryNotFound);
                }

                var learningList = await _context.LearningLists.FindAsync(request.LearningListId);

                if (learningList == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.LearningListNotFound);
                }

                if (DateChecker.IsLearningListOutdated(learningList))
                {
                    throw new RestException(HttpStatusCode.Gone, ErrorType.LearningListOutdated);
                }

                var learningItem = await _context.LearningItems
                                   .Where(i => i.Id == request.LearningItemId)
                                   .Include(i => i.Item)
                                   .FirstOrDefaultAsync();

                if (learningItem == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.LearningItemNotFound);
                }

                if (learningItem.NumberInSequence != learningList.CompletedItemsCount)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.LearningItemNotFound);
                }

                var answer = request.Answer.ToLower();
                var item   = learningItem.Item;

                var isAnswerCorrect = learningItem.LearningMode == LearningMode.Primary
                    ? answer.ToLower().Equals(item.Original.ToLower())
                    : answer.ToLower().Equals(item.Translation.ToLower());

                if (isAnswerCorrect)
                {
                    learningList.CorrectAnswersCount++;
                }

                ItemAnswerProcessor.ProcessItemAnswer(dictionary, learningList, learningItem, isAnswerCorrect);

                learningList.CompletedItemsCount++;
                learningList.TotalCompletedItemsCount++;

                if (learningList.Size == learningList.CompletedItemsCount)
                {
                    learningList.CompletedItemsCount = 0;
                    learningList.IsCompleted         = true;
                    learningList.TimesCompleted++;
                }

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return new LearningItemResult
                           {
                               IsAnswerCorrect  = isAnswerCorrect,
                               UserAnswer       = request.Answer ?? "",
                               NumberInSequence = learningItem.NumberInSequence,

                               Item = new TestItemAnswer
                               {
                                   Id     = item.Id,
                                   Item   = learningItem.LearningMode == LearningMode.Primary ? item.Translation : item.Original,
                                   Answer = learningItem.LearningMode == LearningMode.Primary
                                ? item.Original
                                : item.Translation,
                                   Definition       = item.Definition,
                                   DefinitionOrigin = item.DefinitionOrigin,
                                   Type             = item.Type,

                                   IsStarred = item.IsStarred,
                                   IsLearned = item.IsLearned,
                                   CorrectAnswersToCompletionCount = item.CorrectAnswersToCompletionCount,
                               }
                           }
                }
                ;
                throw new RestException(HttpStatusCode.InternalServerError, ErrorType.SavingChangesError);
            }
        }