|
| 1 | +using Foundatio.Mediator; |
| 2 | +using Orders.Module.Messages; |
| 3 | +using Products.Module.Messages; |
| 4 | + |
| 5 | +namespace WebApp.Api; |
| 6 | + |
| 7 | +public static class DashboardApi |
| 8 | +{ |
| 9 | + public static void MapDashboardEndpoints(this IEndpointRouteBuilder endpoints) |
| 10 | + { |
| 11 | + var group = endpoints.MapGroup("/api/dashboard") |
| 12 | + .WithTags("Dashboard"); |
| 13 | + |
| 14 | + group.MapGet("/", async (IMediator mediator) => |
| 15 | + { |
| 16 | + var ordersTask = mediator.InvokeAsync<Result<List<Order>>>(new GetOrders()); |
| 17 | + var productsTask = mediator.InvokeAsync<Result<List<Product>>>(new GetProducts()); |
| 18 | + |
| 19 | + await Task.WhenAll(ordersTask.AsTask(), productsTask.AsTask()); |
| 20 | + |
| 21 | + var orders = ordersTask.Result; |
| 22 | + var products = productsTask.Result; |
| 23 | + |
| 24 | + return Results.Ok(new DashboardResponse( |
| 25 | + TotalOrders: orders.IsSuccess ? orders.Value?.Count ?? 0 : 0, |
| 26 | + TotalProducts: products.IsSuccess ? products.Value?.Count ?? 0 : 0, |
| 27 | + TotalRevenue: orders.IsSuccess ? orders.Value?.Sum(o => o.Amount) ?? 0 : 0, |
| 28 | + RecentOrders: orders.IsSuccess ? orders.Value?.OrderByDescending(o => o.CreatedAt).Take(5).ToList() ?? [] : [], |
| 29 | + RecentProducts: products.IsSuccess ? products.Value?.OrderByDescending(p => p.CreatedAt).Take(5).ToList() ?? [] : [] |
| 30 | + )); |
| 31 | + }) |
| 32 | + .WithName("GetDashboard") |
| 33 | + .WithSummary("Get dashboard overview with orders and products summary"); |
| 34 | + |
| 35 | + group.MapPost("/quick-order", async (QuickOrderRequest request, IMediator mediator) => |
| 36 | + { |
| 37 | + var command = new CreateOrder(request.CustomerId, request.Amount, request.Description ?? "Quick order"); |
| 38 | + var result = await mediator.InvokeAsync<Result<Order>>(command); |
| 39 | + |
| 40 | + return result.IsSuccess |
| 41 | + ? Results.Created($"/api/orders/{result.Value?.Id}", result.Value) |
| 42 | + : Results.BadRequest(new { error = result.Message }); |
| 43 | + }) |
| 44 | + .WithName("QuickOrder") |
| 45 | + .WithSummary("Create a quick order with minimal input"); |
| 46 | + |
| 47 | + group.MapPost("/quick-product", async (QuickProductRequest request, IMediator mediator) => |
| 48 | + { |
| 49 | + var command = new CreateProduct(request.Name, request.Description ?? $"Product: {request.Name}", request.Price); |
| 50 | + var result = await mediator.InvokeAsync<Result<Product>>(command); |
| 51 | + |
| 52 | + return result.IsSuccess |
| 53 | + ? Results.Created($"/api/products/{result.Value?.Id}", result.Value) |
| 54 | + : Results.BadRequest(new { error = result.Message }); |
| 55 | + }) |
| 56 | + .WithName("QuickProduct") |
| 57 | + .WithSummary("Create a quick product with minimal input"); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Request/Response DTOs |
| 62 | +public record QuickOrderRequest(string CustomerId, decimal Amount, string? Description = null); |
| 63 | +public record QuickProductRequest(string Name, decimal Price, string? Description = null); |
| 64 | + |
| 65 | +public record DashboardResponse( |
| 66 | + int TotalOrders, |
| 67 | + int TotalProducts, |
| 68 | + decimal TotalRevenue, |
| 69 | + List<Order> RecentOrders, |
| 70 | + List<Product> RecentProducts); |
0 commit comments