public async Task ChangePrice() { var(_, repository, mediator) = TestSetup.Init(); var handler = new ProductCommandHandlers(repository, mediator); var id = Guid.NewGuid(); var productCreateCommand = new ProductCreateCommand { Id = id, Name = "mock name", Price = 500 }; await handler.Handle(productCreateCommand, CancellationToken.None); const int newPrice = 600; var priceChangeCommand = new ProductChangePriceCommand { ProductId = id, Price = newPrice }; await handler.Handle(priceChangeCommand, CancellationToken.None); var agg = await repository.GetHydratedAggregate <Kanayri.Domain.Product.Product>(id, CancellationToken.None); Assert.Equal(newPrice, agg.Price); }
public async Task Handle(ProductChangePriceCommand command, CancellationToken cancellationToken) { var aggregate = await _repository.GetHydratedAggregate <Product>(command.ProductId, cancellationToken); var priceChangedEvent = new ProductPriceChangedEvent(command.ProductId, command.Price); aggregate.Handle(priceChangedEvent); await _repository.SaveAggregateEvent(aggregate, priceChangedEvent, cancellationToken); await _mediator.Publish(priceChangedEvent, cancellationToken); }
public async Task QueryChangePrice() { var(_, repository, mediator) = TestSetup.Init(); var handler = new ProductCommandHandlers(repository, mediator); var id = Guid.NewGuid(); const int price1 = 500; const int price2 = 600; const int price3 = 700; var productCreateCommand = new ProductCreateCommand { Id = id, Name = "mock name", Price = price1 }; await handler.Handle(productCreateCommand, CancellationToken.None); var priceChange1 = new ProductChangePriceCommand { ProductId = id, Price = price2 }; await handler.Handle(priceChange1, CancellationToken.None); var priceChange2 = new ProductChangePriceCommand { ProductId = id, Price = price3 }; await handler.Handle(priceChange2, CancellationToken.None); var events = await repository.GetEventsOfType <ProductPriceChangedEvent>(id, CancellationToken.None); var changedEvents = events as ProductPriceChangedEvent[] ?? events.ToArray(); Assert.Equal(2, changedEvents.Count()); Assert.Equal(price2, changedEvents.ElementAt(0).Price); Assert.Equal(price3, changedEvents.ElementAt(1).Price); }