From 7b02850473cd4cdc27bc6c75e83bf87156c681e0 Mon Sep 17 00:00:00 2001 From: witskeeper Date: Thu, 12 Dec 2024 16:55:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6Command=E3=80=81CommandHandle?= =?UTF-8?q?r=E3=80=81CommandValidator=E5=88=B0=E4=B8=80=E4=B8=AA=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CreateOrderCommand.cs | 28 +++++++++++++++++-- .../Commands/CreateOrderCommandHandler.cs | 21 -------------- .../Commands/CreateOrderCommandValidator.cs | 13 --------- .../Commands/DeliverGoodsCommand.cs | 17 +++++++++-- .../Commands/DeliverGoodsCommandHandler.cs | 16 ----------- .../Application/Commands/OrderPaidCommand.cs | 16 +++++++++-- .../Commands/OrderPaidCommandHandler.cs | 15 ---------- 7 files changed, 53 insertions(+), 73 deletions(-) delete mode 100644 template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandHandler.cs delete mode 100644 template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandValidator.cs delete mode 100644 template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommandHandler.cs delete mode 100644 template/src/ABC.Template.Web/Application/Commands/OrderPaidCommandHandler.cs diff --git a/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommand.cs b/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommand.cs index c33d9da..2c129b8 100644 --- a/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommand.cs +++ b/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommand.cs @@ -1,7 +1,31 @@ using ABC.Template.Domain.AggregatesModel.OrderAggregate; +using ABC.Template.Infrastructure.Repositories; +using ABC.Template.Web.Application.IntegrationEventHandlers; +using FluentValidation; +using NetCorePal.Extensions.Mappers; using NetCorePal.Extensions.Primitives; -namespace ABC.Template.Web.Application.Commands +namespace ABC.Template.Web.Application.Commands; + +public record CreateOrderCommand(string Name, int Price, int Count) : ICommand; + +public class CreateOrderCommandValidator : AbstractValidator { - public record CreateOrderCommand(string Name, int Price, int Count) : ICommand; + public CreateOrderCommandValidator() + { + RuleFor(x => x.Name).NotEmpty().MaximumLength(10).WithErrorCode("name error code"); + RuleFor(x => x.Price).InclusiveBetween(18, 60).WithErrorCode("price error code"); + } } + +public class CreateOrderCommandHandler(IOrderRepository orderRepository, IMapperProvider mapperProvider, ILogger logger) : ICommandHandler +{ + + public async Task Handle(CreateOrderCommand request, CancellationToken cancellationToken) + { + var order = request.MapTo(mapperProvider); + order = await orderRepository.AddAsync(order, cancellationToken); + logger.LogInformation("order created, id:{orderId}", order.Id); + return order.Id; + } +} \ No newline at end of file diff --git a/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandHandler.cs b/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandHandler.cs deleted file mode 100644 index 5c46d25..0000000 --- a/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandHandler.cs +++ /dev/null @@ -1,21 +0,0 @@ -using ABC.Template.Domain.AggregatesModel.OrderAggregate; -using ABC.Template.Infrastructure.Repositories; -using ABC.Template.Web.Application.IntegrationEventHandlers; -using NetCorePal.Extensions.Mappers; -using NetCorePal.Extensions.Primitives; -using NetCorePal.Extensions.Repository; - -namespace ABC.Template.Web.Application.Commands -{ - public class CreateOrderCommandHandler(IOrderRepository orderRepository, IMapperProvider mapperProvider, ILogger logger) : ICommandHandler - { - - public async Task Handle(CreateOrderCommand request, CancellationToken cancellationToken) - { - var order = request.MapTo(mapperProvider); - order = await orderRepository.AddAsync(order, cancellationToken); - logger.LogInformation("order created, id:{orderId}", order.Id); - return order.Id; - } - } -} diff --git a/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandValidator.cs b/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandValidator.cs deleted file mode 100644 index cd684fa..0000000 --- a/template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandValidator.cs +++ /dev/null @@ -1,13 +0,0 @@ -using FluentValidation; - -namespace ABC.Template.Web.Application.Commands -{ - public class CreateOrderCommandValidator : AbstractValidator - { - public CreateOrderCommandValidator() - { - RuleFor(x => x.Name).NotEmpty().MaximumLength(10).WithErrorCode("name error code"); - RuleFor(x => x.Price).InclusiveBetween(18, 60).WithErrorCode("price error code"); - } - } -} diff --git a/template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommand.cs b/template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommand.cs index debdf0d..e606e81 100644 --- a/template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommand.cs +++ b/template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommand.cs @@ -1,8 +1,19 @@ using ABC.Template.Domain.AggregatesModel.DeliverAggregate; using ABC.Template.Domain.AggregatesModel.OrderAggregate; +using ABC.Template.Infrastructure.Repositories; using NetCorePal.Extensions.Primitives; -namespace ABC.Template.Web.Application.Commands +namespace ABC.Template.Web.Application.Commands; + +public record DeliverGoodsCommand(OrderId OrderId) : ICommand; + +public class DeliverGoodsCommandHandler(IDeliverRecordRepository deliverRecordRepository) + : ICommandHandler { - public record DeliverGoodsCommand(OrderId OrderId) : ICommand; -} + public Task Handle(DeliverGoodsCommand request, CancellationToken cancellationToken) + { + var record = new DeliverRecord(request.OrderId); + deliverRecordRepository.Add(record); + return Task.FromResult(record.Id); + } +} \ No newline at end of file diff --git a/template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommandHandler.cs b/template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommandHandler.cs deleted file mode 100644 index 91091b3..0000000 --- a/template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommandHandler.cs +++ /dev/null @@ -1,16 +0,0 @@ -using ABC.Template.Domain.AggregatesModel.DeliverAggregate; -using ABC.Template.Infrastructure.Repositories; -using NetCorePal.Extensions.Primitives; - -namespace ABC.Template.Web.Application.Commands -{ - public class DeliverGoodsCommandHandler(IDeliverRecordRepository deliverRecordRepository) : ICommandHandler - { - public Task Handle(DeliverGoodsCommand request, CancellationToken cancellationToken) - { - var record = new DeliverRecord(request.OrderId); - deliverRecordRepository.Add(record); - return Task.FromResult(record.Id); - } - } -} diff --git a/template/src/ABC.Template.Web/Application/Commands/OrderPaidCommand.cs b/template/src/ABC.Template.Web/Application/Commands/OrderPaidCommand.cs index d38e32d..05c9aed 100644 --- a/template/src/ABC.Template.Web/Application/Commands/OrderPaidCommand.cs +++ b/template/src/ABC.Template.Web/Application/Commands/OrderPaidCommand.cs @@ -1,7 +1,17 @@ using ABC.Template.Domain.AggregatesModel.OrderAggregate; +using ABC.Template.Infrastructure.Repositories; using NetCorePal.Extensions.Primitives; -namespace ABC.Template.Web.Application.Commands +namespace ABC.Template.Web.Application.Commands; + +public record class OrderPaidCommand(OrderId OrderId) : ICommand; + +public class OrderPaidCommandHandler(IOrderRepository orderRepository) : ICommandHandler { - public record class OrderPaidCommand(OrderId OrderId) : ICommand; -} + public async Task Handle(OrderPaidCommand request, CancellationToken cancellationToken) + { + var order = await orderRepository.GetAsync(request.OrderId, cancellationToken) ?? + throw new KnownException($"未找到订单,OrderId = {request.OrderId}"); + order.OrderPaid(); + } +} \ No newline at end of file diff --git a/template/src/ABC.Template.Web/Application/Commands/OrderPaidCommandHandler.cs b/template/src/ABC.Template.Web/Application/Commands/OrderPaidCommandHandler.cs deleted file mode 100644 index 86244aa..0000000 --- a/template/src/ABC.Template.Web/Application/Commands/OrderPaidCommandHandler.cs +++ /dev/null @@ -1,15 +0,0 @@ -using ABC.Template.Domain.AggregatesModel.OrderAggregate; -using ABC.Template.Infrastructure.Repositories; -using NetCorePal.Extensions.Primitives; - -namespace ABC.Template.Web.Application.Commands -{ - public class OrderPaidCommandHandler(IOrderRepository orderRepository) : ICommandHandler - { - public async Task Handle(OrderPaidCommand request, CancellationToken cancellationToken) - { - var order = await orderRepository.GetAsync(request.OrderId, cancellationToken) ?? throw new KnownException($"未找到订单,OrderId = {request.OrderId}"); - order.OrderPaid(); - } - } -} \ No newline at end of file