Skip to content

Commit

Permalink
Moved orders queries to controller
Browse files Browse the repository at this point in the history
Added also some questions to rethink the design by viewers
  • Loading branch information
oskardudycz committed May 17, 2024
1 parent 8af83d7 commit 7038643
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public async Task SubscribeToAll(EventStoreDBSubscriptionToAllOptions subscripti
}
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
Expand Down
6 changes: 4 additions & 2 deletions Core/Commands/InMemoryCommandBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ await retryPolicy.ExecuteAsync((token) =>

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<CommandHandlerMetrics>();
services.AddSingleton<CommandHandlerActivity>();
Expand Down
30 changes: 18 additions & 12 deletions Sample/ECommerce/Orders/Orders.Api/Controllers/CartsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<IActionResult> InitOrder([FromBody] InitOrderRequest? request)
{
Expand All @@ -28,7 +31,7 @@ public async Task<IActionResult> 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
);

Expand Down Expand Up @@ -77,11 +80,14 @@ public async Task<IActionResult> ConfirmOrder(Guid id)
}

[HttpGet("{id}")]
public async Task<IActionResult> GetStatus(Guid id)
{
var query = GetOrderStatus.For(id);

return await queryBus.Query<GetOrderStatus, OrderDetails?>(query) is {} details ?
Ok(details) : NotFound();
}
public Task GetStatus(Guid id) =>
querySession.Json.WriteById<OrderDetails>(id, HttpContext);

[HttpGet]
public Task<IPagedList<PendingOrder>> GetPending(
[FromQuery] int? pageNumber,
[FromQuery] int? pageSize,
CancellationToken ct
) =>
querySession.Query<PendingOrder>().ToPagedListAsync(pageNumber ?? 1, pageSize ?? 10, ct);
}
1 change: 1 addition & 0 deletions Sample/ECommerce/Orders/Orders.Api/Orders.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Marten.AspNetCore" Version="7.10.1" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.2" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.5.1" />
<PackageReference Include="Aspire.Confluent.Kafka" Version="8.0.0-preview.7.24251.11"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Core.Marten.Repository;
using Marten;
using Orders.Orders.GettingOrderStatus;
using Orders.Orders.GettingPending;

namespace Orders.Orders.CancellingOrder;

Expand Down
5 changes: 1 addition & 4 deletions Sample/ECommerce/Orders/Orders/Orders/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +25,6 @@ internal static IServiceCollection AddOrders(this IServiceCollection services) =
services
.AddMartenRepository<Order>()
.AddCommandHandlers()
.AddQueryHandlers()
.AddEventHandlers();

private static IServiceCollection AddCommandHandlers(this IServiceCollection services) =>
Expand All @@ -33,9 +33,6 @@ private static IServiceCollection AddCommandHandlers(this IServiceCollection ser
.AddCommandHandler<CompleteOrder, HandleCompleteOrder>()
.AddCommandHandler<CancelOrder, HandleCancelOrder>();

private static IServiceCollection AddQueryHandlers(this IServiceCollection services) =>
services.AddQueryHandler<GetOrderStatus, OrderDetails?, HandleGetOrderStatus>();

private static IServiceCollection AddEventHandlers(this IServiceCollection services) =>
services.AddEventHandler<CartFinalized, OrderSaga>()
.AddEventHandler<OrderInitiated, OrderSaga>()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions Sample/ECommerce/Orders/Orders/Orders/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Sample/ECommerce/Orders/Orders/Orders/OrderEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
}
11 changes: 4 additions & 7 deletions Sample/Helpdesk.Wolverine/Helpdesk.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}

0 comments on commit 7038643

Please sign in to comment.