public static void AlmostOrderedArticlesReport() { var shoppingCartCreated = DateTime.UtcNow; var article1Ordered = shoppingCartCreated.AddMinutes(1); var article2Ordered = shoppingCartCreated.AddMinutes(2); var article3Ordered = shoppingCartCreated.AddMinutes(3); var article2Removed = shoppingCartCreated.AddMinutes(4); var article1Removed = shoppingCartCreated.AddMinutes(9); var ordered = shoppingCartCreated.AddMinutes(10); var state = new AlmostOrderedArticlesState(); state = state.Apply(new ShoppingCartCreated("ShoppingCart-Id", "Great-Customer-Id", "Great Customer", shoppingCartCreated)); state = state.Apply(new ShoppingCartArticlePlaced("ShoppingCart-Id", "ShoppingCartArticle-Id", "Article-Id", "A12345", "A great Article", Money.EUR(99.99m), 1, Money.EUR(99.99m), article1Ordered)); state = state.Apply(new ShoppingCartArticlePlaced("ShoppingCart-Id", "ShoppingCartArticle2-Id", "Article2-Id", "B12345", "Another great Article", Money.EUR(59.99m), 1, Money.EUR(99.99m), article2Ordered)); state = state.Apply(new ShoppingCartArticlePlaced("ShoppingCart-Id", "ShoppingCartArticle3-Id", "Article3-Id", "C12345", "Yet another great Article", Money.EUR(39.99m), 1, Money.EUR(99.99m), article3Ordered)); state = state.Apply(new ShoppingCartArticleRemoved("ShoppingCart-Id", "ShoppingCartArticle2-Id", "Article2-Id", "B12345", "Yet yet another great Article", Money.EUR(59.99m), 1, Money.EUR(99.99m), article2Removed)); state = state.Apply(new ShoppingCartArticleRemoved("ShoppingCart-Id", "ShoppingCartArticle-Id", "Article-Id", "A12345", "Yet yet yet another great Article", Money.EUR(99.99m), 1, Money.EUR(99.99m), article1Removed)); state = state.Apply(new ShoppingCartOrdered("ShoppingCart-Id", ordered)); Console.WriteLine("Almost ordered shopping cart articles: "); foreach (var entry in state.AlmostOrderedShoppingCartArticles) { Console.WriteLine($"{entry.CustomerName}: {string.Join(", ", entry.Articlenumber)} - {entry.Timespan} before placing order"); } Console.WriteLine(); }
public static async IAsyncEnumerable <AlmostOrderedArticlesState.ShoppingCartArticleRemovedInfo> AnalyseAlmostOrderedWithState(IPersistenceEngine engine = null) { engine = engine ?? Program.engine; var loadedMessages = await engine.LoadStreamEntriesAsync( 0, int.MaxValue, new[] { typeof(ShoppingCartCreated), typeof(ShoppingCartArticleRemoved), typeof(ShoppingCartOrdered), typeof(ShoppingCartCancelled) }) .Select(x => engine.Serializer.Deserialize(x.Payload)) .ToListAsync(); Console.WriteLine("Removed Articles: "); var almostOrderedState = AlmostOrderedArticlesState.LoadState((AlmostOrderedArticlesState)null, loadedMessages); var removedArticles = almostOrderedState .AlmostOrderedShoppingCartArticles .ToList(); var groupedByCustomer = removedArticles .GroupBy(x => x.CustomerName) .Select(x => new { CustomerName = x.Key, Articlenumbers = x.GroupBy(y => y.Articlenumber) .Select(y => new { Count = y.Count(), Articlenumber = y.Key }) .ToList() }) .ToList(); foreach (var item in groupedByCustomer) { Console.WriteLine($"{item.CustomerName}: {string.Join(", ", item.Articlenumbers.Select(x => "" + x.Count + "x " + x.Articlenumber))} "); } Console.WriteLine(); foreach (var item in removedArticles) { yield return(item); } }