Skip to content

Commit

Permalink
chore: add endpoints folder
Browse files Browse the repository at this point in the history
  • Loading branch information
blasther12 authored Jun 6, 2024
1 parent 30f5b2a commit 2f7b017
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
36 changes: 36 additions & 0 deletions src/Saphira.Api/Endpoints/WeatherforecastEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Saphira.Api.Endpoints
{
public static class WeatherforecastEndpoints
{
public static void MapWeatherforecastEndpoints(this IEndpointRouteBuilder builder)
{
var group = builder.MapGroup("api/weatherforecast")
.WithTags("Weatherforecast");

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

group.MapGet("/", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
}

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
}
30 changes: 4 additions & 26 deletions src/Saphira.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Saphira.Api.Endpoints;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -17,31 +19,7 @@

app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.MapHealthChecks("/api/health");
app.MapWeatherforecastEndpoints();
app.MapHealthChecks("/healthz");

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

0 comments on commit 2f7b017

Please sign in to comment.