using Codex.Dal; using FluentValidation; using Microsoft.EntityFrameworkCore; using OpenHarbor.CQRS.Abstractions; namespace Codex.CQRS.Commands; /// /// Command to soft-delete an agent /// public record DeleteAgentCommand { /// /// ID of the agent to delete /// public Guid Id { get; init; } } /// /// Handler for deleting an agent (soft delete) /// public class DeleteAgentCommandHandler(CodexDbContext dbContext) : ICommandHandler { 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); } } /// /// Validator for DeleteAgentCommand /// public class DeleteAgentCommandValidator : AbstractValidator { public DeleteAgentCommandValidator() { RuleFor(x => x.Id) .NotEmpty().WithMessage("Agent ID is required"); } }