Skip to content

Commit

Permalink
Add Fastendpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ubhaya committed Jan 19, 2024
1 parent 4bacd75 commit 4796076
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CleanArchitecture.Maui.MobileUi.Shared.WeatherForecasts;
using FastEndpoints;

namespace CleanArchitecture.Maui.MobileUi.WebApi.Endpoints;

public class WeatherForecastEndpoint : EndpointWithoutRequest<IEnumerable<WeatherForecast>>
{
private readonly string[] _summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];
public override void Configure()
{
Verbs(Http.GET);
Routes("/weatherforecast");
AllowAnonymous();
}

public override async Task HandleAsync(CancellationToken cancellationToken)
{
var forecasts = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = _summaries[Random.Shared.Next(_summaries.Length)]
})
.ToArray();
await SendAsync(forecasts, cancellation: cancellationToken);
}
}
26 changes: 5 additions & 21 deletions src/content/CleanArchitecture.Maui/src/MobileUi/WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using FastEndpoints;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddFastEndpoints();

var app = builder.Build();

Expand All @@ -14,31 +17,12 @@
app.UseSwaggerUI();
}

app.UseFastEndpoints();
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.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
app.RunAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FastEndpoints" Version="5.21.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
Expand Down

0 comments on commit 4796076

Please sign in to comment.