public async Task <IActionResult> AddProduct(
        [FromServices] Func <AddProductItemToShoppingCart, CancellationToken, ValueTask> handle,
        [FromRoute] Guid id,
        [FromBody] AddProductRequest?request,
        CancellationToken ct
        )
    {
        if (request == null)
        {
            throw new ArgumentNullException(nameof(request));
        }

        var command = AddProductItemToShoppingCart.From(
            id,
            ProductItem.From(
                request.ProductItem?.ProductId,
                request.ProductItem?.Quantity
                ),
            request.Version
            );

        await handle(command, ct);

        return(Ok());
    }
Ejemplo n.º 2
0
 public static IServiceCollection AddShoppingCartsModule(this IServiceCollection services) =>
 services
 .For <ShoppingCart>(
     ShoppingCart.Default,
     ShoppingCart.When,
     builder => builder
     .AddOn <InitializeShoppingCart>(
         InitializeShoppingCart.Handle,
         command => ShoppingCart.MapToStreamId(command.ShoppingCartId)
         )
     .UpdateOn <AddProductItemToShoppingCart>(
         sp =>
         (command, shoppingCart) =>
         AddProductItemToShoppingCart.Handle(
             sp.GetRequiredService <IProductPriceCalculator>(),
             command,
             shoppingCart
             ),
         command => ShoppingCart.MapToStreamId(command.ShoppingCartId),
         command => command.Version
         )
     .UpdateOn <RemoveProductItemFromShoppingCart>(
         RemoveProductItemFromShoppingCart.Handle,
         command => ShoppingCart.MapToStreamId(command.ShoppingCartId),
         command => command.Version
         )
     .UpdateOn <ConfirmShoppingCart>(
         ConfirmShoppingCart.Handle,
         command => ShoppingCart.MapToStreamId(command.ShoppingCartId),
         command => command.Version
         )
     )
 .For <ShoppingCartDetails, ECommerceDbContext>(
     builder => builder
     .AddOn <ShoppingCartInitialized>(ShoppingCartDetailsProjection.Handle)
     .UpdateOn <ProductItemAddedToShoppingCart>(
         e => e.ShoppingCartId,
         ShoppingCartDetailsProjection.Handle,
         (entry, ct) => entry.Collection(x => x.ProductItems).LoadAsync(ct)
         )
     .UpdateOn <ProductItemRemovedFromShoppingCart>(
         e => e.ShoppingCartId,
         ShoppingCartDetailsProjection.Handle,
         (entry, ct) => entry.Collection(x => x.ProductItems).LoadAsync(ct)
         )
     .UpdateOn <ShoppingCartConfirmed>(
         e => e.ShoppingCartId,
         ShoppingCartDetailsProjection.Handle
         )
     .QueryWith <GetCartById>(GetCartById.Handle)
     )
 .For <ShoppingCartShortInfo, ECommerceDbContext>(
     builder => builder
     .AddOn <ShoppingCartInitialized>(ShoppingCartShortInfoProjection.Handle)
     .UpdateOn <ProductItemAddedToShoppingCart>(
         e => e.ShoppingCartId,
         ShoppingCartShortInfoProjection.Handle
         )
     .UpdateOn <ProductItemRemovedFromShoppingCart>(
         e => e.ShoppingCartId,
         ShoppingCartShortInfoProjection.Handle
         )
     .UpdateOn <ShoppingCartConfirmed>(
         e => e.ShoppingCartId,
         ShoppingCartShortInfoProjection.Handle
         )
     .QueryWith <GetCarts>(GetCarts.Handle)
     );