feat: Diseño del esquema de BD y configuración de Entity Framework Core
This commit is contained in:
@@ -1,41 +1,57 @@
|
||||
using Elecciones.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
// --- 1. Configuración de Servicios ---
|
||||
|
||||
// Añade la cadena de conexión y el DbContext para Entity Framework Core.
|
||||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||
builder.Services.AddDbContext<EleccionesDbContext>(options =>
|
||||
options.UseSqlServer(connectionString));
|
||||
|
||||
// Añade los servicios para los controladores de la API.
|
||||
builder.Services.AddControllers();
|
||||
|
||||
// Configura CORS para permitir que tu frontend (y www.eldia.com) consuman la API.
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:8600", "http://www.eldia.com", "http://elecciones2025.eldia.com") // Añade aquí los dominios que necesites
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Añade la configuración de Swagger/OpenAPI para la documentación de la API.
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
// --- 2. Configuración del Pipeline de Peticiones HTTP ---
|
||||
|
||||
// Habilita la UI de Swagger en el entorno de desarrollo.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
// Redirige las peticiones HTTP a HTTPS.
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
var summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
// Usa la política de CORS que definimos arriba.
|
||||
app.UseCors();
|
||||
|
||||
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");
|
||||
// Habilita la autorización (lo configuraremos si es necesario más adelante).
|
||||
app.UseAuthorization();
|
||||
|
||||
app.Run();
|
||||
// Mapea las rutas a los controladores de la API.
|
||||
app.MapControllers();
|
||||
|
||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||||
{
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
// Inicia la aplicación.
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user