diff --git a/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs b/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs index eb8d39c8b..113890175 100644 --- a/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs +++ b/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs @@ -78,7 +78,7 @@ await foreach (var @event in subscription) } catch (Exception ex) { - logger.LogWarning("Subscription was dropped: {ex}", ex); + logger.LogWarning("Subscription was dropped: {Exception}", ex); // Sleep between reconnections to not flood the database or not kill the CPU with infinite loop // Randomness added to reduce the chance of multiple subscriptions trying to reconnect at the same time diff --git a/Core/Commands/InMemoryCommandBus.cs b/Core/Commands/InMemoryCommandBus.cs index e3f1e3a3c..e64e77334 100644 --- a/Core/Commands/InMemoryCommandBus.cs +++ b/Core/Commands/InMemoryCommandBus.cs @@ -40,8 +40,10 @@ public async Task Send(TCommand command, CancellationToken ct = defaul public static class EventBusExtensions { - public static IServiceCollection AddInMemoryCommandBus(this IServiceCollection services, - AsyncPolicy? asyncPolicy = null) + public static IServiceCollection AddInMemoryCommandBus( + this IServiceCollection services, + AsyncPolicy? asyncPolicy = null + ) { services.AddSingleton(); services.AddSingleton(); diff --git a/Sample/ECommerce/Orders/Orders.Api/Controllers/CartsController.cs b/Sample/ECommerce/Orders/Orders.Api/Controllers/CartsController.cs index daaf00263..97ae88cfe 100644 --- a/Sample/ECommerce/Orders/Orders.Api/Controllers/CartsController.cs +++ b/Sample/ECommerce/Orders/Orders.Api/Controllers/CartsController.cs @@ -2,9 +2,13 @@ using Core.Commands; using Core.Ids; using Core.Queries; +using Marten; +using Marten.AspNetCore; +using Marten.Pagination; using Orders.Api.Requests.Carts; using Orders.Orders.CompletingOrder; using Orders.Orders.GettingOrderStatus; +using Orders.Orders.GettingPending; using Orders.Orders.InitializingOrder; using Orders.Products; @@ -13,12 +17,11 @@ namespace Orders.Api.Controllers; [Route("api/[controller]")] public class OrdersController( ICommandBus commandBus, - IQueryBus queryBus, - IIdGenerator idGenerator) + IQuerySession querySession, + IIdGenerator idGenerator +) : Controller { - private readonly IQueryBus queryBus = queryBus; - [HttpPost] public async Task InitOrder([FromBody] InitOrderRequest? request) { @@ -28,7 +31,7 @@ public async Task InitOrder([FromBody] InitOrderRequest? request) orderId, request?.ClientId, request?.ProductItems?.Select( - pi => PricedProductItem.Create(pi.ProductId, pi.Quantity,pi.UnitPrice)).ToList(), + pi => PricedProductItem.Create(pi.ProductId, pi.Quantity, pi.UnitPrice)).ToList(), request?.TotalPrice ); @@ -77,11 +80,14 @@ public async Task ConfirmOrder(Guid id) } [HttpGet("{id}")] - public async Task GetStatus(Guid id) - { - var query = GetOrderStatus.For(id); - - return await queryBus.Query(query) is {} details ? - Ok(details) : NotFound(); - } + public Task GetStatus(Guid id) => + querySession.Json.WriteById(id, HttpContext); + + [HttpGet] + public Task> GetPending( + [FromQuery] int? pageNumber, + [FromQuery] int? pageSize, + CancellationToken ct + ) => + querySession.Query().ToPagedListAsync(pageNumber ?? 1, pageSize ?? 10, ct); } diff --git a/Sample/ECommerce/Orders/Orders.Api/Orders.Api.csproj b/Sample/ECommerce/Orders/Orders.Api/Orders.Api.csproj index d2d6bd2c7..faeb52e80 100644 --- a/Sample/ECommerce/Orders/Orders.Api/Orders.Api.csproj +++ b/Sample/ECommerce/Orders/Orders.Api/Orders.Api.csproj @@ -5,6 +5,7 @@ + diff --git a/Sample/ECommerce/Orders/Orders/Orders/CancellingOrder/CancelOrder.cs b/Sample/ECommerce/Orders/Orders/Orders/CancellingOrder/CancelOrder.cs index 8c90e10be..e7149d895 100644 --- a/Sample/ECommerce/Orders/Orders/Orders/CancellingOrder/CancelOrder.cs +++ b/Sample/ECommerce/Orders/Orders/Orders/CancellingOrder/CancelOrder.cs @@ -3,6 +3,7 @@ using Core.Marten.Repository; using Marten; using Orders.Orders.GettingOrderStatus; +using Orders.Orders.GettingPending; namespace Orders.Orders.CancellingOrder; diff --git a/Sample/ECommerce/Orders/Orders/Orders/Config.cs b/Sample/ECommerce/Orders/Orders/Orders/Config.cs index 3e4a5235a..4563244f1 100644 --- a/Sample/ECommerce/Orders/Orders/Orders/Config.cs +++ b/Sample/ECommerce/Orders/Orders/Orders/Config.cs @@ -8,6 +8,7 @@ using Orders.Orders.CancellingOrder; using Orders.Orders.CompletingOrder; using Orders.Orders.GettingOrderStatus; +using Orders.Orders.GettingPending; using Orders.Orders.InitializingOrder; using Orders.Orders.RecordingOrderPayment; using Orders.Payments.FinalizingPayment; @@ -24,7 +25,6 @@ internal static class OrdersConfig services .AddMartenRepository() .AddCommandHandlers() - .AddQueryHandlers() .AddEventHandlers(); private static IServiceCollection AddCommandHandlers(this IServiceCollection services) => @@ -33,9 +33,6 @@ internal static class OrdersConfig .AddCommandHandler() .AddCommandHandler(); - private static IServiceCollection AddQueryHandlers(this IServiceCollection services) => - services.AddQueryHandler(); - private static IServiceCollection AddEventHandlers(this IServiceCollection services) => services.AddEventHandler() .AddEventHandler() diff --git a/Sample/ECommerce/Orders/Orders/Orders/GettingOrderStatus/GetOrderStatus.cs b/Sample/ECommerce/Orders/Orders/Orders/GettingOrderStatus/GetOrderStatus.cs deleted file mode 100644 index 6e97dd898..000000000 --- a/Sample/ECommerce/Orders/Orders/Orders/GettingOrderStatus/GetOrderStatus.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Core.Queries; -using Core.Validation; -using Marten; - -namespace Orders.Orders.GettingOrderStatus; - -public record GetOrderStatus(Guid OrderId) -{ - public static GetOrderStatus For(Guid orderId) => new(orderId.NotEmpty()); -} - -internal class HandleGetOrderStatus(IQuerySession querySession): IQueryHandler -{ - public Task Handle(GetOrderStatus query, CancellationToken cancellationToken) => - querySession.LoadAsync(query.OrderId, cancellationToken); -} diff --git a/Sample/ECommerce/Orders/Orders/Orders/CancellingOrder/PendingOrder.cs b/Sample/ECommerce/Orders/Orders/Orders/GettingPendingOrders/PendingOrder.cs similarity index 83% rename from Sample/ECommerce/Orders/Orders/Orders/CancellingOrder/PendingOrder.cs rename to Sample/ECommerce/Orders/Orders/Orders/GettingPendingOrders/PendingOrder.cs index 2988fbf7b..4cf8367e0 100644 --- a/Sample/ECommerce/Orders/Orders/Orders/CancellingOrder/PendingOrder.cs +++ b/Sample/ECommerce/Orders/Orders/Orders/GettingPendingOrders/PendingOrder.cs @@ -1,8 +1,6 @@ using Marten.Events.Aggregation; -using Orders.Orders.CompletingOrder; -using Orders.Orders.InitializingOrder; -namespace Orders.Orders.CancellingOrder; +namespace Orders.Orders.GettingPending; using static OrderEvent; public class PendingOrder diff --git a/Sample/ECommerce/Orders/Orders/Orders/Order.cs b/Sample/ECommerce/Orders/Orders/Orders/Order.cs index 14a2af8e9..3a62eb126 100644 --- a/Sample/ECommerce/Orders/Orders/Orders/Order.cs +++ b/Sample/ECommerce/Orders/Orders/Orders/Order.cs @@ -77,12 +77,13 @@ public void Complete(DateTimeOffset now) public void Cancel(OrderCancellationReason cancellationReason, DateTimeOffset now) { + // QUESTION TO THE READER: Is throwing Exception here fine or not? if (OrderStatus.Closed.HasFlag(Status)) throw new InvalidOperationException("Cannot cancel a closed order."); - var @event = OrderCancelled.Create(Id, PaymentId, cancellationReason, now); - - Enqueue(@event); + Enqueue( + OrderCancelled.Create(Id, PaymentId, cancellationReason, now) + ); } public override void Apply(OrderEvent @event) diff --git a/Sample/ECommerce/Orders/Orders/Orders/OrderEvent.cs b/Sample/ECommerce/Orders/Orders/Orders/OrderEvent.cs index 047c82971..50d9656b8 100644 --- a/Sample/ECommerce/Orders/Orders/Orders/OrderEvent.cs +++ b/Sample/ECommerce/Orders/Orders/Orders/OrderEvent.cs @@ -82,5 +82,7 @@ DateTimeOffset cancelledAt new(orderId.NotEmpty(), paymentId.NotEmpty(), orderCancellationReason.NotEmpty(), cancelledAt.NotEmpty()); } + // QUESTION TO THE READER: How to split OrderCancelled into OrderCancelled and OrderTimedOut + private OrderEvent() { } } diff --git a/Sample/Helpdesk.Wolverine/Helpdesk.Api/Program.cs b/Sample/Helpdesk.Wolverine/Helpdesk.Api/Program.cs index 35ecfc479..42ac1280c 100644 --- a/Sample/Helpdesk.Wolverine/Helpdesk.Api/Program.cs +++ b/Sample/Helpdesk.Wolverine/Helpdesk.Api/Program.cs @@ -110,13 +110,10 @@ return await app.RunOaktonCommands(args); -namespace Helpdesk.Api +public class IncidentsHub: Hub { - public class IncidentsHub: Hub - { - } +} - public partial class Program - { - } +public partial class Program +{ }