265 lines
10 KiB
C#
265 lines
10 KiB
C#
|
|
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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene todos los países
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>
|
|||
|
|
/// Retorna todos las países de la base de datos
|
|||
|
|
/// </returns>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<PaisDTO>))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpGet("obtenerTodos/"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")]
|
|||
|
|
public async Task<IActionResult> Get()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var paises = await _PaisRepo.Get();
|
|||
|
|
if (paises == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al obtener los paises");
|
|||
|
|
}
|
|||
|
|
var PaisesDto = _mapper.Map<IEnumerable<PaisDTO>>(paises);
|
|||
|
|
return Ok(PaisesDto);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene los países aplicando los filtros de paginación
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>
|
|||
|
|
/// Retorna las países de la base de datos paginados
|
|||
|
|
/// </returns>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<PaisDTO>))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpGet, Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")]
|
|||
|
|
public async Task<IActionResult> Get([FromQuery] PaginacionQueryFilter filters)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
int NumeroDePagina = _configuration.GetValue<int>("PaginationDefaultConfig:NumeroDePagina");
|
|||
|
|
int RegistrosPorPagina = _configuration.GetValue<int>("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<IEnumerable<PaisDTO>>(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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene el país especificado por su id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idPais"></param>
|
|||
|
|
/// <returns>Retorna los datos del banco específicado</returns>
|
|||
|
|
[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<IActionResult> 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<PaisDTO>(pais);
|
|||
|
|
return Ok(PaisDto);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Agrega un país
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="paisDto"></param>
|
|||
|
|
[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<IActionResult> Post(PaisDTO paisDto)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var pais = _mapper.Map<Pais>(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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Actualiza el país especificado por su id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idPais"></param>
|
|||
|
|
/// <param name="paisDto"></param>
|
|||
|
|
[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<IActionResult> 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<Pais>(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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Elimina el país especificado por su id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idPais"></param>
|
|||
|
|
[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<IActionResult> 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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|