private void QueryReceived(object sender, MsgHandlerEventArgs args) { Console.WriteLine("Query received."); // messageType is ignored for now - only 1 query supported try { using (var dbContext = new StoreDBContext()) { StringBuilder ordersList = new StringBuilder($"Order#\t| Status\t| Total amount\n"); ordersList.AppendLine($"--------|---------------|----------------"); foreach (Order order in dbContext.Orders) { ordersList.AppendLine($"{order.OrderNumber}\t| {order.Status}\t| {order.TotalPrice}"); } if (args.Message.Reply != null) { string message = ordersList.ToString(); _natsConnection.Publish(args.Message.Reply, Encoding.UTF8.GetBytes(message)); } } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($"Error: {ex.InnerException.Message}"); } } }
private async void Handle(OrderCreated e) { Console.WriteLine($"Order #{e.OrderNumber} created."); using (var dbContext = new StoreDBContext()) { dbContext.Orders.Add(new Order { OrderNumber = e.OrderNumber, Status = "In progress" }); await dbContext.SaveChangesAsync(); } }
private async void Handle(OrderCancelled e) { Console.WriteLine($"Order #{e.OrderNumber} cancelled."); using (var dbContext = new StoreDBContext()) { var order = dbContext.Orders.FirstOrDefault(o => o.OrderNumber == e.OrderNumber); if (order != null) { dbContext.Orders.Remove(order); await dbContext.SaveChangesAsync(); } } }
private async void Handle(OrderShipped e) { Console.WriteLine($"Order #{e.OrderNumber} shipped."); using (var dbContext = new StoreDBContext()) { var order = dbContext.Orders.FirstOrDefault(o => o.OrderNumber == e.OrderNumber); if (order != null) { order.Status = "Shipped"; await dbContext.SaveChangesAsync(); } } }
private async void Handle(ProductOrdered e) { Console.WriteLine($"Product #{e.ProductNumber} added to order #{e.OrderNumber}."); using (var dbContext = new StoreDBContext()) { var order = dbContext.Orders.FirstOrDefault(o => o.OrderNumber == e.OrderNumber); if (order != null) { order.Products.Add(new OrderedProduct { Id = Guid.NewGuid().ToString("N"), ProductNumber = e.ProductNumber, Price = e.Price }); order.TotalPrice += e.Price; await dbContext.SaveChangesAsync(); } } }
private async void Handle(ProductRemoved e) { Console.WriteLine($"Product #{e.ProductNumber} removed from order #{e.OrderNumber}."); using (var dbContext = new StoreDBContext()) { var order = dbContext.Orders.FirstOrDefault(o => o.OrderNumber == e.OrderNumber); if (order != null) { dbContext.Entry(order).Collection(o => o.Products).Load(); var product = order.Products.FirstOrDefault(p => p.ProductNumber == e.ProductNumber); if (product != null) { order.Products.Remove(product); order.TotalPrice -= product.Price; await dbContext.SaveChangesAsync(); } } } }