-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathWeatherForecastFunction.cs
More file actions
57 lines (48 loc) · 1.47 KB
/
WeatherForecastFunction.cs
File metadata and controls
57 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Net;
using BlazorApp.Shared;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace Api.Functions
{
public class HttpTrigger
{
private readonly ILogger _logger;
public HttpTrigger(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HttpTrigger>();
}
[Function("WeatherForecast")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
{
var randomNumber = new Random();
var temp = 0;
var result = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = temp = randomNumber.Next(-20, 55),
Summary = GetSummary(temp)
}).ToArray();
var response = req.CreateResponse(HttpStatusCode.OK);
response.WriteAsJsonAsync(result);
return response;
}
private string GetSummary(int temp)
{
var summary = "Mild";
if (temp >= 32)
{
summary = "Hot";
}
else if (temp <= 16 && temp > 0)
{
summary = "Cold";
}
else if (temp <= 0)
{
summary = "Freezing";
}
return summary;
}
}
}