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; } ///         /// Obtiene todas las monedas         ///         ///         /// Retorna todas las monedas 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, Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")] public async Task Get() { try { var monedas = await _MonedaRepo.Get(); if (monedas == null) { throw new BadRequestException("Hubo un problema al obtener las monedas"); } var rolesDto = _mapper.Map>(monedas); return Ok(rolesDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Obtiene la moneda especificada por su id         ///         ///         /// Retorna los datos de la moneda especificado [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 Get(int idMoneda) { try { var moneda = await _MonedaRepo.Get(idMoneda); if (moneda == null) { throw new BadRequestException("La moneda no existe"); } var rolDto = _mapper.Map(moneda); return Ok(rolDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Agrega una moneda         ///         ///         [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(MonedaDTO rolDto) { try { var moneda = _mapper.Map(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; } } } ///         /// Actualiza la moneda especificada por id         ///         ///         /// Accion         [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 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(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; } } } ///         /// Elimina la moneda especificada por id         ///         ///         [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 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; } } } } }