DeleteActionConfigurationCommandHandler.cs 1.38 KB
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;

public class DeleteActionConfigurationCommandHandler : IConsumer<DeleteActionConfigurationCommand>
{
    private readonly ILogger<DeleteActionConfigurationCommandHandler> _logger;
    private readonly IActionConfigurationRepository _repository;

    public DeleteActionConfigurationCommandHandler(
        ILogger<DeleteActionConfigurationCommandHandler> logger,
        IActionConfigurationRepository repository)
    {
        _logger = logger;
        _repository = repository;
    }

    public async Task Consume(ConsumeContext<DeleteActionConfigurationCommand> context)
    {
        var command = context.Message;

        try
        {
            var entity = await _repository.GetByIdAsync(command.ActionConfigId, context.CancellationToken);
            if (entity == null)
            {
                throw new InvalidOperationException($"动作配置ID {command.ActionConfigId} 不存在");
            }

            await _repository.DeleteByIdAsync(command.ActionConfigId);
            await context.RespondAsync(ApiResponse.Successful());
        }
        catch (Exception ex)
        {
            await context.RespondAsync(ApiResponse.Failed(ex.Message));
        }
    }
}