-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from netcorepal/v2
合并Command、CommandHandler、CommandValidator到一个文件
- Loading branch information
Showing
7 changed files
with
53 additions
and
73 deletions.
There are no files selected for viewing
28 changes: 26 additions & 2 deletions
28
template/src/ABC.Template.Web/Application/Commands/CreateOrderCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OrderId>; | ||
|
||
public class CreateOrderCommandValidator : AbstractValidator<CreateOrderCommand> | ||
{ | ||
public record CreateOrderCommand(string Name, int Price, int Count) : ICommand<OrderId>; | ||
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<OrderPaidIntegrationEventHandler> logger) : ICommandHandler<CreateOrderCommand, OrderId> | ||
{ | ||
|
||
public async Task<OrderId> Handle(CreateOrderCommand request, CancellationToken cancellationToken) | ||
{ | ||
var order = request.MapTo<Order>(mapperProvider); | ||
order = await orderRepository.AddAsync(order, cancellationToken); | ||
logger.LogInformation("order created, id:{orderId}", order.Id); | ||
return order.Id; | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandHandler.cs
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
template/src/ABC.Template.Web/Application/Commands/CreateOrderCommandValidator.cs
This file was deleted.
Oops, something went wrong.
17 changes: 14 additions & 3 deletions
17
template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DeliverRecordId>; | ||
|
||
public class DeliverGoodsCommandHandler(IDeliverRecordRepository deliverRecordRepository) | ||
: ICommandHandler<DeliverGoodsCommand, DeliverRecordId> | ||
{ | ||
public record DeliverGoodsCommand(OrderId OrderId) : ICommand<DeliverRecordId>; | ||
} | ||
public Task<DeliverRecordId> Handle(DeliverGoodsCommand request, CancellationToken cancellationToken) | ||
{ | ||
var record = new DeliverRecord(request.OrderId); | ||
deliverRecordRepository.Add(record); | ||
return Task.FromResult(record.Id); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
template/src/ABC.Template.Web/Application/Commands/DeliverGoodsCommandHandler.cs
This file was deleted.
Oops, something went wrong.
16 changes: 13 additions & 3 deletions
16
template/src/ABC.Template.Web/Application/Commands/OrderPaidCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OrderPaidCommand> | ||
{ | ||
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(); | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
template/src/ABC.Template.Web/Application/Commands/OrderPaidCommandHandler.cs
This file was deleted.
Oops, something went wrong.