-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
84 lines (68 loc) · 2.26 KB
/
Program.cs
File metadata and controls
84 lines (68 loc) · 2.26 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Microsoft.EntityFrameworkCore;
using NC2025_MinimalAPI_NET8.Data;
using NC2025_MinimalAPI_NET8.Endpoints;
using NC2025_MinimalAPI_NET8.Repositories.Implementations;
using NC2025_MinimalAPI_NET8.Repositories.Interfaces;
using Microsoft.OpenApi.Models;
using Scalar.AspNetCore;
using Swashbuckle.AspNetCore.Swagger;
var builder = WebApplication.CreateBuilder(args);
// Aggiunta supporto a OpenAPI (Swagger)
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
// Dependency Injection
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
var app = builder.Build();
// Attività da effettuare sul DB (Migrations e Seed)
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Applica eventuali migrazioni in sospeso
dbContext.Database.Migrate();
// Effettua il seed iniziale
DataSeeder.Seed(dbContext);
}
if (app.Environment.IsDevelopment())
{
// Aggiunta supporto a OpenAPI (Swagger)
var oaName = "NC2025_MinimalAPI_NET8";
var oaEndpoint = "/openapi/v1.json";
app.UseSwagger();
// Swashbuckle.AspNetCore.SwaggerUI
// default URL: /swagger
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint(oaEndpoint, oaName);
});
// Swashbuckle.AspNetCore.ReDoc
// default URL: /api-docs
app.UseReDoc(c =>
{
c.DocumentTitle = oaName;
c.SpecUrl = oaEndpoint;
});
// Scalar.AspNetCore
// default URL: /scalar
app.MapScalarApiReference();
}
// Metodi Minimal API
app.MapGet("/", (HttpRequest request) =>
{
var dic = request.Query
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
return Results.Json(dic);
});
app.MapPost("/", (HttpRequest request) =>
{
var dic = request.HasFormContentType
? request.Form.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToString())
: new Dictionary<string, string>();
return Results.Json(dic);
});
// Endpoint Minimal API
app.MapCategoriesEndpoints();
app.MapProductsEndpoints();
app.Run();