207 lines
7.7 KiB
C#
207 lines
7.7 KiB
C#
|
|
using AutoMapper;
|
|||
|
|
using CuentasCobrar.CORE.DTOs;
|
|||
|
|
using CuentasCobrar.CORE.Entities;
|
|||
|
|
using CuentasCobrar.CORE.Exceptions;
|
|||
|
|
using CuentasCobrar.CORE.Interfaces;
|
|||
|
|
using CuentasCobrar.CORE.ResponseObjects;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using System.Net;
|
|||
|
|
|
|||
|
|
namespace CuentasCobrar.API.Controllers
|
|||
|
|
{
|
|||
|
|
[Route("api/[controller]")]
|
|||
|
|
[Produces("application/json")]
|
|||
|
|
[ApiController]
|
|||
|
|
public class MonedaController : ControllerBase
|
|||
|
|
{
|
|||
|
|
private readonly IMonedaRepo _MonedaRepo;
|
|||
|
|
private readonly IMapper _mapper;
|
|||
|
|
public MonedaController(IMonedaRepo monedaRepo, IMapper mapper)
|
|||
|
|
{
|
|||
|
|
_MonedaRepo = monedaRepo;
|
|||
|
|
_mapper = mapper;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene todas las monedas
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>
|
|||
|
|
/// Retorna todas las monedas de la base de datos
|
|||
|
|
/// </returns>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<MonedaDTO>))]
|
|||
|
|
[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()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var monedas = await _MonedaRepo.Get();
|
|||
|
|
if (monedas == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al obtener las monedas");
|
|||
|
|
}
|
|||
|
|
var rolesDto = _mapper.Map<IEnumerable<MonedaDTO>>(monedas);
|
|||
|
|
return Ok(rolesDto);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Obtiene la moneda especificada por su id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idMoneda"></param>
|
|||
|
|
/// <returns>Retorna los datos de la moneda especificado</returns>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(MonedaDTO))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpGet("{idMoneda}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")]
|
|||
|
|
public async Task<IActionResult> Get(int idMoneda)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var moneda = await _MonedaRepo.Get(idMoneda);
|
|||
|
|
if (moneda == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("La moneda no existe");
|
|||
|
|
}
|
|||
|
|
var rolDto = _mapper.Map<MonedaDTO>(moneda);
|
|||
|
|
return Ok(rolDto);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Agrega una moneda
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="rolDto"></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(MonedaDTO rolDto)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var moneda = _mapper.Map<Moneda>(rolDto);
|
|||
|
|
bool agregado = await _MonedaRepo.Post(moneda);
|
|||
|
|
if (agregado == false)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al agregar la moneda");
|
|||
|
|
}
|
|||
|
|
return Ok(agregado);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Actualiza la moneda especificada por id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idMoneda"></param>
|
|||
|
|
/// <param name="rolDto">Accion</param>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpPut("{idMoneda}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
|
|||
|
|
public async Task<IActionResult> Put(int idMoneda, MonedaDTO rolDto)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var rolTemp = await _MonedaRepo.Get(idMoneda);
|
|||
|
|
if (rolTemp == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("La moneda a modificar no existe");
|
|||
|
|
}
|
|||
|
|
var moneda = _mapper.Map<Moneda>(rolDto);
|
|||
|
|
moneda.IdMoneda = idMoneda;
|
|||
|
|
bool modificado = await _MonedaRepo.Put(moneda);
|
|||
|
|
|
|||
|
|
if (modificado == false)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al modificar la moneda");
|
|||
|
|
}
|
|||
|
|
return Ok(modificado);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Elimina la moneda especificada por id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="idMoneda"></param>
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
|||
|
|
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
|||
|
|
[HttpDelete("{idMoneda}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")]
|
|||
|
|
public async Task<IActionResult> Delete(int idMoneda)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var rolTemp = await _MonedaRepo.Get(idMoneda);
|
|||
|
|
if (rolTemp == null)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("La moneda a eliminar no existe");
|
|||
|
|
}
|
|||
|
|
bool eliminado = await _MonedaRepo.Delete(idMoneda);
|
|||
|
|
if (eliminado == false)
|
|||
|
|
{
|
|||
|
|
throw new BadRequestException("Hubo un problema al eliminar la moneda");
|
|||
|
|
}
|
|||
|
|
return Ok(eliminado);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (ex.GetType() != typeof(BadRequestException))
|
|||
|
|
{
|
|||
|
|
throw new InternalErrorException("Hubo un problema en el servidor");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|