TFS_ANTIGUO/TPAtesa_CuentasCobrar/CuentasCobrar.API/Controllers/PagoController.cs

266 lines
10 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 PagoController : ControllerBase
{
private readonly IPagoRepo _PagoRepo;
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;
public PagoController(IPagoRepo pagoRepo, IMapper mapper, IConfiguration configuration)
{
_PagoRepo = pagoRepo;
_mapper = mapper;
_configuration = configuration;
}
/// <summary>
        /// Obtiene todos los pagos
        /// </summary>
        /// <returns>
        /// Retorna todos las pagos de la base de datos
        /// </returns>
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<PagoDTO>))]
[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 pagoes = await _PagoRepo.Get();
if (pagoes == null)
{
throw new BadRequestException("Hubo un problema al obtener los pagos");
}
var PagoesDto = _mapper.Map<IEnumerable<PagoDTO>>(pagoes);
return Ok(PagoesDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
/// <summary>
        /// Obtiene los pagos aplicando los filtros de paginación
        /// </summary>
        /// <returns>
        /// Retorna las pagos de la base de datos paginados
        /// </returns>
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<PagoDTO>))]
[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] PagoQueryFilter 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 pagoes = await _PagoRepo.Get(filters);
if (pagoes == null)
{
throw new BadRequestException("Hubo un problema al obtener los pagos");
}
var PagoesDto = _mapper.Map<IEnumerable<PagoDTO>>(pagoes);
var metadata = new
{
pagoes.NumeroDePagina,
pagoes.RegistrosTotales,
pagoes.RegistrosPorPagina,
pagoes.PaginasTotales,
pagoes.HayPaginaSiguiente,
pagoes.HayPaginaAnterior,
pagoes.NumeroPaginaSiguiente,
pagoes.NumeroPaginaAnterior
};
Response.Headers.Add("Pagination-Info", JsonConvert.SerializeObject(metadata));
return Ok(PagoesDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
/// <summary>
        /// Obtiene el pago especificado por su id
        /// </summary>
        /// <param name="noRecibo"></param>
        /// <returns>Retorna los datos del banco específicado</returns>
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PagoDTO))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
[HttpGet("{noRecibo}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")]
public async Task<IActionResult> Get(int noRecibo)
{
try
{
var pago = await _PagoRepo.Get(noRecibo);
if (pago == null)
{
throw new BadRequestException("El pago no existe");
}
var PagoDto = _mapper.Map<PagoDTO>(pago);
return Ok(PagoDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
/// <summary>
        /// Agrega un pago
        /// </summary>
        /// <param name="pagoDto"></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(PagoDTO pagoDto)
{
try
{
var pago = _mapper.Map<Pago>(pagoDto);
var facturas = _mapper.Map<IEnumerable<Factura>>(pagoDto.Facturas).ToList();
bool agregado = await _PagoRepo.Post(pago, facturas);
if (agregado == false)
{
throw new BadRequestException("Hubo un problema al agregar el pago");
}
return Ok(agregado);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
//return Ok(pagoDto);
}
/// <summary>
        /// Actualiza el pago especificado por su id
        /// </summary>
        /// <param name="noRecibo"></param>
        /// <param name="pagoDto"></param>
        [ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
[HttpPut("{noRecibo}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
public async Task<IActionResult> Put(int noRecibo, PagoDTO pagoDto)
{
try
{
var pagoTemp = await _PagoRepo.Get(noRecibo);
if (pagoTemp == null)
{
throw new BadRequestException("El pago a modificar no existe");
}
var pago = _mapper.Map<Pago>(pagoDto);
pago.NoRecibo = noRecibo;
bool modificado = await _PagoRepo.Put(pago);
if (modificado == false)
{
throw new BadRequestException("Hubo un problema al modificar el pago");
}
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 pago especificado por su id
        /// </summary>
        /// <param name="noRecibo"></param>
        [ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
[HttpDelete("{noRecibo}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
public async Task<IActionResult> Delete(int noRecibo)
{
try
{
var pagoTemp = await _PagoRepo.Get(noRecibo);
if (pagoTemp == null)
{
throw new BadRequestException("El pago a eliminar no existe");
}
bool eliminado = await _PagoRepo.Delete(noRecibo);
if (eliminado == false)
{
throw new BadRequestException("Hubo un problema al eliminar el pago");
}
return Ok(eliminado);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
}
}