| | | 1 | | using FAU.DataAccess; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace FAU.API.Controllers; |
| | | 9 | | |
| | | 10 | | [ApiController] |
| | | 11 | | [Route("api/[controller]")] |
| | | 12 | | [Authorize] |
| | | 13 | | public class UnidadesController : ControllerBase |
| | | 14 | | { |
| | | 15 | | private readonly ApplicationDbContext _context; |
| | | 16 | | |
| | 0 | 17 | | public UnidadesController(ApplicationDbContext context) => _context = context; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// GET /api/unidades |
| | | 21 | | /// Retorna las unidades vigentes con sus sub-unidades. |
| | | 22 | | /// </summary> |
| | | 23 | | [HttpGet] |
| | | 24 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerSubUnidades)] |
| | | 25 | | public async Task<IActionResult> GetUnidades() |
| | | 26 | | { |
| | 0 | 27 | | var unidades = await _context.Unidades |
| | 0 | 28 | | .Where(u => u.Vigente) |
| | 0 | 29 | | .Include(u => u.SubUnidades.Where(s => s.Vigente)) |
| | 0 | 30 | | .OrderBy(u => u.Denominacion) |
| | 0 | 31 | | .ToListAsync(); |
| | | 32 | | |
| | 0 | 33 | | return Ok(ApiResponse<object>.SuccessResult(unidades.Select(u => new |
| | 0 | 34 | | { |
| | 0 | 35 | | id = u.Id, |
| | 0 | 36 | | codigo = u.Codigo, |
| | 0 | 37 | | denominacion = u.Denominacion, |
| | 0 | 38 | | subUnidades = u.SubUnidades.Select(s => new |
| | 0 | 39 | | { |
| | 0 | 40 | | id = s.Id, |
| | 0 | 41 | | codigo = s.Codigo, |
| | 0 | 42 | | denominacion = s.Denominacion |
| | 0 | 43 | | }) |
| | 0 | 44 | | }))); |
| | 0 | 45 | | } |
| | | 46 | | } |