TFS_ANTIGUO/TPAtesa_RecHuma/REC-HUMA.API/Controllers/CantonController.cs
2026-06-26 10:14:39 -06:00

131 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 CantonController : ControllerBase
{
private readonly ICantonRepo _CantonRepo;
private readonly IMapper _mapper;
private readonly ILogger<CantonController> _logger;
public CantonController(ICantonRepo CantonRepo, IMapper mapper, ILogger<CantonController> logger)
{
_CantonRepo = CantonRepo;
_mapper = mapper;
_logger = logger;
}
[HttpGet]
public async Task<IEnumerable<CantonDto>> GetCantones()
{
try
{
var cantones = await _CantonRepo.GetCantones();
var cantonDto = _mapper.Map<IEnumerable<CantonDto>>(cantones);
return cantonDto;
}
catch (Exception ex)
{
var cantonDTO = new List<CantonDto>
{
new CantonDto() { CodResultado = 500, Mensaje = "Error" }
};
_logger.LogError(ex, ex.Message);
return cantonDTO;
}
}
[HttpGet("{id}")]
public async Task<CantonDto> GetCanton(int id)
{
try
{
var cantones = await _CantonRepo.GetCanton(id);
var cantonDto = _mapper.Map<CantonDto>(cantones);
return cantonDto;
}
catch (Exception ex)
{
var cantonDTO = new CantonDto
{
CodResultado = 500,
Mensaje = "Error"
};
_logger.LogError(ex, ex.Message);
return cantonDTO;
}
}
[HttpPut]
public async Task<IActionResult> PutCanton(CantonDto cantonDto)
{
try
{
var canton = _mapper.Map<Canton>(cantonDto);
await _CantonRepo.PutCanton(canton);
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500);
}
}
[HttpPost]
public async Task<IActionResult> PostCanton(CantonDto cantonDto)
{
try
{
var canton = _mapper.Map<Canton>(cantonDto);
await _CantonRepo.PostCanton(canton);
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500);
}
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCantones(int id)
{
try
{
await _CantonRepo.DeleteCanton(id);
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500);
}
}
}
}