269 lines
11 KiB
C#
269 lines
11 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 ClienteController : ControllerBase
|
||
{
|
||
private readonly IClienteRepo _clienteRepo;
|
||
private readonly IMapper _mapper;
|
||
private readonly IConfiguration _configuration;
|
||
|
||
public ClienteController(IClienteRepo clienteRepo, IMapper mapper, IConfiguration configuration)
|
||
{
|
||
_clienteRepo = clienteRepo;
|
||
_mapper = mapper;
|
||
_configuration = configuration;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene todos los clientes
|
||
/// </summary>
|
||
/// <returns>
|
||
/// Retorna todos las clientes de la base de datos
|
||
/// </returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<ClienteDTO>))]
|
||
[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 clientes = await _clienteRepo.Get();
|
||
if (clientes == null)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al obtener los clientes");
|
||
}
|
||
var ClientesDto = _mapper.Map<IEnumerable<ClienteDTO>>(clientes);
|
||
return Ok(ClientesDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene los clientes aplicando los filtros de paginación y de cliente
|
||
/// </summary>
|
||
/// <returns>
|
||
/// Retorna las clientes de la base de datos paginados
|
||
/// </returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<ClienteDTO>))]
|
||
[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] ClienteQueryFilter 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 clientes = await _clienteRepo.Get(filters);
|
||
if (clientes == null)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al obtener los clientes");
|
||
}
|
||
var clientesDto = _mapper.Map<IEnumerable<ClienteDTO>>(clientes);
|
||
|
||
var metadata = new
|
||
{
|
||
clientes.NumeroDePagina,
|
||
clientes.RegistrosTotales,
|
||
clientes.RegistrosPorPagina,
|
||
clientes.PaginasTotales,
|
||
clientes.HayPaginaSiguiente,
|
||
clientes.HayPaginaAnterior,
|
||
clientes.NumeroPaginaSiguiente,
|
||
clientes.NumeroPaginaAnterior
|
||
};
|
||
|
||
Response.Headers.Add("Pagination-Info", JsonConvert.SerializeObject(metadata));
|
||
|
||
return Ok(clientesDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene el cliente especificado por su id
|
||
/// </summary>
|
||
/// <param name="noIdentificacion"></param>
|
||
/// <returns>Retorna los datos del banco específicado</returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ClienteDTO))]
|
||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
||
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
||
[HttpGet("{noIdentificacion}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")]
|
||
public async Task<IActionResult> Get(string noIdentificacion)
|
||
{
|
||
try
|
||
{
|
||
var cliente = await _clienteRepo.Get(noIdentificacion);
|
||
if (cliente == null)
|
||
{
|
||
throw new BadRequestException("El cliente no existe");
|
||
}
|
||
var clienteDto = _mapper.Map<ClienteDTO>(cliente);
|
||
return Ok(clienteDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Agrega un cliente
|
||
/// </summary>
|
||
/// <param name="clienteDto"></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(ClienteDTO clienteDto)
|
||
{
|
||
try
|
||
{
|
||
var cliente = _mapper.Map<Cliente>(clienteDto);
|
||
var clienteTemp = await _clienteRepo.Get(cliente.NoIdentificacion);
|
||
if (clienteTemp != null)
|
||
{
|
||
throw new BadRequestException("El cliente ingresado ya existe");
|
||
}
|
||
var agregado = await _clienteRepo.Post(cliente);
|
||
if (agregado == false)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al agregar el cliente");
|
||
}
|
||
return Ok(agregado);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Actualiza el cliente especificado por su id
|
||
/// </summary>
|
||
/// <param name="noIdentificacion"></param>
|
||
/// <param name="clienteDto"></param>
|
||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
||
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
||
[HttpPut("{noIdentificacion}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
|
||
public async Task<IActionResult> Put(string noIdentificacion, ClienteDTO clienteDto)
|
||
{
|
||
try
|
||
{
|
||
var clienteTemp = await _clienteRepo.Get(noIdentificacion);
|
||
if (clienteTemp == null)
|
||
{
|
||
throw new BadRequestException("El cliente a modificar no existe");
|
||
}
|
||
var cliente = _mapper.Map<Cliente>(clienteDto);
|
||
bool modificado = await _clienteRepo.Put(noIdentificacion, cliente);
|
||
if (modificado == false)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al modificar el cliente");
|
||
}
|
||
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 cliente especificado por su id
|
||
/// </summary>
|
||
/// <param name="noIdentificacion"></param>
|
||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
||
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
||
[HttpDelete("{noIdentificacion}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
|
||
public async Task<IActionResult> Delete(string noIdentificacion)
|
||
{
|
||
try
|
||
{
|
||
var clienteTemp = await _clienteRepo.Get(noIdentificacion);
|
||
if (clienteTemp == null)
|
||
{
|
||
throw new BadRequestException("El cliente a eliminar no existe");
|
||
}
|
||
bool eliminado = await _clienteRepo.Delete(noIdentificacion);
|
||
if (eliminado == false)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al eliminar el cliente");
|
||
}
|
||
return Ok(eliminado);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|