using AutoMapper; using CuentasCobrar.CORE.DTOs; using CuentasCobrar.CORE.Entities; using CuentasCobrar.CORE.Exceptions; using CuentasCobrar.CORE.Interfaces; using CuentasCobrar.CORE.QueryFilters; using CuentasCobrar.CORE.ResponseObjects; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System.Net; namespace CuentasCobrar.API.Controllers { [Route("api/[controller]")] [Produces("application/json")] [ApiController] public class PaisController : ControllerBase { private readonly IPaisRepo _PaisRepo; private readonly IMapper _mapper; private readonly IConfiguration _configuration; public PaisController(IPaisRepo paisRepo, IMapper mapper, IConfiguration configuration) { _PaisRepo = paisRepo; _mapper = mapper; _configuration = configuration; } ///         /// Obtiene todos los países         ///         ///         /// Retorna todos las países de la base de datos         /// [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpGet("obtenerTodos/"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")] public async Task Get() { try { var paises = await _PaisRepo.Get(); if (paises == null) { throw new BadRequestException("Hubo un problema al obtener los paises"); } var PaisesDto = _mapper.Map>(paises); return Ok(PaisesDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Obtiene los países aplicando los filtros de paginación         ///         ///         /// Retorna las países de la base de datos paginados         /// [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpGet, Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")] public async Task Get([FromQuery] PaginacionQueryFilter filters) { try { int NumeroDePagina = _configuration.GetValue("PaginationDefaultConfig:NumeroDePagina"); int RegistrosPorPagina = _configuration.GetValue("PaginationDefaultConfig:RegistrosPorPagina"); filters.NumeroDePagina = filters.NumeroDePagina == 0 ? NumeroDePagina : filters.NumeroDePagina; filters.RegistrosPorPagina = filters.RegistrosPorPagina == 0 ? RegistrosPorPagina : filters.RegistrosPorPagina; var paises = await _PaisRepo.Get(filters); if (paises == null) { throw new BadRequestException("Hubo un problema al obtener los países"); } var PaisesDto = _mapper.Map>(paises); var metadata = new { paises.NumeroDePagina, paises.RegistrosTotales, paises.RegistrosPorPagina, paises.PaginasTotales, paises.HayPaginaSiguiente, paises.HayPaginaAnterior, paises.NumeroPaginaSiguiente, paises.NumeroPaginaAnterior }; Response.Headers.Add("Pagination-Info", JsonConvert.SerializeObject(metadata)); return Ok(PaisesDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Obtiene el país especificado por su id         ///         ///         /// Retorna los datos del banco específicado [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PaisDTO))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpGet("{idPais}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")] public async Task Get(int idPais) { try { var pais = await _PaisRepo.Get(idPais); if (pais == null) { throw new BadRequestException("El país no existe"); } var PaisDto = _mapper.Map(pais); return Ok(PaisDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Agrega un país         ///         ///         [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpPost, Authorize(Roles = "ADMINISTRADOR,OPERATIVO")] public async Task Post(PaisDTO paisDto) { try { var pais = _mapper.Map(paisDto); bool agregado = await _PaisRepo.Post(pais); if (agregado == false) { throw new BadRequestException("Hubo un problema al agregar el país"); } return Ok(agregado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Actualiza el país especificado por su id         ///         ///         ///         [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpPut("{idPais}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")] public async Task Put(int idPais, PaisDTO paisDto) { try { var paisTemp = await _PaisRepo.Get(idPais); if (paisTemp == null) { throw new BadRequestException("El país a modificar no existe"); } var pais = _mapper.Map(paisDto); pais.IdPais = idPais; bool modificado = await _PaisRepo.Put(pais); if (modificado == false) { throw new BadRequestException("Hubo un problema al modificar el país"); } return Ok(modificado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Elimina el país especificado por su id         ///         ///         [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpDelete("{idPais}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")] public async Task Delete(int idPais) { try { var paisTemp = await _PaisRepo.Get(idPais); if (paisTemp == null) { throw new BadRequestException("El pais a eliminar no existe"); } bool eliminado = await _PaisRepo.Delete(idPais); if (eliminado == false) { throw new BadRequestException("Hubo un problema al eliminar el país"); } return Ok(eliminado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } } }