- Add AgentState entity mapping to 'agents' table (snake_case columns) - Add IAgentStateRepository interface (GetAllAsync, GetBySessionKeyAsync, UpdateStatusAsync) - Add AgentStateRepository with EF Core implementation - Add ControlCenterDbContext with ApplyConfigurationsFromAssembly - Add AgentStateConfiguration with snake_case column mappings and indexes - Register DbContext (Npgsql) and repository in Program.cs DI - Add ConnectionStrings to appsettings.json - Add EF Core 9.0.7 and Npgsql.EntityFrameworkCore.PostgreSQL 9.0.4 packages
62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using ControlCenter.Data;
|
|
using ControlCenter.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ControlCenter.Repositories;
|
|
|
|
/// <summary>
|
|
/// EF Core implementation of <see cref="IAgentStateRepository"/>.
|
|
///
|
|
/// <para>Queries the <c>agents</c> table via <see cref="ControlCenterDbContext"/>
|
|
/// using snake_case column mappings defined in the <see cref="Data.Configurations.AgentStateConfiguration"/>.
|
|
/// All write operations call <c>SaveChangesAsync</c> immediately.</para>
|
|
/// </summary>
|
|
public class AgentStateRepository : IAgentStateRepository
|
|
{
|
|
private readonly ControlCenterDbContext _context;
|
|
private readonly ILogger<AgentStateRepository> _logger;
|
|
|
|
public AgentStateRepository(
|
|
ControlCenterDbContext context,
|
|
ILogger<AgentStateRepository> logger)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IEnumerable<AgentState>> GetAllAsync()
|
|
{
|
|
_logger.LogDebug("Fetching all agent states");
|
|
return await _context.AgentStates.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<AgentState?> GetBySessionKeyAsync(string sessionKey)
|
|
{
|
|
_logger.LogDebug("Looking up agent state by session key: {SessionKey}", sessionKey);
|
|
return await _context.AgentStates
|
|
.FirstOrDefaultAsync(a => a.SessionKey == sessionKey);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<AgentState?> UpdateStatusAsync(Guid id, string status)
|
|
{
|
|
_logger.LogDebug("Updating agent status: {Id} → {Status}", id, status);
|
|
|
|
var entity = await _context.AgentStates.FindAsync(id);
|
|
if (entity is null)
|
|
{
|
|
_logger.LogWarning("Agent state not found: {Id}", id);
|
|
return null;
|
|
}
|
|
|
|
entity.Status = status;
|
|
entity.LastActivity = DateTime.UtcNow;
|
|
|
|
_context.AgentStates.Update(entity);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return entity;
|
|
}
|
|
} |