feat: Crea el controlador de Sectores con el endpoint GET para consultar todos
This commit is contained in:
44
backend/Controllers/SectoresController.cs
Normal file
44
backend/Controllers/SectoresController.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Dapper;
|
||||
using Inventario.API.Data;
|
||||
using Inventario.API.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Inventario.API.Controllers
|
||||
{
|
||||
// [ApiController] habilita comportamientos estándar de API como la validación automática.
|
||||
[ApiController]
|
||||
// [Route("api/[controller]")] define la URL base para este controlador.
|
||||
// "[controller]" se reemplaza por el nombre de la clase sin "Controller", es decir, "api/sectores".
|
||||
[Route("api/[controller]")]
|
||||
public class SectoresController : ControllerBase
|
||||
{
|
||||
// Campo privado para guardar la referencia a nuestro contexto de Dapper.
|
||||
private readonly DapperContext _context;
|
||||
|
||||
// El constructor recibe el DapperContext a través de la inyección de dependencias que configuramos en Program.cs.
|
||||
public SectoresController(DapperContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// [HttpGet] marca este método para que responda a peticiones GET a la ruta base ("api/sectores").
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> ConsultarSectores()
|
||||
{
|
||||
// Definimos nuestra consulta SQL. Es buena práctica listar las columnas explícitamente.
|
||||
var query = "SELECT Id, Nombre FROM dbo.sectores ORDER BY Nombre;";
|
||||
|
||||
// Creamos una conexión a la base de datos. El 'using' asegura que la conexión se cierre y se libere correctamente, incluso si hay errores.
|
||||
using (var connection = _context.CreateConnection())
|
||||
{
|
||||
// Usamos el método QueryAsync de Dapper.
|
||||
// <Sector> le dice a Dapper que mapee cada fila del resultado a un objeto de nuestra clase Sector.
|
||||
// 'await' espera a que la base de datos responda sin bloquear el servidor.
|
||||
var sectores = await connection.QueryAsync<Sector>(query);
|
||||
|
||||
// Ok() crea una respuesta HTTP 200 OK y serializa la lista de sectores a formato JSON.
|
||||
return Ok(sectores);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,47 +6,21 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// --- AÑADIR ESTA LÍNEA ---
|
||||
builder.Services.AddSingleton<DapperContext>();
|
||||
// -------------------------
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c =>
|
||||
{
|
||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Inventario API V1");
|
||||
c.RoutePrefix = string.Empty;
|
||||
});
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
app.MapControllers();
|
||||
app.Run();
|
||||
|
||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||||
{
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Inventario.API")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+80eeac45d8b90ed69a6479e9d3dcadde5e317a90")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Inventario.API")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Inventario.API")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
Reference in New Issue
Block a user