public void ToOrderedMutationEvent_Should_ReturnMutationForEachItem() { var product = ProductFactory.GetProduct("0"); Assert.Equal(10, product.Available); var mutations = ProductFactory.GetProductStockMutations(product.Id); var actual = MutationFactory.ToOrderedMutationEvent <Product, ProductStockMutation, string, int, DateTimeOffset>( x => x.Available, mutations, x => x.Id, x => x.Amount, x => x.OrderDate).ToList(); Assert.Equal(mutations.Count, actual.Count); var mutation1 = actual[0]; var mutation2 = actual.Last(); mutation1.Revert(product); product.Available.Should().Be(10 - mutations[0].Amount); mutation2.Apply(product); product.Available.Should().Be(10 - mutations[0].Amount + mutations.Last().Amount); }
internal static List <IEvent <Product, string> > GetChanges(Guid itemId) { var priceChanges = ProductFactory.GetProductPriceChanges(itemId).ToList(); var stockMutationChanges = ProductFactory.GetProductStockMutations(itemId).ToList(); var changes = ChangeFactory.ToOrderedChangeEvent <Product, ProductPriceChange, string, double, DateTime>( x => x.Price, 0, priceChanges, x => x.Id, x => x.Price, x => x.ChangeDate).Cast <IEvent <Product, string> >().ToList(); changes.AddRange(MutationFactory.ToMutationEvent <Product, ProductStockMutation, string, int>( x => x.Available, stockMutationChanges, x => x.Id, x => x.ApplyableAmount).Cast <IEvent <Product, string> >()); changes.AddRange(MutationFactory.ToOrderedMutationEvent <Product, ProductStockMutation, string, int, DateTime>( x => x.InStock, stockMutationChanges, x => x.Id, x => x.ApplyableAmount, x => x.DeliveryDate.Date).Cast <IEvent <Product, string> >()); return(changes); }
internal static DL.Models.Product SetProduct(ProductStock.DL.Models.Product product) { if (!ProductStocks.ContainsKey(product.Id)) { ProductStocks.Add(product.Id, product); var result = new DL.Models.Product(product.Id, product.Number, product.Description); Products.Add(product.Id, result); var priceChanges = ChangeFactory.ToOrderedChangeEvent < DL.Models.Product, ProductStock.DL.Models.ProductPrice, Guid, double, DateTimeOffset>( x => x.Price, 0, product.Prices, x => x.Id, x => x.Price, x => x.RegisterDate). Cast <IEvent <DL.Models.Product, Guid> >().ToList(); var stockAvailableMutations = MutationFactory.ToOrderedMutationEvent < DL.Models.Product, ProductStock.DL.Models.ProductStockMutation, Guid, int, DateTimeOffset>( x => x.Available, product.StockMutations, x => x.Id, x => x.Amount * (x.Type == ProductStock.DL.Enums.MutationType.Purchase ? -1 : 1), x => x.OrderDate).Cast <IEvent <DL.Models.Product, Guid> >().ToList(); var stockInStockMutations = MutationFactory.ToOrderedMutationEvent < DL.Models.Product, ProductStock.DL.Models.ProductStockMutation, Guid, int, DateTimeOffset>( x => x.InStock, product.StockMutations, x => x.Id, x => x.Amount * (x.Type == ProductStock.DL.Enums.MutationType.Purchase ? -1 : 1), x => x.ShipmentDate).Cast <IEvent <DL.Models.Product, Guid> >().ToList(); AddEvents(product.Id, new OrderedEventCollection <DL.Models.Product, Guid, DateTimeOffset>(priceChanges.ToList())); AddEvents(product.Id, new OrderedEventCollection <DL.Models.Product, Guid, DateTimeOffset>(stockAvailableMutations.ToList())); AddEvents(product.Id, new OrderedEventCollection <DL.Models.Product, Guid, DateTimeOffset>(stockInStockMutations.ToList())); return(result); } return(Products[product.Id]); }