269 lines
10 KiB
C#
269 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 BancoController : ControllerBase
|
|||
|
|
{
|
|||
|
|
private readonly IBancoRepo _BancoRepo;
|
|||
|
|
private readonly IMapper _mapper;
|
|||
|
|
private readonly IConfiguration _configuration;
|
|||
|
|
|
|||
|
|
public BancoController(IBancoRepo bancoRepo, IMapper mapper, IConfiguration configuration)
|
|||
|
|
{
|
|||
|
|
_BancoRepo = bancoRepo;
|
|||
|
|
_mapper = mapper;
|
|||
|
|
_configuration = configuration;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene todos los bancos
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>
|
|||
|
|
/// Retorna todos las bancos de la base de datos
|
|||
|
|
/// </returns>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<BancoDTO>))]
|
|||
|
|
[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 bancos = await _BancoRepo.Get();
|
|||
|
|
if (bancos == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al obtener los bancos");
|
|||
|
|
}
|
|||
|
|
var bancosDto = _mapper.Map<IEnumerable<BancoDTO>>(bancos);
|
|||
|
|
return Ok(bancosDto);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene los bancos aplicando los filtros de paginación
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>
|
|||
|
|
/// Retorna las bancos de la base de datos paginados
|
|||
|
|
/// </returns>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<BancoDTO>))]
|
|||
|
|
[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 bancos = await _BancoRepo.Get(filters);
|
|||
|
|
if (bancos == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al obtener los bancos");
|
|||
|
|
}
|
|||
|
|
var bancosDto = _mapper.Map<IEnumerable<BancoDTO>>(bancos);
|
|||
|
|
|
|||
|
|
var metadata = new
|
|||
|
|
{
|
|||
|
|
bancos.NumeroDePagina,
|
|||
|
|
bancos.RegistrosTotales,
|
|||
|
|
bancos.RegistrosPorPagina,
|
|||
|
|
bancos.PaginasTotales,
|
|||
|
|
bancos.HayPaginaSiguiente,
|
|||
|
|
bancos.HayPaginaAnterior,
|
|||
|
|
bancos.NumeroPaginaSiguiente,
|
|||
|
|
bancos.NumeroPaginaAnterior
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
Response.Headers.Add("Pagination-Info", JsonConvert.SerializeObject(metadata));
|
|||
|
|
|
|||
|
|
return Ok(bancosDto);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene el banco especificado por su id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idBanco"></param>
|
|||
|
|
/// <returns>Retorna los datos del banco específicado</returns>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(BancoDTO))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpGet("{idBanco}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")]
|
|||
|
|
public async Task<IActionResult> Get(int idBanco)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var banco = await _BancoRepo.Get(idBanco);
|
|||
|
|
if (banco == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("El banco no existe");
|
|||
|
|
}
|
|||
|
|
var BancoDto = _mapper.Map<BancoDTO>(banco);
|
|||
|
|
return Ok(BancoDto);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Agrega un banco
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="bancoDto"></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(BancoDTO bancoDto)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var banco = _mapper.Map<Banco>(bancoDto);
|
|||
|
|
bool agregado = await _BancoRepo.Post(banco);
|
|||
|
|
if (agregado == false)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al agregar el banco");
|
|||
|
|
}
|
|||
|
|
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 banco especificado por su id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idBanco"></param>
|
|||
|
|
/// <param name="bancoDto"></param>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpPut("{idBanco}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
|
|||
|
|
public async Task<IActionResult> Put(int idBanco, BancoDTO bancoDto)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var bancoTemp = await _BancoRepo.Get(idBanco);
|
|||
|
|
if (bancoTemp == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("El banco a modificar no existe");
|
|||
|
|
}
|
|||
|
|
var banco = _mapper.Map<Banco>(bancoDto);
|
|||
|
|
banco.IdBanco = idBanco;
|
|||
|
|
bool modificado = await _BancoRepo.Put(banco);
|
|||
|
|
|
|||
|
|
if (modificado == false)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al modificar el banco");
|
|||
|
|
}
|
|||
|
|
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 banco especificado por su id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idBanco"></param>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpDelete("{idBanco}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
|
|||
|
|
public async Task<IActionResult> Delete(int idBanco)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var bancoTemp = await _BancoRepo.Get(idBanco);
|
|||
|
|
if (bancoTemp == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("El banco a eliminar no existe");
|
|||
|
|
}
|
|||
|
|
bool eliminado = await _BancoRepo.Delete(idBanco);
|
|||
|
|
if (eliminado == false)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al eliminar el banco");
|
|||
|
|
}
|
|||
|
|
return Ok(eliminado);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|