add: implementacao inicial

This commit is contained in:
2025-08-25 00:06:43 -03:00
parent 2378741455
commit cf9f2a1179
9 changed files with 203 additions and 28 deletions

View File

@@ -1,41 +1,56 @@
// Note: repository implementation removed for workshop exercise (TODOs in project files)
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>
{
o.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "CampusWorkshops API",
Version = "v1",
Description = "API para gestão de workshops do campus (MVP in-memory)."
});
});
// DI
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
// Exception handler that returns RFC7807 ProblemDetails for unhandled errors
app.UseExceptionHandler(errApp =>
{
app.MapOpenApi();
}
errApp.Run(async context =>
{
var feature = context.Features.Get<IExceptionHandlerFeature>();
var ex = feature?.Error;
var pd = new ProblemDetails
{
Title = "An unexpected error occurred.",
Status = StatusCodes.Status500InternalServerError,
Detail = app.Environment.IsDevelopment() ? ex?.Message : null
};
context.Response.StatusCode = pd.Status.Value;
context.Response.ContentType = "application/problem+json";
await context.Response.WriteAsJsonAsync(pd);
});
});
app.UseHttpsRedirection();
var summaries = new[]
app.UseSwagger();
app.UseSwaggerUI(c =>
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
c.SwaggerEndpoint("/swagger/v1/swagger.json", "CampusWorkshops API v1");
c.RoutePrefix = "swagger"; // serve at /swagger
});
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");
app.MapControllers();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}