Skip to content

Commit

Permalink
Merge pull request #25 from netcorepal/v2
Browse files Browse the repository at this point in the history
合并Command、CommandHandler、CommandValidator到一个文件
  • Loading branch information
witskeeper authored Dec 13, 2024
2 parents 1109720 + 7b02850 commit 16915fc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 73 deletions.
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;
}
}

This file was deleted.

This file was deleted.

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);
}
}

This file was deleted.

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();
}
}

This file was deleted.

0 comments on commit 16915fc

Please sign in to comment.