127 lines
3.4 KiB
C#
127 lines
3.4 KiB
C#
|
|
using AutoMapper;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Http;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using REC_HUMA.CORE.DTOs;
|
|||
|
|
using REC_HUMA.CORE.Entities;
|
|||
|
|
using REC_HUMA.CORE.Interfaces;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace REC_HUMA.API.Controllers
|
|||
|
|
{
|
|||
|
|
[Route("api/[controller]")]
|
|||
|
|
[ApiController]
|
|||
|
|
public class BancoController : ControllerBase
|
|||
|
|
{
|
|||
|
|
private readonly IBancoRepo _BancoRepo;
|
|||
|
|
private readonly IMapper _mapper;
|
|||
|
|
private readonly ILogger<BancoController> _logger;
|
|||
|
|
|
|||
|
|
public BancoController(IBancoRepo BancoRepo, IMapper mapper, ILogger<BancoController> logger)
|
|||
|
|
{
|
|||
|
|
_BancoRepo = BancoRepo;
|
|||
|
|
_mapper = mapper;
|
|||
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpGet]
|
|||
|
|
public async Task<IEnumerable<BancoDto>> GetBancos()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var bancos = await _BancoRepo.GetBancos();
|
|||
|
|
|
|||
|
|
var bancoDto = _mapper.Map<IEnumerable<BancoDto>>(bancos);
|
|||
|
|
|
|||
|
|
return bancoDto;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
var BancoDTO = new List<BancoDto>
|
|||
|
|
{
|
|||
|
|
new BancoDto() { CodResultado = 500, Mensaje = "Error" }
|
|||
|
|
};
|
|||
|
|
_logger.LogError(ex, ex.Message);
|
|||
|
|
return BancoDTO;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpGet("{id}")]
|
|||
|
|
public async Task<BancoDto> GetBanco(int id)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var bancos = await _BancoRepo.GetBanco(id);
|
|||
|
|
|
|||
|
|
var bancoDto = _mapper.Map<BancoDto>(bancos);
|
|||
|
|
|
|||
|
|
return bancoDto;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
var BancoDTO = new BancoDto
|
|||
|
|
{
|
|||
|
|
CodResultado = 500,
|
|||
|
|
Mensaje = "Error"
|
|||
|
|
};
|
|||
|
|
_logger.LogError(ex, ex.Message);
|
|||
|
|
return BancoDTO;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpPut]
|
|||
|
|
public async Task<IActionResult> PutBanco(BancoDto bancoDto)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var banco = _mapper.Map<Banco>(bancoDto);
|
|||
|
|
await _BancoRepo.PutBanco(banco);
|
|||
|
|
return Ok();
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, ex.Message);
|
|||
|
|
return StatusCode(500);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<IActionResult> PostBanco(BancoDto bancoDto)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var banco = _mapper.Map<Banco>(bancoDto);
|
|||
|
|
await _BancoRepo.PostBanco(banco);
|
|||
|
|
|
|||
|
|
return Ok(new RespuestaDTO() { Resultado = "Se realizaron los cambios satisfactoriamente" });
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, ex.Message);
|
|||
|
|
return StatusCode(500);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpDelete("{id}")]
|
|||
|
|
public async Task<IActionResult> DeleteBanco(int id)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
await _BancoRepo.DeleteBanco(id);
|
|||
|
|
return Ok();
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, ex.Message);
|
|||
|
|
return StatusCode(500);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|