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 TelefonoController : ControllerBase { private readonly ITelefonoRepo _TelefonoRepo; private readonly IMapper _mapper; public TelefonoController(ITelefonoRepo telefonoRepo, IMapper mapper) { _TelefonoRepo = telefonoRepo; _mapper = mapper; } ///         /// Obtiene todos los telefono         ///         ///         /// Retorna todos los telefono 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 telefonos = await _TelefonoRepo.Get(); if (telefonos == null) { throw new BadRequestException("Hubo un problema al obtener los teléfonos"); } var TelefonosDto = _mapper.Map>(telefonos); return Ok(TelefonosDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Obtiene el telefono especificado por su id         ///         ///         /// Retorna los datos del telefono especificado [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(TelefonoDTO))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpGet("{idTelefono}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO,CONTADOR")] public async Task Get(int idTelefono) { try { var telefono = await _TelefonoRepo.Get(idTelefono); if (telefono == null) { throw new BadRequestException("El teléfono no existe"); } var TelefonoDto = _mapper.Map(telefono); return Ok(TelefonoDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Agrega un telefono         ///         /// [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(TelefonoDTO telefonoDto) { try { var telefono = _mapper.Map(telefonoDto); bool agregado = await _TelefonoRepo.Post(telefono); if (agregado == false) { throw new BadRequestException("Hubo un problema al agregar el teléfono"); } return Ok(agregado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Actualiza el telefono especificado por su id         ///         ///         /// [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpPut("{idTelefono}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")] public async Task Put(int idTelefono, TelefonoDTO telefonoDto) { try { var telefonoTemp = await _TelefonoRepo.Get(idTelefono); if (telefonoTemp == null) { throw new BadRequestException("El teléfono a modificar no existe"); } var telefono = _mapper.Map(telefonoDto); telefono.IdTelefono = idTelefono; bool modificado = await _TelefonoRepo.Put(telefono); if (modificado == false) { throw new BadRequestException("Hubo un problema al modificar el teléfono"); } return Ok(modificado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Elimina el telefono especificado por su id         ///         /// [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpDelete("{idTelefono}"), Authorize(Roles = "ADMINISTRADOR,OPERATIVO")] public async Task Delete(int idTelefono) { try { var accionTemp = await _TelefonoRepo.Get(idTelefono); if (accionTemp == null) { throw new BadRequestException("El teléfono a eliminar no existe"); } bool eliminado = await _TelefonoRepo.Delete(idTelefono); if (eliminado == false) { throw new BadRequestException("Hubo un problema al eliminar el teléfono"); } return Ok(eliminado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } } }