GetRobotTaskQueryHandler.cs
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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));
}
}