public async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest httpRequest, ExecutionContext executionContext) { var response = new HttpResponseMessage { Content = new StringContent(await _razorEngine.GetWebPage(executionContext, Constants.Pages.Faq, new { })) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return(response); }
public async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest httpRequest, ExecutionContext executionContext) { using (var context = _contextFactory.Create()) { var today = DateTime.Today; var startOfYear = new DateTime(today.Year, 1, 1); var daysSinceStartOfYear = Math.Abs((today - startOfYear).Days); var totalMentionsThisYear = await context.Articles .Where(x => x.MentionsHitler) .Where(x => x.PublishedDate > startOfYear) .CountAsync(); var averageMentionsPerDay = Math.Round((decimal)totalMentionsThisYear / (decimal)daysSinceStartOfYear, 2); // This will be null if they haven't mentioned since the start of the year var mostMentionsInOneDay = await context.Articles .Where(x => x.MentionsHitler) .Where(x => x.PublishedDate > startOfYear) .GroupBy(x => new { x.PublishedDate.Date, }) .Select(x => new { Date = x.Key, Count = x.Count() }) .OrderByDescending(x => x.Count) .FirstOrDefaultAsync(); var response = new HttpResponseMessage { Content = new StringContent(await _razorEngine.GetWebPage(executionContext, Constants.Pages.Overview, new OverviewModel { AverageMentions = averageMentionsPerDay, TotalMentions = totalMentionsThisYear, MostMentionedCount = mostMentionsInOneDay.Count })) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return(response); } }
public async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest httpRequest, ExecutionContext executionContext) { using (var context = _contextFactory.Create()) { var fifteenDaysAgo = DateTime.Today.AddDays(-15); var hitlerMentionsInLastFifteenDays = await context.Articles .Where(x => x.MentionsHitler == true) .Where(x => x.PublishedDate >= fifteenDaysAgo) .GroupBy(x => x.PublishedDate.Date) .Select(x => new { Date = x.Key, Count = x.Count() }).ToListAsync(); var model = new HistoryModel(); while (fifteenDaysAgo < DateTime.Today) { var hitlerMentionsForDay = hitlerMentionsInLastFifteenDays .SingleOrDefault(x => x.Date == fifteenDaysAgo.Date); model.Labels.Add(fifteenDaysAgo.ToString("dd/MM/yyyy")); model.Data.Add(hitlerMentionsForDay?.Count ?? 0); fifteenDaysAgo = fifteenDaysAgo.AddDays(1); } var response = new HttpResponseMessage { Content = new StringContent(await _razorEngine.GetWebPage(executionContext, Constants.Pages.History, model)) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return(response); } }
public async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest httpRequest, ExecutionContext executionContext) { using (var context = _contextFactory.Create()) { var today = DateTime.Today; var articlesMentioningHitler = await context.Articles .Where(x => x.MentionsHitler) .Where(x => x.PublishedDate.Date == today) .ToListAsync(); var lastMention = await context.Articles .Where(x => x.MentionsHitler) .OrderByDescending(x => x.PublishedDate) .FirstOrDefaultAsync(); var lastMentionDaysAgo = 0; if (lastMention != null) { lastMentionDaysAgo = (today - lastMention.PublishedDate.Date).Days; } var response = new HttpResponseMessage { Content = new StringContent(await _razorEngine.GetWebPage(executionContext, Constants.Pages.Index, new IndexModel { Articles = articlesMentioningHitler, PreviousMention = lastMentionDaysAgo })) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return(response); } }