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