コード例 #1
0
    public async Task <Package> SendPackage(SendPackage request, CancellationToken cancellationToken = default)
    {
        if (request == null)
        {
            throw new ArgumentNullException(nameof(request));
        }

        if (request.ProductItems?.Count == 0)
        {
            throw new ArgumentException("It's not possible to send package with empty product items");
        }

        if (!productAvailabilityService.IsEnoughOf(request.ProductItems !.ToArray()))
        {
            await Publish(new ProductWasOutOfStock(request.OrderId, DateTime.UtcNow));

            throw new ArgumentOutOfRangeException(nameof(request.ProductItems), "Cannot send package - product was out of stock.");
        }

        var package = new Package
        {
            Id           = Guid.NewGuid(),
            OrderId      = request.OrderId,
            ProductItems = request.ProductItems.Select(pi =>
                                                       new ProductItem {
                Id = Guid.NewGuid(), ProductId = pi.ProductId, Quantity = pi.Quantity
            }).ToList(),
            SentAt = DateTime.UtcNow
        };

        await Packages.AddAsync(package, cancellationToken);

        var @event = new PackageWasSent(package.Id,
                                        package.OrderId,
                                        package.ProductItems,
                                        package.SentAt);

        await SaveChangesAndPublish(@event, cancellationToken);

        return(package);
    }
コード例 #2
0
 public Task Handle(PackageWasSent @event, CancellationToken cancellationToken)
 {
     return(commandBus.Send(CompleteOrder.Create(@event.OrderId)));
 }