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 PagoContieneFacturaController : ControllerBase
{
private readonly IPagoContieneFacturaRepo _PagoContieneFacturaRepo;
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;
public PagoContieneFacturaController(IPagoContieneFacturaRepo pagoContieneFacturaRepo, IMapper mapper, IConfiguration configuration)
{
_PagoContieneFacturaRepo = pagoContieneFacturaRepo;
_mapper = mapper;
_configuration = configuration;
}
///
/// Obtiene todos los pagoContieneFacturas
///
///
/// Retorna todos las pagoContieneFacturas 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 pagoContieneFacturas = await _PagoContieneFacturaRepo.Get();
if (pagoContieneFacturas == null)
{
throw new BadRequestException("Hubo un problema al obtener los pagos relacionados con facturas");
}
var PagoContieneFacturaesDto = _mapper.Map>(pagoContieneFacturas);
return Ok(PagoContieneFacturaesDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
///
/// Obtiene todas las facturas de un pago dado
///
///
/// Retorna todas las facturas de un pago dado 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("FacturasDePago/{noRecibo}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")]
public async Task Get(int noRecibo)
{
try
{
var facturasDePago = await _PagoContieneFacturaRepo.GetFacturasDePago(noRecibo);
if (facturasDePago == null)
{
throw new BadRequestException("Hubo un problema al obtener los pagos relacionados con facturas");
}
var facturasDePagoDto = _mapper.Map>(facturasDePago);
return Ok(facturasDePagoDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
///
/// Obtiene los pagoContieneFacturas aplicando los filtros de paginación
///
///
/// Retorna las pagoContieneFacturas 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 pagoContieneFacturas = await _PagoContieneFacturaRepo.Get(filters);
if (pagoContieneFacturas == null)
{
throw new BadRequestException("Hubo un problema al obtener los pagos relacionados con facturas");
}
var PagoContieneFacturasDto = _mapper.Map>(pagoContieneFacturas);
var metadata = new
{
pagoContieneFacturas.NumeroDePagina,
pagoContieneFacturas.RegistrosTotales,
pagoContieneFacturas.RegistrosPorPagina,
pagoContieneFacturas.PaginasTotales,
pagoContieneFacturas.HayPaginaSiguiente,
pagoContieneFacturas.HayPaginaAnterior,
pagoContieneFacturas.NumeroPaginaSiguiente,
pagoContieneFacturas.NumeroPaginaAnterior
};
Response.Headers.Add("Pagination-Info", JsonConvert.SerializeObject(metadata));
return Ok(PagoContieneFacturasDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
///
/// Agrega un pagoContieneFactura
///
///
[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(PagoContieneFacturaDTO pagoContieneFacturaDto)
{
try
{
var pagoContieneFactura = _mapper.Map(pagoContieneFacturaDto);
bool agregado = await _PagoContieneFacturaRepo.Post(pagoContieneFactura);
if (agregado == false)
{
throw new BadRequestException("Hubo un problema al agregar el pago relacionado a factura");
}
return Ok(agregado);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
}
}