TFS_ANTIGUO/TPAtesa_CuentasCobrar/CuentasCobrar.API/Controllers/TelefonoController.cs

206 lines
7.8 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
/// <summary>
        /// Obtiene todos los telefono
        /// </summary>
        /// <returns>
        /// Retorna todos los telefono de la base de datos
        /// </returns>
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<TelefonoDTO>))]
[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 telefonos = await _TelefonoRepo.Get();
if (telefonos == null)
{
throw new BadRequestException("Hubo un problema al obtener los teléfonos");
}
var TelefonosDto = _mapper.Map<IEnumerable<TelefonoDTO>>(telefonos);
return Ok(TelefonosDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
/// <summary>
        /// Obtiene el telefono especificado por su id
        /// </summary>
        /// <param name="idTelefono"></param>
        /// <returns>Retorna los datos del telefono especificado</returns>
[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<IActionResult> 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<TelefonoDTO>(telefono);
return Ok(TelefonoDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
/// <summary>
        /// Agrega un telefono
        /// </summary>
        /// <param name="telefonoDto"></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(TelefonoDTO telefonoDto)
{
try
{
var telefono = _mapper.Map<Telefono>(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;
}
}
}
/// <summary>
        /// Actualiza el telefono especificado por su id
        /// </summary>
        /// <param name="idTelefono"></param>
        /// <param name="telefonoDto"></param>
[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<IActionResult> 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<Telefono>(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;
}
}
}
/// <summary>
        /// Elimina el telefono especificado por su id
        /// </summary>
        /// <param name="idTelefono"></param>
[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<IActionResult> 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;
}
}
}
}
}