using Dapr.Client; using Dapr.Workflow; using WorkflowApp; using WorkflowApp.Activities; var builder = WebApplication.CreateBuilder(args); builder.Services.AddSingleton(DaprClient.CreateInvokeHttpClient(appId: "shipping")); builder.Services.AddSingleton(); builder.Services.AddDaprClient(); builder.Services.AddDaprWorkflow(options => { options.RegisterWorkflow(); options.RegisterActivity(); options.RegisterActivity(); options.RegisterActivity(); options.RegisterActivity(); options.RegisterActivity(); options.RegisterActivity(); }); var app = builder.Build(); app.UseCloudEvents(); app.MapPost("/start", async ( Order order, InventoryManagement inventory) => { // This is to ensure to have enough inventory for the order. await inventory.CreateDefaultInventoryAsync(); await using var scope = app.Services.CreateAsyncScope(); var workflowClient = scope.ServiceProvider.GetRequiredService(); var instanceId = await workflowClient.ScheduleNewWorkflowAsync( name: nameof(OrderWorkflow), instanceId: order.Id, input: order); return Results.Accepted(instanceId); }); app.MapPost("/shipmentRegistered", async (ShipmentRegistrationStatus status) => { await using var scope = app.Services.CreateAsyncScope(); var workflowClient = scope.ServiceProvider.GetRequiredService(); Console.WriteLine($"Shipment registered for order {status}"); await workflowClient.RaiseEventAsync( instanceId: status.OrderId, eventName: Constants.SHIPMENT_REGISTERED_EVENT, status); return Results.Accepted(); }); app.MapPost("/inventory/restock", async ( ProductInventory productInventory, DaprClient daprClient ) => { await daprClient.SaveStateAsync( Constants.DAPR_INVENTORY_COMPONENT, productInventory.ProductId, productInventory); return Results.Accepted(); }); app.MapGet("/inventory/{productId}", async ( string productId, DaprClient daprClient ) => { var productInventory = await daprClient.GetStateAsync( Constants.DAPR_INVENTORY_COMPONENT, productId); if (productInventory == null) { return Results.NotFound(); } return Results.Ok(productInventory); }); app.Run(); internal sealed record ProductInventory(string ProductId, int Quantity); internal sealed record Order(string Id, OrderItem OrderItem, CustomerInfo CustomerInfo); internal sealed record OrderItem(string ProductId, string ProductName, int Quantity, decimal TotalPrice); internal sealed record CustomerInfo(string Id, string Country); internal sealed record ShipmentRegistrationStatus(string OrderId, bool IsSuccess, string Message = "");