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