Beispiel #1
0
        /// <inheritdoc />
        public async Task <List <PopularCategoryViewModel> > GetPopularCategories()
        {
            using var scope = ServiceProvider.CreateScope();
            var context = scope.ServiceProvider.GetRequiredService <ApplicationContext>();

            var categoriesEnumerable = context.Categories.AsSplitQuery().AsNoTracking().AsEnumerable();

            var categories = categoriesEnumerable.Where(x => CategoryRecords.Any(e => x.Id == e.Record))
                             .OrderBy(x => CategoryRecords.FirstOrDefault(c => x.Id == c.Record).Score).ToList();

            // If there are no popular categories get all of them.
            if (!categories.Any())
            {
                return((await context.Categories.AsNoTracking().ToListAsync())
                       .Select(x => new PopularCategoryViewModel()
                {
                    Id = x.Id,
                    Name = x.Name,
                    Score = 0
                }).ToList());
            }

            return(categories.Select(x => new PopularCategoryViewModel()
            {
                Id = x.Id,
                Name = x.Name,
                Score = CategoryRecords.Find(c => x.Id == c.Record).Score
            }).ToList());
        }
Beispiel #2
0
        /// <inheritdoc />
        public async Task PopulariseCategory(CancellationToken cancellationToken, Guid categoryId, DateTime created)
        {
            if (!cancellationToken.IsCancellationRequested)
            {
                Logger.LogInformation("Popularising category {Id}", categoryId);

                var popularityRecord = CategoryRecords.FirstOrDefault(x => x.Record == categoryId);

                // if the category is already popular
                if (popularityRecord != null)
                {
                    Logger.LogInformation("Category {Id} is already popular, making more popular", categoryId);

                    popularityRecord.Updated = created;
                    popularityRecord.Score++;
                }
                else
                {
                    using var scope = ServiceProvider.CreateScope();
                    var context = scope.ServiceProvider.GetRequiredService <ApplicationContext>();

                    var c = await context.Categories.FirstOrDefaultAsync(x => x.Id == categoryId, cancellationToken);

                    if (c == null)
                    {
                        Logger.LogInformation("Category not found");
                        return;
                    }

                    CategoryRecords.Add(new PopularityRecord
                    {
                        Added   = created,
                        Updated = created,
                        Record  = categoryId,
                        Score   = 1
                    });
                }

                // sends popular categories to all web-socket clients
                await PopularityHubContext.Clients.All.SendAsync("categories", await GetPopularCategories());

                Logger.LogInformation("Popularised category {Id}", categoryId);
            }
        }