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;
}
///
/// Obtiene todos los bancos
///
///
/// Retorna todos las bancos 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 bancos = await _BancoRepo.Get();
if (bancos == null)
{
throw new BadRequestException("Hubo un problema al obtener los bancos");
}
var bancosDto = _mapper.Map>(bancos);
return Ok(bancosDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
///
/// Obtiene los bancos aplicando los filtros de paginación
///
///
/// Retorna las bancos 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 bancos = await _BancoRepo.Get(filters);
if (bancos == null)
{
throw new BadRequestException("Hubo un problema al obtener los bancos");
}
var bancosDto = _mapper.Map>(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;
}
}
}
///
/// Obtiene el banco especificado por su id
///
///
/// Retorna los datos del banco específicado
[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 Get(int idBanco)
{
try
{
var banco = await _BancoRepo.Get(idBanco);
if (banco == null)
{
throw new BadRequestException("El banco no existe");
}
var BancoDto = _mapper.Map(banco);
return Ok(BancoDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
///
/// Agrega un banco
///
///
[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(BancoDTO bancoDto)
{
try
{
var banco = _mapper.Map(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;
}
}
}
///
/// Actualiza el banco especificado por su id
///
///
///
[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 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(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;
}
}
}
///
/// Elimina el banco especificado por su id
///
///
[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 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;
}
}
}
}
}