GetRobotTaskQueryHandler.cs 1.4 KB
using AutoMapper;
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.DTOs;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;

/// <summary>
/// 查询单个任务命令处理器
/// @author zzy
/// </summary>
public class GetRobotTaskQueryHandler : IConsumer<GetRobotTaskQuery>
{
    private readonly ILogger<GetRobotTaskQueryHandler> _logger;
    private readonly IRobotTaskRepository _robotTaskRepository;
    private readonly IMapper _mapper;

    public GetRobotTaskQueryHandler(
        ILogger<GetRobotTaskQueryHandler> logger,
        IRobotTaskRepository robotTaskRepository,
        IMapper mapper)
    {
        _logger = logger;
        _robotTaskRepository = robotTaskRepository;
        _mapper = mapper;
    }

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

        var task = await _robotTaskRepository.GetByIdWithDetailsAsync(query.TaskId, context.CancellationToken);
        if (task == null)
        {
            await context.RespondAsync(ApiResponse<RobotTaskDto>.Failed($"未找到任务ID为 {query.TaskId} 的任务"));
            return;
        }

        var taskDto = _mapper.Map<RobotTaskDto>(task);
        await context.RespondAsync(ApiResponse<RobotTaskDto>.Successful(taskDto));
    }
}