CODEX_ADK/BACKEND/Codex.CQRS/Commands/DeleteAgentCommand.cs
Svrnty 229a0698a3 Initial commit: CODEX_ADK monorepo
Multi-agent AI laboratory with ASP.NET Core 8.0 backend and Flutter frontend.
Implements CQRS architecture, OpenAPI contract-first API design.

BACKEND: Agent management, conversations, executions with PostgreSQL + Ollama
FRONTEND: Cross-platform UI with strict typing and Result-based error handling

Co-Authored-By: Jean-Philippe Brule <jp@svrnty.io>
2025-10-26 23:12:32 -04:00

53 lines
1.4 KiB
C#

using Codex.Dal;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.Abstractions;
namespace Codex.CQRS.Commands;
/// <summary>
/// Command to soft-delete an agent
/// </summary>
public record DeleteAgentCommand
{
/// <summary>
/// ID of the agent to delete
/// </summary>
public Guid Id { get; init; }
}
/// <summary>
/// Handler for deleting an agent (soft delete)
/// </summary>
public class DeleteAgentCommandHandler(CodexDbContext dbContext) : ICommandHandler<DeleteAgentCommand>
{
public async Task HandleAsync(DeleteAgentCommand command, CancellationToken cancellationToken = default)
{
var agent = await dbContext.Agents
.FirstOrDefaultAsync(a => a.Id == command.Id && !a.IsDeleted, cancellationToken);
if (agent == null)
{
throw new InvalidOperationException($"Agent with ID {command.Id} not found or has already been deleted");
}
// Soft delete
agent.IsDeleted = true;
agent.UpdatedAt = DateTime.UtcNow;
await dbContext.SaveChangesAsync(cancellationToken);
}
}
/// <summary>
/// Validator for DeleteAgentCommand
/// </summary>
public class DeleteAgentCommandValidator : AbstractValidator<DeleteAgentCommand>
{
public DeleteAgentCommandValidator()
{
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Agent ID is required");
}
}