public async Task CreateAsync(string customerId) { var cart = new Cart(CartId.NewCartId(), new CustomerId(customerId)); subscriber.Subscribe <CartCreatedEvent>(async @event => await HandleAsync(cartCreatedEventHandlers, @event)); await cartRepository.SaveAsync(cart); }
public async Task DeleteProductFromCart() { var services = new ServiceCollection(); services.AddTransient <IProductRepository, FakeProductRepository>(); using var resolver = EventFlowOptions.New .UseServiceCollection(services) .AddDefaults(typeof(CartContext).Assembly) .UseEntityFrameworkEventStore <CartContext>() .ConfigureEntityFramework(EntityFrameworkConfiguration.New) .AddDbContextProvider <CartContext, MySqlCartContextProvider>() .AddEvents(typeof(ProductAddedEvent), typeof(ProductRemovedEvent)) .AddCommands(typeof(AddProductCommand), typeof(RemoveProductCommand)) .AddCommandHandlers(typeof(AddProductCommandHandler), typeof(RemoveProductCommandHandler)) .CreateResolver(); var commandBus = resolver.Resolve <ICommandBus>(); var aggregateStore = resolver.Resolve <IAggregateStore>(); CartId cartId = CartId.NewCartId(); ProductId productId = new ProductId(Guid.Empty); await commandBus.PublishAsync(new AddProductCommand(cartId, productId), CancellationToken.None); var cart = await aggregateStore.LoadAsync <Cart, CartId>(cartId, CancellationToken.None); Assert.AreEqual(1, cart.Products.Count); await commandBus.PublishAsync(new RemoveProductCommand(cartId, productId), CancellationToken.None); cart = await aggregateStore.LoadAsync <Cart, CartId>(cartId, CancellationToken.None); Assert.AreEqual(0, cart.Products.Count); }
public async Task AddProductToCart() { var rmqUri = new Uri("amqp://*****:*****@localhost:5672/vhost"); var services = new ServiceCollection(); services.AddTransient <IProductRepository, FakeProductRepository>(); using var resolver = EventFlowOptions.New .UseServiceCollection(services) .AddDefaults(typeof(CartContext).Assembly) .UseEntityFrameworkEventStore <CartContext>() .ConfigureEntityFramework(EntityFrameworkConfiguration.New) .AddDbContextProvider <CartContext, MySqlCartContextProvider>() .AddEvents(typeof(ProductAddedEvent)) .AddCommands(typeof(AddProductCommand)) .AddCommandHandlers(typeof(AddProductCommandHandler)) .PublishToRabbitMq(RabbitMqConfiguration.With(rmqUri)) .CreateResolver(); var commandBus = resolver.Resolve <ICommandBus>(); var aggregateStore = resolver.Resolve <IAggregateStore>(); CartId cartId = CartId.NewCartId(); await commandBus.PublishAsync( new AddProductCommand(cartId, new ProductId(Guid.Empty)), CancellationToken.None); Cart cart = await aggregateStore.LoadAsync <Cart, CartId>(cartId, CancellationToken.None); Assert.AreEqual(1, cart.Products.Count); }
public void MissingCart() { var cartId = CartId.NewCartId("missing-cart"); var storage = new SpyStorage(); App.ApplyDiscount(cartId, storage); Assert.Null(storage.Saved); }
public void NoDiscount() { var cartId = CartId.NewCartId("some-normal-cart"); var storage = new SpyStorage(); App.ApplyDiscount(cartId, storage); Assert.Null(storage.Saved); }
public Cart Please() { Cart cart = new Cart(CartId.NewCartId()); foreach (var product in _products) { cart.Apply(new ProductAddedEvent(product)); } return(cart); }
public void HappyPath() { var cartId = CartId.NewCartId("some-gold-cart"); var storage = new SpyStorage(); App.ApplyDiscount(cartId, storage); var expected = new Cart( CartId.NewCartId("some-gold-cart"), CustomerId.NewCustomerId("gold-customer"), Amount.NewAmount(50)); Assert.Equal(expected, storage.Saved); }