diff --git a/backend/Controllers/SectoresController.cs b/backend/Controllers/SectoresController.cs new file mode 100644 index 0000000..7858464 --- /dev/null +++ b/backend/Controllers/SectoresController.cs @@ -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 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. + // 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(query); + + // Ok() crea una respuesta HTTP 200 OK y serializa la lista de sectores a formato JSON. + return Ok(sectores); + } + } + } +} \ No newline at end of file diff --git a/backend/Program.cs b/backend/Program.cs index 584ad34..ef8d541 100644 --- a/backend/Program.cs +++ b/backend/Program.cs @@ -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(); -// ------------------------- - -// 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.Run(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/backend/obj/Debug/net9.0/Inventario.API.AssemblyInfo.cs b/backend/obj/Debug/net9.0/Inventario.API.AssemblyInfo.cs index f896cd1..ae9f5c7 100644 --- a/backend/obj/Debug/net9.0/Inventario.API.AssemblyInfo.cs +++ b/backend/obj/Debug/net9.0/Inventario.API.AssemblyInfo.cs @@ -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")]