151 lines
4.2 KiB
C#
151 lines
4.2 KiB
C#
using AutoMapper;
|
|
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.Threading.Tasks;
|
|
|
|
namespace REC_HUMA.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class PersonaController : ControllerBase
|
|
{
|
|
private readonly IPersonaRepo _PersonaRepo;
|
|
private readonly IMapper _mapper;
|
|
private readonly ILogger<PersonaController> _logger;
|
|
|
|
public PersonaController(IPersonaRepo PersonaRepo, IMapper mapper, ILogger<PersonaController> logger)
|
|
{
|
|
_PersonaRepo = PersonaRepo;
|
|
_mapper = mapper;
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IEnumerable<PersonaDto>> GetPersonas()
|
|
{
|
|
try
|
|
{
|
|
var personas = await _PersonaRepo.GetPersonas();
|
|
var personaDto = _mapper.Map<IEnumerable<PersonaDto>>(personas);
|
|
|
|
return personaDto;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var personaDto = new List<PersonaDto>
|
|
{
|
|
new PersonaDto() { CodResultado = 500, Mensaje = "Error" }
|
|
};
|
|
|
|
_logger.LogError(ex, ex.Message);
|
|
|
|
return personaDto;
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<PersonaDto> GetPersona(int id)
|
|
{
|
|
try
|
|
{
|
|
var personas = await _PersonaRepo.GetPersona(id);
|
|
var personaDto = _mapper.Map<PersonaDto>(personas);
|
|
|
|
return personaDto;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var personaDTO = new PersonaDto
|
|
{
|
|
CodResultado = 500,
|
|
Mensaje = "Error"
|
|
};
|
|
|
|
_logger.LogError(ex, ex.Message);
|
|
|
|
return personaDTO;
|
|
}
|
|
}
|
|
|
|
[Route("obtenerPersonaCorreo/{correo}")]
|
|
[HttpGet]
|
|
public async Task<PersonaDto> GetPersonaCorreo(string correo)
|
|
{
|
|
try
|
|
{
|
|
var personas = await _PersonaRepo.GetPersonaEmail(correo);
|
|
var personaDto = _mapper.Map<PersonaDto>(personas);
|
|
|
|
return personaDto;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var personaDTO = new PersonaDto
|
|
{
|
|
CodResultado = 500,
|
|
Mensaje = "Error"
|
|
};
|
|
|
|
_logger.LogError(ex, ex.Message);
|
|
|
|
return personaDTO;
|
|
}
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutPersona(int id, PersonaDto personaDto)
|
|
{
|
|
try
|
|
{
|
|
var persona = _mapper.Map<Persona>(personaDto);
|
|
persona.IdPersona = id;
|
|
|
|
var resultado = await _PersonaRepo.PutPersona(persona);
|
|
|
|
return Ok(resultado);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(500);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> PostPersona(PersonaDto personaDto)
|
|
{
|
|
try
|
|
{
|
|
var persona = _mapper.Map<Persona>(personaDto);
|
|
await _PersonaRepo.PostPersona(persona);
|
|
|
|
return Ok(new RespuestaDTO() { Resultado = "Se agregó la persona satisfactoriamente" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(500);
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeletePersona(int id)
|
|
{
|
|
try
|
|
{
|
|
var resultado = await _PersonaRepo.DeletePersona(id);
|
|
return Ok(resultado);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(500);
|
|
}
|
|
}
|
|
}
|
|
}
|