public async Task <IActionResult> GetLatestNews() { var iNews = await _dbContext.News.Include(p => p.Photo).Where(inews => inews.IsImportant == true).OrderByDescending(x => x.AddedAt).ToListAsync(); var importantNewsFromAllSections = iNews.Take(7).ToList(); var importantNewsToReturn = NewsForHomeDto.Map(importantNewsFromAllSections).ToList(); var polandNews = await GetNewsForHomeComponent("poland", importantNewsFromAllSections); var policyNews = await GetNewsForHomeComponent("policy", importantNewsFromAllSections); var sportNews = await GetNewsForHomeComponent("sport", importantNewsFromAllSections); var businessNews = await GetNewsForHomeComponent("business", importantNewsFromAllSections); var worldNews = await GetNewsForHomeComponent("world", importantNewsFromAllSections); var lNews = await _dbContext.News.Include(p => p.Photo).OrderByDescending(x => x.AddedAt).ToListAsync(); var latestNews = LatestNewsDto.Map(lNews.Take(30).ToList()); var dataToReturn = new DataForHomeComponent() { ImportantNews = importantNewsToReturn, SectionPolandNews = polandNews, SectionBusinessNews = businessNews, SectionPolicyNews = policyNews, SectionSportNews = sportNews, SectionWorldNews = worldNews, LatestNews = latestNews }; return(Ok(dataToReturn)); }
private async Task <List <NewsForHomeDto> > GetNewsForHomeComponent(string section, List <News> importantNewsFromAllSections) { var news = await _dbContext.News.Include(p => p.Photo) .Where(n => n.Section == section).OrderByDescending(x => x.AddedAt).ToListAsync(); var importantNewsFromThisSection = news.Except(importantNewsFromAllSections). Where(n => n.IsImportant == true).Take(2).ToList(); var newsToReturn = new List <NewsForHomeDto>(); foreach (var item in importantNewsFromThisSection) { newsToReturn.Add(NewsForHomeDto.Map(item)); } var importantNewsCount = importantNewsFromThisSection.Count(); int numberOfLatestNews; if (importantNewsCount == 2) { numberOfLatestNews = 4; } else if (importantNewsCount == 1) { numberOfLatestNews = 5; } else { numberOfLatestNews = 6; } var otherLatestNews = await _dbContext.News.Include(p => p.Photo) .Where(s => s.Section == section).Except(importantNewsFromThisSection) .OrderByDescending(x => x.AddedAt) .Take(numberOfLatestNews).ToListAsync(); foreach (var item in otherLatestNews) { newsToReturn.Add(NewsForHomeDto.Map(item)); } return(newsToReturn); }