DeleteTaskTemplateCommandHandler.cs
1.54 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
46
47
48
49
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 DeleteTaskTemplateCommandHandler : IConsumer<DeleteTaskTemplateCommand>
{
private readonly ILogger<DeleteTaskTemplateCommandHandler> _logger;
private readonly ITaskTemplateRepository _taskTemplateRepository;
public DeleteTaskTemplateCommandHandler(
ILogger<DeleteTaskTemplateCommandHandler> logger,
ITaskTemplateRepository taskTemplateRepository)
{
_logger = logger;
_taskTemplateRepository = taskTemplateRepository;
}
public async Task Consume(ConsumeContext<DeleteTaskTemplateCommand> context)
{
var command = context.Message;
try
{
var template = await _taskTemplateRepository.GetByIdAsync(
command.TemplateId,
context.CancellationToken);
if (template == null)
{
await context.RespondAsync(ApiResponse.Failed("未找到指定的任务模板"));
return;
}
await _taskTemplateRepository.DeleteAsync(template, context.CancellationToken);
await _taskTemplateRepository.SaveChangesAsync(context.CancellationToken);
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}