Exemplo n.º 1
0
        public async Task <IActionResult> GetFlashcardsWithProgress(Guid collectionId)
        {
            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (String.IsNullOrEmpty(userId))
            {
                return(NotFound());
            }

            var getFlashcardsListCommand = new GetFlashcardsListWithProgressesParallelQ(collectionId, userId);

            var actionResponse = await _mediator.Send(getFlashcardsListCommand);

            return(Ok(_mapper.Map <List <FlashcardWithProgressionGetModel> >(actionResponse)));
        }
        public async Task <List <FlashcardWithProgressionGetModel> > Handle(GetFlashcardsListWithProgressesParallelQ request, CancellationToken cancellationToken)
        {
            if (await _mediator.Send(new GetCollectionByIdWithDailyStatsQ(request.CollectionId, request.UserId)) == null)
            {
                return(new List <FlashcardWithProgressionGetModel>());
            }

            var flashcards = await _flashcardRepo.GetWhereCollectionId(request.CollectionId);

            var output = new ConcurrentBag <FlashcardWithProgressionGetModel>();

            var progressModels = await _flashcardProgressRepo.GetAllUserFlashcards(request.UserId);

            Parallel.ForEach(flashcards, async flashcard =>
            {
                var nativeToForeign = progressModels.FirstOrDefault(e => e.FlashcardModelId == flashcard.Id && e.PracticeDirection == PracticeDirection.NativeToForeign);
                var foreignToNative = progressModels.FirstOrDefault(e => e.FlashcardModelId == flashcard.Id && e.PracticeDirection == PracticeDirection.ForeignToNative);

                output.Add(new FlashcardWithProgressionGetModel()
                {
                    Id      = flashcard.Id,
                    Foreign = flashcard.Foreign,
                    Native  = flashcard.Native,
                    ProgressForeignToNative = new FlashcardWithProgressGetModel()
                    {
                        CorrectInRow             = foreignToNative.CorrectInRow,
                        PracticeDate             = foreignToNative.PracticeDate.ToString("dd-MM-yyyy"),
                        PracticeDateIfCorrectAns = ((await _mediator.Send(new CalculatePracticeDateQ(foreignToNative.CorrectInRow, FlashcardProgress.Know)))
                                                    .AddDays((foreignToNative.PracticeDate - DateTime.Now.Date).TotalDays)).ToString("dd-MM-yyyy")
                    },
                    ProgressNativeToForeign = new FlashcardWithProgressGetModel()
                    {
                        CorrectInRow             = nativeToForeign.CorrectInRow,
                        PracticeDate             = nativeToForeign.PracticeDate.ToString("dd-MM-yyyy"),
                        PracticeDateIfCorrectAns = ((await _mediator.Send(new CalculatePracticeDateQ(nativeToForeign.CorrectInRow, FlashcardProgress.Know)))
                                                    .AddDays((nativeToForeign.PracticeDate - DateTime.Now.Date).TotalDays)).ToString("dd-MM-yyyy")
                    }
                });
            });

            return(output.ToList());
        }