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:
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