feat(CUB-54): implement Agent State Repository with EF Core
- Add AgentState read model (Models/AgentState.cs) - Add IAgentStateRepository interface with GetAllAsync, GetBySessionKeyAsync, UpdateStatusAsync - Add AgentStateRepository EF Core implementation mapping Agent entity → AgentState model - Register IAgentStateRepository in DI (Program.cs) - Exclude ControlCenter sub-project from Api compilation Build: 0 warnings, 0 errors
This commit is contained in:
@@ -6,6 +6,11 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- Exclude the separate ControlCenter sub-project from this project's compilation -->
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="ControlCenter/**/*.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.14" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.14" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
|
||||||
|
|||||||
19
backend/Models/AgentState.cs
Normal file
19
backend/Models/AgentState.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
namespace ControlCenter.Api.Models;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read-only model representing an agent's current state.
|
||||||
|
/// Used as the return type from the Agent State Repository
|
||||||
|
/// to decouple consumers from the persistence layer.
|
||||||
|
/// </summary>
|
||||||
|
public class AgentState
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string Status { get; set; } = string.Empty;
|
||||||
|
public string? Task { get; set; }
|
||||||
|
public int? Progress { get; set; }
|
||||||
|
public string SessionKey { get; set; } = string.Empty;
|
||||||
|
public string Channel { get; set; } = string.Empty;
|
||||||
|
public DateTime LastActivity { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
public DateTime UpdatedAt { get; set; }
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using ControlCenter.Api.Data;
|
using ControlCenter.Api.Data;
|
||||||
using ControlCenter.Api.Hubs;
|
using ControlCenter.Api.Hubs;
|
||||||
|
using ControlCenter.Api.Repositories;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -10,6 +11,9 @@ builder.Services.AddOpenApi();
|
|||||||
// Register SignalR for real-time agent status updates
|
// Register SignalR for real-time agent status updates
|
||||||
builder.Services.AddSignalR();
|
builder.Services.AddSignalR();
|
||||||
|
|
||||||
|
// Register Agent State Repository
|
||||||
|
builder.Services.AddScoped<IAgentStateRepository, AgentStateRepository>();
|
||||||
|
|
||||||
// Register DbContext with PostgreSQL
|
// Register DbContext with PostgreSQL
|
||||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||||
{
|
{
|
||||||
|
|||||||
76
backend/Repositories/AgentStateRepository.cs
Normal file
76
backend/Repositories/AgentStateRepository.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
using ControlCenter.Api.Data;
|
||||||
|
using ControlCenter.Api.Entities;
|
||||||
|
using ControlCenter.Api.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace ControlCenter.Api.Repositories;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EF Core implementation of the Agent State Repository.
|
||||||
|
/// Maps between the persisted <see cref="Agent"/> entity and the
|
||||||
|
/// read-oriented <see cref="AgentState"/> model.
|
||||||
|
/// </summary>
|
||||||
|
public class AgentStateRepository : IAgentStateRepository
|
||||||
|
{
|
||||||
|
private readonly AppDbContext _db;
|
||||||
|
|
||||||
|
public AgentStateRepository(AppDbContext db)
|
||||||
|
{
|
||||||
|
_db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<AgentState>> GetAllAsync(CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var agents = await _db.Agents
|
||||||
|
.AsNoTracking()
|
||||||
|
.OrderByDescending(a => a.LastActivity)
|
||||||
|
.ToListAsync(ct);
|
||||||
|
|
||||||
|
return agents.Select(ToModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<AgentState?> GetBySessionKeyAsync(string sessionKey, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var agent = await _db.Agents
|
||||||
|
.AsNoTracking()
|
||||||
|
.FirstOrDefaultAsync(a => a.SessionKey == sessionKey, ct);
|
||||||
|
|
||||||
|
return agent is null ? null : ToModel(agent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<bool> UpdateStatusAsync(Guid id, string status, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
if (!Enum.TryParse<AgentStatus>(status, ignoreCase: true, out var parsedStatus))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var agent = await _db.Agents.FindAsync([id], ct);
|
||||||
|
if (agent is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
agent.Status = parsedStatus;
|
||||||
|
agent.UpdatedAt = DateTime.UtcNow;
|
||||||
|
agent.LastActivity = DateTime.UtcNow;
|
||||||
|
|
||||||
|
await _db.SaveChangesAsync(ct);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps a persisted <see cref="Agent"/> entity to a <see cref="AgentState"/> model.
|
||||||
|
/// </summary>
|
||||||
|
private static AgentState ToModel(Agent agent) => new()
|
||||||
|
{
|
||||||
|
Id = agent.Id,
|
||||||
|
Status = agent.Status.ToString(),
|
||||||
|
Task = agent.Task,
|
||||||
|
Progress = agent.Progress,
|
||||||
|
SessionKey = agent.SessionKey,
|
||||||
|
Channel = agent.Channel,
|
||||||
|
LastActivity = agent.LastActivity,
|
||||||
|
CreatedAt = agent.CreatedAt,
|
||||||
|
UpdatedAt = agent.UpdatedAt,
|
||||||
|
};
|
||||||
|
}
|
||||||
27
backend/Repositories/IAgentStateRepository.cs
Normal file
27
backend/Repositories/IAgentStateRepository.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using ControlCenter.Api.Models;
|
||||||
|
|
||||||
|
namespace ControlCenter.Api.Repositories;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Repository interface for accessing and mutating Agent State.
|
||||||
|
/// Provides a clean abstraction over the EF Core data access layer.
|
||||||
|
/// </summary>
|
||||||
|
public interface IAgentStateRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve all agent states.
|
||||||
|
/// </summary>
|
||||||
|
Task<IReadOnlyList<AgentState>> GetAllAsync(CancellationToken ct = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a single agent state by its session key.
|
||||||
|
/// Returns null if no agent is found with the given session key.
|
||||||
|
/// </summary>
|
||||||
|
Task<AgentState?> GetBySessionKeyAsync(string sessionKey, CancellationToken ct = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update the status of an agent by its primary key.
|
||||||
|
/// Returns true if the agent was found and updated, false otherwise.
|
||||||
|
/// </summary>
|
||||||
|
Task<bool> UpdateStatusAsync(Guid id, string status, CancellationToken ct = default);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user