190 lines
8.1 KiB
C#
190 lines
8.1 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 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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene todos los pagoContieneFacturas
|
||
/// </summary>
|
||
/// <returns>
|
||
/// Retorna todos las pagoContieneFacturas de la base de datos
|
||
/// </returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<PagoContieneFacturaDTO>))]
|
||
[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 pagoContieneFacturas = await _PagoContieneFacturaRepo.Get();
|
||
if (pagoContieneFacturas == null)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al obtener los pagos relacionados con facturas");
|
||
}
|
||
var PagoContieneFacturaesDto = _mapper.Map<IEnumerable<PagoContieneFacturaDTO>>(pagoContieneFacturas);
|
||
return Ok(PagoContieneFacturaesDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene todas las facturas de un pago dado
|
||
/// </summary>
|
||
/// <returns>
|
||
/// Retorna todas las facturas de un pago dado de la base de datos
|
||
/// </returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<FacturaDTO>))]
|
||
[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<IActionResult> 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<IEnumerable<Factura>>(facturasDePago);
|
||
return Ok(facturasDePagoDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene los pagoContieneFacturas aplicando los filtros de paginación
|
||
/// </summary>
|
||
/// <returns>
|
||
/// Retorna las pagoContieneFacturas de la base de datos paginados
|
||
/// </returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<PagoContieneFacturaDTO>))]
|
||
[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 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<IEnumerable<PagoContieneFacturaDTO>>(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;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// Agrega un pagoContieneFactura
|
||
/// </summary>
|
||
/// <param name="pagoContieneFacturaDto"></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(PagoContieneFacturaDTO pagoContieneFacturaDto)
|
||
{
|
||
try
|
||
{
|
||
var pagoContieneFactura = _mapper.Map<PagoContieneFactura>(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;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|