| | | 1 | | using FAU.DataAccess; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | [ApiController] |
| | | 10 | | [Route("api/catalogo-items")] |
| | | 11 | | [Authorize] |
| | | 12 | | public class CatalogoItemsController : ControllerBase |
| | | 13 | | { |
| | | 14 | | private readonly ApplicationDbContext _context; |
| | | 15 | | private readonly ILogger<CatalogoItemsController> _logger; |
| | | 16 | | |
| | 3 | 17 | | public CatalogoItemsController(ApplicationDbContext context, ILogger<CatalogoItemsController> logger) |
| | | 18 | | { |
| | 3 | 19 | | _context = context; |
| | 3 | 20 | | _logger = logger; |
| | 3 | 21 | | } |
| | | 22 | | |
| | | 23 | | [HttpGet] |
| | | 24 | | public async Task<IActionResult> GetCatalogoItems([FromQuery] bool? vigente = null) |
| | | 25 | | { |
| | | 26 | | try |
| | | 27 | | { |
| | 3 | 28 | | var query = _context.CatalogoItems.AsNoTracking(); |
| | | 29 | | |
| | 3 | 30 | | if (vigente.HasValue) |
| | | 31 | | { |
| | 1 | 32 | | query = query.Where(item => item.Vigente == vigente.Value); |
| | | 33 | | } |
| | | 34 | | |
| | 3 | 35 | | var catalogos = await query |
| | 3 | 36 | | .OrderBy(item => item.OrdenCalculo) |
| | 3 | 37 | | .ThenBy(item => item.Nombre) |
| | 3 | 38 | | .Select(item => new CatalogoItemDto |
| | 3 | 39 | | { |
| | 3 | 40 | | Id = item.Id, |
| | 3 | 41 | | Codigo = item.Codigo, |
| | 3 | 42 | | Nombre = item.Nombre, |
| | 3 | 43 | | Tipo = item.Tipo, |
| | 3 | 44 | | Vigente = item.Vigente, |
| | 3 | 45 | | OrdenCalculo = item.OrdenCalculo |
| | 3 | 46 | | }) |
| | 3 | 47 | | .ToListAsync(); |
| | | 48 | | |
| | 3 | 49 | | return Ok(ApiResponse<IEnumerable<CatalogoItemDto>>.SuccessResult(catalogos)); |
| | | 50 | | } |
| | 0 | 51 | | catch (Exception ex) |
| | | 52 | | { |
| | 0 | 53 | | _logger.LogError(ex, "Error al obtener catálogo de ítems"); |
| | 0 | 54 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 55 | | } |
| | 3 | 56 | | } |
| | | 57 | | } |