using Dapper; using Inventario.API.Data; using Microsoft.AspNetCore.Mvc; namespace Inventario.API.Controllers { [ApiController] [Route("api/[controller]")] public class DashboardController : ControllerBase { private readonly DapperContext _context; public DashboardController(DapperContext context) { _context = context; } public class StatItemDto { // Cambiamos el tipo de Label a string para que Dapper no tenga problemas // al leer el ram_installed (que es un int). Lo formatearemos en el frontend. public string Label { get; set; } = ""; public int Count { get; set; } } public class DashboardStatsDto { public IEnumerable OsStats { get; set; } = new List(); public IEnumerable SectorStats { get; set; } = new List(); public IEnumerable CpuStats { get; set; } = new List(); public IEnumerable RamStats { get; set; } = new List(); // <-- 1. AƱadir propiedad para RAM } [HttpGet("stats")] public async Task GetDashboardStats() { var osQuery = @" SELECT Os AS Label, COUNT(Id) AS Count FROM dbo.equipos WHERE Os IS NOT NULL AND Os != '' GROUP BY Os ORDER BY Count DESC;"; var sectorQuery = @" SELECT s.Nombre AS Label, COUNT(e.Id) AS Count FROM dbo.equipos e JOIN dbo.sectores s ON e.sector_id = s.id GROUP BY s.Nombre ORDER BY Count DESC;"; var cpuQuery = @" SELECT Cpu AS Label, COUNT(Id) AS Count FROM dbo.equipos WHERE Cpu IS NOT NULL AND Cpu != '' GROUP BY Cpu ORDER BY Count DESC;"; var ramQuery = @" SELECT CAST(ram_installed AS VARCHAR) AS Label, COUNT(Id) AS Count FROM dbo.equipos WHERE ram_installed > 0 GROUP BY ram_installed ORDER BY ram_installed ASC;"; using (var connection = _context.CreateConnection()) { var osStats = await connection.QueryAsync(osQuery); var sectorStats = await connection.QueryAsync(sectorQuery); var cpuStats = await connection.QueryAsync(cpuQuery); var ramStats = await connection.QueryAsync(ramQuery); var result = new DashboardStatsDto { OsStats = osStats, SectorStats = sectorStats, CpuStats = cpuStats, RamStats = ramStats }; return Ok(result); } } } }