public List <ArticleDTO> GetTopArticlesByCategoryId(int categoryId, TimePeriodValue timePeriodValue = TimePeriodValue.DAY) { var categorySources = db.Categories.FirstOrDefault(c => c.Id == categoryId).Sources.Select(s => s.Id); var categoryArticles = db.Articles.Where(a => a.Sources.Any(s => categorySources.Contains(s.Id))); categoryArticles = GetTopArticles(categoryArticles, timePeriodValue); return(categoryArticles.ProjectTo <ArticleDTO>().ToList()); }
private IQueryable <Article> GetTopArticles(IQueryable <Article> articles, TimePeriodValue timePeriodValue) { DateTime?startDate = DateTime.UtcNow.AddHours(-(int)timePeriodValue); // Filter by date period articles = articles.Where(a => startDate <= a.CreatedDate && a.EngagementCount >= 1); // Order by share count articles = articles.OrderByDescending(a => a.EngagementCount); // Get top 10 distinct articles articles = articles.Take(10); return(articles); }
public TimePeriod(TimePeriodValue value, string name) { Id = (int)value; Name = name; }