示例#1
0
        protected override async Task <ResultWithMetrologyProperties <ResultModel> > DoRunAsync(Request request)
        {
            var users = DbContext.Users.AsNoTracking().Where(user => EF.Functions.Like(user.UserName, $"%{request.Filter}%")).OrderBy(user => user.UserName);

            var totalCount  = users.Count();
            var pageCount   = (int)Math.Ceiling((double)totalCount / request.PageSize);
            var pageEntries = await users.Skip((request.PageNo - 1) *request.PageSize).Take(request.PageSize).ToListAsync();

            var resultUsers = new List <ResultUserModel>();

            foreach (var user in pageEntries)
            {
                var roles = await RoleChecker.GetRolesAsync(user);

                resultUsers.Add(new ResultUserModel(user.UserName, user.Id, string.Join(',', roles), user.Email, user.MinimumCountOfDaysBetweenNotifs, user.LastNotificationUtcDate));
            }
            var result = new ResultModel(totalCount, pageCount, resultUsers);

            return(new ResultWithMetrologyProperties <ResultModel>(result,
                                                                   ("LoggedUser", request.UserId.ToString()),
                                                                   ("PageSize", request.PageSize.ToString()),
                                                                   ("PageNo", request.PageNo.ToString()),
                                                                   ("FilterLength", request.Filter.Length.ToString()),
                                                                   ("TotalCount", result.TotalCount.ToString()),
                                                                   ("PageCount", result.PageCount.ToString()),
                                                                   ("ResultCount", result.Users.Count().ToString())));
        }
示例#2
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var tagName = await DbContext.Tags.Where(tag => tag.Id == request.TagId).Select(tag => tag.Name).SingleAsync();

            var previousVersionCreator = new PreviousVersionCreator(DbContext);

            foreach (var cardId in request.CardIds)
            {
                if (!DbContext.TagsInCards.Any(tagInCard => tagInCard.CardId == cardId && tagInCard.TagId == request.TagId))
                {
                    var card = await previousVersionCreator.RunAsync(cardId, request.VersionCreator.Id, Localized.Get("AddTag") + $" '{tagName}'");

                    card.VersionCreator = request.VersionCreator; //A priori inutile, à confirmer
                    DbContext.TagsInCards.Add(new TagInCard()
                    {
                        TagId = request.TagId, CardId = cardId
                    });
                }
            }
            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(),
                                                              ("TagId", request.TagId.ToString()),
                                                              ("TagName", tagName),
                                                              ("CardCount", request.CardIds.Count().ToString())));
        }
示例#3
0
        protected override async Task <ResultWithMetrologyProperties <IEnumerable <IResultCardVersion> > > DoRunAsync(Request request)
        {
            var currentVersion = await DbContext.Cards.Include(card => card.PreviousVersion)
                                 .Where(card => card.Id == request.CardId)
                                 .Select(card => new CardVersionFromDb(
                                             card.Id,
                                             true,
                                             card.PreviousVersion == null ? (Guid?)null : card.PreviousVersion.Id,
                                             card.VersionUtcDate,
                                             card.VersionCreator,
                                             card.VersionDescription,
                                             card.CardLanguage.Id,
                                             card.FrontSide,
                                             card.BackSide,
                                             card.AdditionalInfo,
                                             card.TagsInCards.Select(tag => tag.TagId),
                                             card.UsersWithView.Select(user => user.UserId),
                                             card.Images.Where(img => img.CardSide == ImageInCard.FrontSide).Select(img => img.ImageId),
                                             card.Images.Where(img => img.CardSide == ImageInCard.BackSide).Select(img => img.ImageId),
                                             card.Images.Where(img => img.CardSide == ImageInCard.AdditionalInfo).Select(img => img.ImageId)
                                             )
                                         ).SingleAsync();

            var allPreviousVersions = DbContext.CardPreviousVersions
                                      .Where(card => card.Card == request.CardId)
                                      .Select(card => new CardVersionFromDb(
                                                  card.Id,
                                                  false,
                                                  card.PreviousVersion == null ? (Guid?)null : card.PreviousVersion.Id,
                                                  card.VersionUtcDate,
                                                  card.VersionCreator,
                                                  card.VersionDescription,
                                                  card.CardLanguage.Id,
                                                  card.FrontSide,
                                                  card.BackSide,
                                                  card.AdditionalInfo,
                                                  card.Tags.Select(tag => tag.TagId),
                                                  card.UsersWithView.Select(u => u.AllowedUserId),
                                                  card.Images.Where(img => img.CardSide == 1).Select(img => img.ImageId),
                                                  card.Images.Where(img => img.CardSide == 2).Select(img => img.ImageId),
                                                  card.Images.Where(img => img.CardSide == 3).Select(img => img.ImageId)
                                                  )
                                              );

            var versionDico = allPreviousVersions.ToImmutableDictionary(ver => ver.Id, ver => ver);

            var result           = new List <ResultCardVersion>();
            var iterationVersion = currentVersion.PreviousVersion == null ? null : versionDico[currentVersion.PreviousVersion.Value];

            result.Add(new ResultCardVersion(currentVersion, iterationVersion));

            while (iterationVersion != null)
            {
                var previousVersion = iterationVersion.PreviousVersion == null ? null : versionDico[iterationVersion.PreviousVersion.Value];
                result.Add(new ResultCardVersion(iterationVersion, previousVersion));
                iterationVersion = previousVersion;
            }

            return(new ResultWithMetrologyProperties <IEnumerable <IResultCardVersion> >(result, ("CardId", request.CardId.ToString()), ("ResultCount", result.Count.ToString())));
        }
示例#4
0
        protected override async Task <ResultWithMetrologyProperties <IEnumerable <Result> > > DoRunAsync(Request request)
        {
            var decks   = DbContext.Decks.Where(deck => deck.Owner.Id == request.UserId).OrderBy(deck => deck.Description);
            var results = decks.Select(deck => new Result(deck.Id, deck.Description, deck.HeapingAlgorithmId, deck.CardInDecks.Count));
            await Task.CompletedTask;

            return(new ResultWithMetrologyProperties <IEnumerable <Result> >(results, ("DeckCount", results.Count().ToString())));
        }
示例#5
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var user = await DbContext.Users.SingleAsync(user => user.Id == request.UserId);

            user.UILanguage = request.CultureName;
            DbContext.SaveChanges();
            return(new ResultWithMetrologyProperties <Result>(new Result(), ("CultureName", request.CultureName)));
        }
示例#6
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var tag = await DbContext.Tags.AsNoTracking().Include(tag => tag.TagsInCards).SingleAsync(tag => tag.Id == request.TagId);

            var result = new Result(tag.Id, tag.Name, tag.Description, tag.TagsInCards == null ? 0 : tag.TagsInCards.Count);

            return(new ResultWithMetrologyProperties <Result>(result, ("TagName", result.TagName), ("CardCount", result.CardCount.ToString())));
        }
示例#7
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var subscription = await DbContext.SearchSubscriptions.Where(s => s.Id == request.SubscriptionId).SingleAsync();

            subscription.Name = request.Name;
            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(), ("Name", request.Name.ToString()), ("NameLength", request.Name.Length.ToString())));
        }
示例#8
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            Tag tag = new() { Name = request.Name, Description = request.Description };

            DbContext.Tags.Add(tag);
            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(tag.Id), ("NameLength", request.Name.Length.ToString()), ("DescriptionLength", request.Description.Length.ToString())));
        }
        protected override async Task <ResultWithMetrologyProperties <IEnumerable <Result> > > DoRunAsync(Request request)
        {
            var now   = runDate == null ? DateTime.UtcNow : runDate;
            var decks = await DbContext.Decks.AsNoTracking().Where(deck => deck.Owner.Id == request.UserId).Select(deck => new { deck.Id, deck.Description, deck.HeapingAlgorithmId }).ToListAsync();

            var result = decks.Select(deck => GetDeck(DbContext, deck.Id, deck.Description, now.Value));

            return(new ResultWithMetrologyProperties <IEnumerable <Result> >(result, ("DeckCount", result.Count().ToString())));
        }
示例#10
0
        protected override async Task <ResultWithMetrologyProperties <ResultModel> > DoRunAsync(Request request)
        {
            var users = await DbContext.Users.AsNoTracking().ToListAsync();

            var admins = users.Where(u => RoleChecker.UserIsAdminAsync(u).Result).Select(u => new ResultUserModel(u.UserName, u.Email));
            var result = new ResultModel(admins);

            return(new ResultWithMetrologyProperties <ResultModel>(result, ("LoggedUser", request.UserId.ToString()), ("ResultCount", result.Users.Count().ToString())));
        }
示例#11
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var result = await DbContext.Images
                         .AsNoTracking()
                         .Where(img => img.Id == request.ImageId)
                         .Select(img => new Result(img.Owner, img.Name, img.Description, img.Source, img.Cards.Count(), img.InitialUploadUtcDate, img.LastChangeUtcDate, img.VersionDescription))
                         .SingleAsync();

            return(new ResultWithMetrologyProperties <Result>(result, ("ImageId", request.ImageId.ToString())));
        }
示例#12
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var deck = DbContext.Decks.Where(deck => deck.Id == request.DeckId).Single();

            deck.Description        = request.Name;
            deck.HeapingAlgorithmId = request.HeapingAlgorithmId;
            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(), ("DeckId", request.DeckId.ToString()), ("Name", request.Name), ("NameLength", request.Name.Length.ToString()), ("HeapingAlgorithmId", request.HeapingAlgorithmId.ToString())));
        }
示例#13
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var previousValue = await SaveRatingByUserAsync(request);

            if (previousValue != request.Rating)
            {
                await UpdateCardAsync(request.CardId);
            }
            return(new ResultWithMetrologyProperties <Result>(new Result(), ("CardId", request.CardId.ToString()), ("Rating", request.Rating.ToString()), ("PreviousValue", previousValue.ToString())));
        }
示例#14
0
        protected override async Task <ResultWithMetrologyProperties <ResultModel> > DoRunAsync(Request request)
        {
            var card = await DbContext.Cards
                       .AsNoTracking()
                       .Include(card => card.VersionCreator)
                       .Include(card => card.Images)
                       .ThenInclude(img => img.Image)
                       .Include(card => card.CardLanguage)
                       .Include(card => card.TagsInCards)
                       .ThenInclude(tagInCard => tagInCard.Tag)
                       .Include(card => card.UsersWithView)
                       .ThenInclude(userWithView => userWithView.User)
                       .Where(card => card.Id == request.CardId)
                       .AsSingleQuery()
                       .SingleAsync();

            var userRating = await DbContext.UserCardRatings.SingleOrDefaultAsync(c => c.CardId == card.Id && c.UserId == request.CurrentUserId);

            int userRatingValue = userRating == null ? 0 : userRating.Rating;

            var ownersOfDecksWithThisCard = DbContext.CardsInDecks
                                            .AsNoTracking()
                                            .Where(cardInDeck => cardInDeck.CardId == request.CardId)
                                            .Select(cardInDeck => cardInDeck.Deck.Owner.UserName)
                                            .Distinct();

            var result = new ResultModel(
                card.FrontSide,
                card.BackSide,
                card.AdditionalInfo,
                card.CardLanguage.Id,
                card.CardLanguage.Name,
                card.TagsInCards.Select(tagInCard => new ResultTagModel(tagInCard.TagId, tagInCard.Tag.Name)),
                card.UsersWithView.Select(userWithView => new ResultUserModel(userWithView.UserId, userWithView.User.UserName)),
                card.InitialCreationUtcDate,
                card.VersionUtcDate,
                card.VersionCreator.UserName,
                card.VersionDescription,
                ownersOfDecksWithThisCard,
                card.Images.Select(img => new ResultImageModel(img)),
                userRatingValue,
                card.AverageRating,
                card.RatingCount
                );

            return(new ResultWithMetrologyProperties <ResultModel>(result,
                                                                   ("CardId", request.CardId.ToString()),
                                                                   ("CardIsPublic", CardVisibilityHelper.CardIsPublic(card.UsersWithView).ToString()),
                                                                   ("CardIsPrivateToSingleUser", CardVisibilityHelper.CardIsPrivateToSingleUser(request.CurrentUserId, card.UsersWithView).ToString()),
                                                                   ("CardAverageRating", card.AverageRating.ToString()),
                                                                   ("CardUserRating", userRatingValue.ToString()),
                                                                   ("AgeOfCurrentVersionInDays", (DateTime.UtcNow - card.VersionUtcDate).TotalDays.ToString()),
                                                                   ("AgeOfCardInDays", (DateTime.UtcNow - card.InitialCreationUtcDate).TotalDays.ToString()),
                                                                   ("CardLanguage", card.CardLanguage.Name)));
        }
示例#15
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var img = await DbContext.Images.Include(img => img.Cards)
                      .Where(image => EF.Functions.Like(image.Name, $"{request.ImageName}"))
                      .Select(img => new { img.Id, img.Name, img.Source })
                      .SingleAsync();

            var result = new Result(img.Id, img.Name, img.Source);

            return(new ResultWithMetrologyProperties <Result>(result, ("ImageName", request.ImageName)));
        }
示例#16
0
 protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
 {
     byte[] result = request.Size switch
     {
         Request.ImageSize.Small => (await DbContext.Images.AsNoTracking().Select(img => new { img.Id, img.SmallBlob }).SingleAsync(img => img.Id == request.ImageId)).SmallBlob,
         Request.ImageSize.Medium => (await DbContext.Images.AsNoTracking().Select(img => new { img.Id, img.MediumBlob }).SingleAsync(img => img.Id == request.ImageId)).MediumBlob,
         Request.ImageSize.Big => (await DbContext.Images.AsNoTracking().Select(img => new { img.Id, img.BigBlob }).SingleAsync(img => img.Id == request.ImageId)).BigBlob,
         _ => throw new NotImplementedException(request.Size.ToString()),
     };
     return(new ResultWithMetrologyProperties <Result>(new Result(result), ("ImageId", request.ImageId.ToString()), ("RequestedSize", request.Size.ToString()), ("ByteCount", result.Length.ToString())));
 }
示例#17
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var tag = await DbContext.Tags.SingleAsync(tag => tag.Id == request.TagId);

            var initialName = tag.Name;

            tag.Name        = request.NewName;
            tag.Description = request.NewDescription;
            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(), ("InitialName", initialName), ("NewName", request.NewName), ("NewDescription", request.NewDescription)));
        }
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var result = await DbContext.CardsInDecks.AsNoTracking()
                         .Where(cardInDeck =>
                                cardInDeck.DeckId == request.DeckId &&
                                (request.LessonModeIsUnknown ? cardInDeck.CurrentHeap == CardInDeck.UnknownHeap : cardInDeck.CurrentHeap != CardInDeck.UnknownHeap) &&
                                (request.LessonModeIsUnknown ? true : cardInDeck.ExpiryUtcTime <= runDate) &&
                                !cardInDeck.Card.TagsInCards.Any(tag => request.ExcludedTagIds.Contains(tag.TagId)))
                         .CountAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(result), ("DeckId", request.DeckId.ToString())));
        }
示例#19
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var now = DateTime.UtcNow;

            foreach (var cardId in request.CardIds)
            {
                CreateSubscription(DbContext, request.UserId, cardId, now, CardNotificationSubscription.CardNotificationRegistrationMethod_ExplicitByUser);
            }

            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(), ("CardCount", request.CardIds.Count().ToString())));
        }
示例#20
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var card = DbContext.CardsInDecks
                       .Where(cardInDeck => cardInDeck.CardId == request.CardId && cardInDeck.DeckId == request.DeckId)
                       .Include(cardInDeck => cardInDeck.Card)
                       .Include(cardInDeck => cardInDeck.Deck)
                       .Single();

            DbContext.CardsInDecks.Remove(card);
            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(card.Card.FrontSide, card.Deck.Description), ("DeckId", request.DeckId.ToString()), ("CardId", request.CardId.ToString())));
        }
示例#21
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var language = new CardLanguage()
            {
                Name = request.Name
            };

            DbContext.CardLanguages.Add(language);
            await DbContext.SaveChangesAsync();

            var result = new Result(language.Id, language.Name, 0);

            return(new ResultWithMetrologyProperties <Result>(result, ("Name", request.Name)));
        }
示例#22
0
        protected override async Task <ResultWithMetrologyProperties <IEnumerable <Result> > > DoRunAsync(Request request)
        {
            var userDecks = await DbContext.Decks.AsNoTracking().Where(deck => deck.Owner.Id == request.UserId).Select(deck => new { deckId = deck.Id, deckDescription = deck.Description }).ToListAsync();

            var result = new List <Result>();

            foreach (var userDeck in userDecks)
            {
                var appTags    = await new GetTagsOfDeck(DbContext).RunAsync(new GetTagsOfDeck.Request(request.UserId, userDeck.deckId));
                var resultTags = appTags.Select(tag => new ResultTag(tag.TagId, tag.TagName));
                result.Add(new Result(userDeck.deckId, userDeck.deckDescription, resultTags));
            }
            return(new ResultWithMetrologyProperties <IEnumerable <Result> >(result, ("DeckCount", result.Count.ToString())));
        }
示例#23
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var existing = request.CardIds
                           .Where(cardId => DbContext.CardsInDecks.Any(cardInDeck => cardInDeck.DeckId == request.DeckId && cardInDeck.CardId == cardId))
                           .Select(cardId => new CardInDeck()
            {
                CardId = cardId, DeckId = request.DeckId
            });

            DbContext.CardsInDecks.RemoveRange(existing);
            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(), ("DeckId", request.DeckId.ToString()), ("CardCount", request.CardIds.Count().ToString())));
        }
示例#24
0
        protected override async Task <ResultWithMetrologyProperties <IEnumerable <Result> > > DoRunAsync(Request request)
        {
            var tagDictionary  = TagLoadingHelper.Run(DbContext);
            var deckDictionary = DbContext.Decks.AsNoTracking().Where(d => d.Owner.Id == request.UserId).Select(t => new { t.Id, t.Description }).ToImmutableDictionary(t => t.Id, t => t.Description);

            var queryResult = await DbContext.SearchSubscriptions.AsNoTracking()
                              .Include(s => s.RequiredTags)
                              .Include(s => s.ExcludedTags)
                              .Where(search => search.UserId == request.UserId)
                              .ToListAsync();

            var result = queryResult.Select(subscription => new Result(subscription, tagDictionary, deckDictionary, DbContext));

            return(new ResultWithMetrologyProperties <IEnumerable <Result> >(result, ("ResultCount", result.Count().ToString())));
        }
示例#25
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            DeleteDecks(request.UserToDeleteId);
            DeleteRatings(request.UserToDeleteId);
            DeleteSearchSubscriptions(request.UserToDeleteId);
            await DeletePrivateCardsAsync(request.UserToDeleteId);

            UpdateCardsVisibility(request.UserToDeleteId);
            DeleteCardNotificationSubscriptions(request.UserToDeleteId);
            await AnonymizeUser(request.UserToDeleteId);

            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(), ("LoggedUserId", request.LoggedUserId.ToString())));
        }
示例#26
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            foreach (var cardId in request.CardIds)
            {
                var existing = await DbContext.CardNotifications.Where(notif => notif.UserId == request.UserId && notif.CardId == cardId).SingleOrDefaultAsync();

                if (existing != null)
                {
                    DbContext.CardNotifications.Remove(existing);
                }
            }

            await DbContext.SaveChangesAsync();

            return(new ResultWithMetrologyProperties <Result>(new Result(), ("CardCount", request.CardIds.Count().ToString())));
        }
        protected override async Task <ResultWithMetrologyProperties <IEnumerable <Result> > > DoRunAsync(Request request)
        {
            var decks = DbContext.Decks.AsNoTracking().Where(deck => deck.Owner.Id == request.UserId).OrderBy(deck => deck.Description).Select(deck => new { deck.Id, deck.Description }).ToList();

            var result = new List <Result>();

            foreach (var deck in decks)
            {
                var heaps = await DbContext.CardsInDecks.AsNoTracking().Where(cardInDeck => cardInDeck.DeckId == deck.Id).Select(cardInDeck => cardInDeck.CurrentHeap).Distinct().ToListAsync();

                var tags = await new GetTagsOfDeck(DbContext).RunAsync(new GetTagsOfDeck.Request(request.UserId, deck.Id));
                result.Add(new Result(deck.Id, deck.Description, heaps, tags.Select(tag => new ResultTag(tag.TagId, tag.TagName))));
            }

            return(new ResultWithMetrologyProperties <IEnumerable <Result> >(result, ("DeckCount", result.Count.ToString())));
        }
示例#28
0
        protected override async Task <ResultWithMetrologyProperties <NotifierResult> > DoRunAsync(Request request)
        {
            var chrono            = Stopwatch.StartNew();
            var now               = runDate == null ? DateTime.UtcNow : runDate;
            var users             = usersToNotifyGetter.Run(now);
            var userNotifications = new List <UserNotifications>();

            foreach (var user in users)
            {
                userNotifications.Add(await GetUserNotificationsAsync(user));
            }
            performanceIndicators.Add($"Total Notifier execution time: {chrono.Elapsed}");
            var result = userNotifications.ToImmutableArray();

            return(new ResultWithMetrologyProperties <NotifierResult>(new NotifierResult(result), ("ResultCount", result.Length.ToString())));
        }
示例#29
0
        protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request)
        {
            var tags       = DbContext.Tags.Where(tag => EF.Functions.Like(tag.Name, $"%{request.Filter}%")).OrderBy(tag => tag.Name);
            var totalCount = await tags.CountAsync();

            var pageCount = (int)Math.Ceiling((double)totalCount / request.PageSize);
            var pageTags  = tags.Skip((request.PageNo - 1) * request.PageSize).Take(request.PageSize);
            var result    = new Result(totalCount, pageCount, pageTags.Select(tag => new ResultTag(tag.Id, tag.Name, tag.TagsInCards.Count)));

            return(new ResultWithMetrologyProperties <Result>(result,
                                                              ("PageSize", request.PageSize.ToString()),
                                                              ("PageNo", request.PageNo.ToString()),
                                                              ("FilterLength", request.Filter.Length.ToString()),
                                                              ("TotalCount", result.TotalCount.ToString()),
                                                              ("PageCount", result.PageCount.ToString()),
                                                              ("TagCount", result.Tags.Count().ToString())));
        }
示例#30
0
        protected override async Task <ResultWithMetrologyProperties <IEnumerable <Result> > > DoRunAsync(Request request)
        {
            var nowUtc = runDate == null ? DateTime.UtcNow : runDate;

            var result = new List <Result>();
            var decks  = await DbContext.Decks.Where(deck => deck.Owner.Id == request.UserId).OrderBy(deck => deck.Description).Select(
                deck => new { deck.Id, deck.Description, deck.HeapingAlgorithmId, CardCount = deck.CardInDecks.Count }
                ).ToArrayAsync();

            foreach (var deck in decks)
            {
                var heaps = new Dictionary <int, HeapInfo>();

                var cardsInDeck = await DbContext.CardsInDecks.Where(cardInDeck => cardInDeck.DeckId == deck.Id).ToListAsync();

                cardsInDeck.ForEach(cardInDeck =>
                {
                    if (!heaps.ContainsKey(cardInDeck.CurrentHeap))
                    {
                        heaps.Add(cardInDeck.CurrentHeap, new HeapInfo(cardInDeck.CurrentHeap));
                    }
                    var heapInfo = heaps[cardInDeck.CurrentHeap];
                    heapInfo.TotalCardCount++;
                    if (cardInDeck.CurrentHeap != 0)
                    {
                        if (cardInDeck.ExpiryUtcTime <= nowUtc.Value)
                        {
                            heapInfo.ExpiredCardCount++;
                        }
                        else
                        {
                            if (cardInDeck.ExpiryUtcTime < heapInfo.NextExpiryUtcDate)
                            {
                                heapInfo.NextExpiryUtcDate = cardInDeck.ExpiryUtcTime;
                            }
                        }
                    }
                });

                result.Add(new Result(deck.Id, deck.Description, deck.HeapingAlgorithmId, deck.CardCount,
                                      heaps.Values.Select(heapInfo => new ResultHeap(heapInfo.HeapId, heapInfo.TotalCardCount, heapInfo.ExpiredCardCount, heapInfo.NextExpiryUtcDate)))
                           );
            }

            return(new ResultWithMetrologyProperties <IEnumerable <Result> >(result, ("DeckCount", result.Count.ToString())));
        }