|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Samples.HelloCart; |
| 8 | +using Samples.HelloCart.V1; |
| 9 | +using Stl.Async; |
| 10 | +using Stl.Extensibility; |
| 11 | +using static System.Console; |
| 12 | + |
| 13 | +// This is our initial data |
| 14 | +var pApple = new Product { Id = "apple", Price = 1.3M }; |
| 15 | +var pBanana = new Product { Id = "banana", Price = 0.6M }; |
| 16 | +var pCarrot = new Product { Id = "carrot", Price = 0.9M }; |
| 17 | +var cart1 = new Cart() { Id = "cart_1apple_2banana", |
| 18 | + Items = ImmutableDictionary<string, decimal>.Empty |
| 19 | + .Add(pApple.Id, 1) |
| 20 | + .Add(pBanana.Id, 2) |
| 21 | +}; |
| 22 | +var cart2 = new Cart() { Id = "cart_1banana_1carrot", |
| 23 | + Items = ImmutableDictionary<string, decimal>.Empty |
| 24 | + .Add(pBanana.Id, 1) |
| 25 | + .Add(pCarrot.Id, 1) |
| 26 | +}; |
| 27 | + |
| 28 | +// Creating services & add initial data there |
| 29 | +await using var services = new ServiceCollection() |
| 30 | + .UseModules(modules => modules.Add<InMemoryModule>()) |
| 31 | + .AddSingleton<Watcher>() |
| 32 | + .BuildServiceProvider(); |
| 33 | +var products = services.GetRequiredService<IProductService>(); |
| 34 | +var carts = services.GetRequiredService<ICartService>(); |
| 35 | +await products.EditAsync(new EditCommand<Product>(pApple)); |
| 36 | +await products.EditAsync(new EditCommand<Product>(pBanana)); |
| 37 | +await products.EditAsync(new EditCommand<Product>(pCarrot)); |
| 38 | +await carts.EditAsync(new EditCommand<Cart>(cart1)); |
| 39 | +await carts.EditAsync(new EditCommand<Cart>(cart2)); |
| 40 | + |
| 41 | +using var stopCts = new CancellationTokenSource(); |
| 42 | +var watcher = services.GetRequiredService<Watcher>(); |
| 43 | +watcher.WatchAsync(new[] {pApple, pBanana, pCarrot}, new[] {cart1, cart2}, stopCts.Token).Ignore(); |
| 44 | + |
| 45 | +await Task.Delay(100); // Just to make sure watch tasks print whatever they want before our prompt appears |
| 46 | +WriteLine(); |
| 47 | +WriteLine("Change product price by typing [productId]=[price], e.g. \"apple=0\"."); |
| 48 | +WriteLine("See the total of every affected cart changes."); |
| 49 | +while (true) { |
| 50 | + await Task.Delay(100); |
| 51 | + Write("[productId]=[price]: "); |
| 52 | + try { |
| 53 | + var parts = (ReadLine() ?? "").Split("="); |
| 54 | + if (parts.Length != 2) |
| 55 | + throw new ApplicationException("Invalid price expression."); |
| 56 | + var productId = parts[0]; |
| 57 | + var price = decimal.Parse(parts[1]); |
| 58 | + var product = await products.FindAsync(productId); |
| 59 | + if (product == null) |
| 60 | + throw new KeyNotFoundException("Specified product doesn't exist."); |
| 61 | + await products.EditAsync(new EditCommand<Product>(product with { Price = price })); |
| 62 | + } |
| 63 | + catch (Exception e) { |
| 64 | + WriteLine($"Error: {e.Message}"); |
| 65 | + } |
| 66 | +} |
0 commit comments