2025-11-18 14:34:26 -03:00
|
|
|
// Controllers/AdminController.cs
|
|
|
|
|
using ChatbotApi.Data.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2025-11-25 11:46:52 -03:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
using ChatbotApi.Services;
|
2025-11-18 14:34:26 -03:00
|
|
|
|
|
|
|
|
namespace ChatbotApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class AdminController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly AppContexto _context;
|
2025-11-25 11:46:52 -03:00
|
|
|
private readonly IMemoryCache _cache;
|
2025-11-18 14:34:26 -03:00
|
|
|
|
2025-11-25 11:46:52 -03:00
|
|
|
public AdminController(AppContexto context, IMemoryCache cache)
|
2025-11-18 14:34:26 -03:00
|
|
|
{
|
|
|
|
|
_context = context;
|
2025-11-25 11:46:52 -03:00
|
|
|
_cache = cache;
|
2025-11-18 14:34:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/admin/contexto
|
|
|
|
|
[HttpGet("contexto")]
|
|
|
|
|
public async Task<IActionResult> GetAllContextoItems()
|
|
|
|
|
{
|
|
|
|
|
var items = await _context.ContextoItems.OrderBy(i => i.Clave).ToListAsync();
|
|
|
|
|
return Ok(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: api/admin/contexto
|
|
|
|
|
[HttpPost("contexto")]
|
|
|
|
|
public async Task<IActionResult> CreateContextoItem([FromBody] ContextoItem item)
|
|
|
|
|
{
|
|
|
|
|
if (await _context.ContextoItems.AnyAsync(i => i.Clave == item.Clave))
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("La clave ya existe.");
|
|
|
|
|
}
|
|
|
|
|
item.FechaActualizacion = DateTime.UtcNow;
|
|
|
|
|
_context.ContextoItems.Add(item);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2025-11-25 11:46:52 -03:00
|
|
|
|
|
|
|
|
// Invalida la caché de KnowledgeItems
|
|
|
|
|
_cache.Remove(CacheKeys.KnowledgeItems);
|
|
|
|
|
|
2025-11-18 14:34:26 -03:00
|
|
|
return CreatedAtAction(nameof(GetAllContextoItems), new { id = item.Id }, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT: api/admin/contexto/5
|
|
|
|
|
[HttpPut("contexto/{id}")]
|
|
|
|
|
public async Task<IActionResult> UpdateContextoItem(int id, [FromBody] ContextoItem item)
|
|
|
|
|
{
|
|
|
|
|
if (id != item.Id)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
var existingItem = await _context.ContextoItems.FindAsync(id);
|
|
|
|
|
if (existingItem == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
existingItem.Valor = item.Valor;
|
|
|
|
|
existingItem.Descripcion = item.Descripcion;
|
|
|
|
|
existingItem.FechaActualizacion = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
2025-11-25 11:46:52 -03:00
|
|
|
// Invalida la caché de KnowledgeItems
|
|
|
|
|
_cache.Remove(CacheKeys.KnowledgeItems);
|
|
|
|
|
|
2025-11-18 14:34:26 -03:00
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/admin/contexto/5
|
|
|
|
|
[HttpDelete("contexto/{id}")]
|
|
|
|
|
public async Task<IActionResult> DeleteContextoItem(int id)
|
|
|
|
|
{
|
|
|
|
|
var item = await _context.ContextoItems.FindAsync(id);
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
_context.ContextoItems.Remove(item);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2025-11-25 11:46:52 -03:00
|
|
|
// Invalida la caché de KnowledgeItems
|
|
|
|
|
_cache.Remove(CacheKeys.KnowledgeItems);
|
|
|
|
|
|
2025-11-18 14:34:26 -03:00
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/admin/logs
|
|
|
|
|
[HttpGet("logs")]
|
|
|
|
|
public async Task<IActionResult> GetConversationLogs()
|
|
|
|
|
{
|
|
|
|
|
// Obtenemos los últimos 200 logs, ordenados del más reciente al más antiguo.
|
|
|
|
|
var logs = await _context.ConversacionLogs
|
|
|
|
|
.OrderByDescending(log => log.Fecha)
|
|
|
|
|
.Take(200)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
return Ok(logs);
|
|
|
|
|
}
|
2025-11-21 11:20:44 -03:00
|
|
|
|
|
|
|
|
// ENDPOINTS PARA FUENTES DE CONTEXTO (URLs)
|
|
|
|
|
[HttpGet("fuentes")]
|
|
|
|
|
public async Task<IActionResult> GetAllFuentes()
|
|
|
|
|
{
|
|
|
|
|
var fuentes = await _context.FuentesDeContexto.OrderBy(f => f.Nombre).ToListAsync();
|
|
|
|
|
return Ok(fuentes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("fuentes")]
|
|
|
|
|
public async Task<IActionResult> CreateFuente([FromBody] FuenteContexto fuente)
|
|
|
|
|
{
|
|
|
|
|
_context.FuentesDeContexto.Add(fuente);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2025-11-25 11:46:52 -03:00
|
|
|
|
|
|
|
|
// Invalida la caché de FuentesDeContexto
|
|
|
|
|
_cache.Remove(CacheKeys.FuentesDeContexto);
|
|
|
|
|
|
2025-11-21 11:20:44 -03:00
|
|
|
return CreatedAtAction(nameof(GetAllFuentes), new { id = fuente.Id }, fuente);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut("fuentes/{id}")]
|
|
|
|
|
public async Task<IActionResult> UpdateFuente(int id, [FromBody] FuenteContexto fuente)
|
|
|
|
|
{
|
|
|
|
|
if (id != fuente.Id)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.Entry(fuente).State = EntityState.Modified;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateConcurrencyException)
|
|
|
|
|
{
|
|
|
|
|
if (!_context.FuentesDeContexto.Any(e => e.Id == id))
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 11:46:52 -03:00
|
|
|
// Invalida la caché de FuentesDeContexto
|
|
|
|
|
_cache.Remove(CacheKeys.FuentesDeContexto);
|
|
|
|
|
|
2025-11-21 11:20:44 -03:00
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("fuentes/{id}")]
|
|
|
|
|
public async Task<IActionResult> DeleteFuente(int id)
|
|
|
|
|
{
|
|
|
|
|
var fuente = await _context.FuentesDeContexto.FindAsync(id);
|
|
|
|
|
if (fuente == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.FuentesDeContexto.Remove(fuente);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
2025-11-25 11:46:52 -03:00
|
|
|
// Invalida la caché de FuentesDeContexto
|
|
|
|
|
_cache.Remove(CacheKeys.FuentesDeContexto);
|
|
|
|
|
|
2025-11-21 11:20:44 -03:00
|
|
|
return NoContent();
|
|
|
|
|
}
|
2025-11-18 14:34:26 -03:00
|
|
|
}
|
|
|
|
|
}
|