66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
|
|
// backend/src/Titulares.Api/Controllers/TitularesController.cs
|
||
|
|
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Titulares.Api.Data;
|
||
|
|
using Titulares.Api.Models;
|
||
|
|
|
||
|
|
namespace Titulares.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public class TitularesController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly TitularRepositorio _repositorio;
|
||
|
|
|
||
|
|
public TitularesController(TitularRepositorio repositorio)
|
||
|
|
{
|
||
|
|
_repositorio = repositorio;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet]
|
||
|
|
public async Task<IActionResult> ObtenerTodos()
|
||
|
|
{
|
||
|
|
var titulares = await _repositorio.ObtenerTodosAsync();
|
||
|
|
return Ok(titulares);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
public async Task<IActionResult> CrearManual([FromBody] CrearTitularDto titularDto)
|
||
|
|
{
|
||
|
|
if (!ModelState.IsValid)
|
||
|
|
{
|
||
|
|
return BadRequest(ModelState);
|
||
|
|
}
|
||
|
|
var nuevoId = await _repositorio.CrearManualAsync(titularDto);
|
||
|
|
return CreatedAtAction(nameof(ObtenerTodos), new { id = nuevoId }, null);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPut("{id}")]
|
||
|
|
public async Task<IActionResult> Actualizar(int id, [FromBody] ActualizarTitularDto titularDto)
|
||
|
|
{
|
||
|
|
if (!ModelState.IsValid)
|
||
|
|
{
|
||
|
|
return BadRequest(ModelState);
|
||
|
|
}
|
||
|
|
var resultado = await _repositorio.ActualizarTextoAsync(id, titularDto);
|
||
|
|
return resultado ? NoContent() : NotFound();
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPut("reordenar")]
|
||
|
|
public async Task<IActionResult> Reordenar([FromBody] List<ReordenarTitularDto> ordenes)
|
||
|
|
{
|
||
|
|
if (ordenes == null || !ordenes.Any())
|
||
|
|
{
|
||
|
|
return BadRequest("La lista de órdenes no puede estar vacía.");
|
||
|
|
}
|
||
|
|
var resultado = await _repositorio.ActualizarOrdenAsync(ordenes);
|
||
|
|
return resultado ? Ok() : StatusCode(500, "Error al actualizar el orden.");
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpDelete("{id}")]
|
||
|
|
public async Task<IActionResult> Eliminar(int id)
|
||
|
|
{
|
||
|
|
var resultado = await _repositorio.EliminarAsync(id);
|
||
|
|
return resultado ? NoContent() : NotFound();
|
||
|
|
}
|
||
|
|
}
|