38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
|
|
using Elecciones.Core.DTOs.ApiResponses;
|
||
|
|
using Elecciones.Database;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace Elecciones.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public class CatalogosController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly EleccionesDbContext _dbContext;
|
||
|
|
|
||
|
|
public CatalogosController(EleccionesDbContext dbContext)
|
||
|
|
{
|
||
|
|
_dbContext = dbContext;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("municipios")]
|
||
|
|
public async Task<IActionResult> GetMunicipios()
|
||
|
|
{
|
||
|
|
// El NivelId 5 corresponde a "Municipio" según los datos que hemos visto.
|
||
|
|
var municipios = await _dbContext.AmbitosGeograficos
|
||
|
|
.AsNoTracking()
|
||
|
|
.Where(a => a.NivelId == 5 && a.MunicipioId != null)
|
||
|
|
.Select(a => new MunicipioSimpleDto
|
||
|
|
{
|
||
|
|
Id = a.MunicipioId!,
|
||
|
|
Nombre = a.Nombre
|
||
|
|
})
|
||
|
|
.OrderBy(m => m.Nombre)
|
||
|
|
.ToListAsync();
|
||
|
|
|
||
|
|
return Ok(municipios);
|
||
|
|
}
|
||
|
|
}
|