Skip to content

Commit d326fd8

Browse files
committed
fix(07): consolidate saga contracts and add OrderSubmittedConsumer
- Remove duplicate Contracts.cs (07-02), use saga Contracts.cs (07-03) - Add OrderSubmittedConsumer to bridge domain event to CheckoutStarted saga event - Update SimulatePaymentCommandHandler to use saga namespace contracts
1 parent e3f5f8e commit d326fd8

3 files changed

Lines changed: 43 additions & 16 deletions

File tree

src/MicroCommerce.ApiService/Features/Ordering/Application/Commands/SimulatePayment/SimulatePaymentCommandHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MassTransit;
22
using MediatR;
3+
using MicroCommerce.ApiService.Features.Ordering.Application.Saga;
34
using MicroCommerce.ApiService.Features.Ordering.Domain.ValueObjects;
45
using MicroCommerce.ApiService.Features.Ordering.Infrastructure;
56
using Microsoft.EntityFrameworkCore;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using MassTransit;
2+
using MicroCommerce.ApiService.Features.Ordering.Application.Saga;
3+
using MicroCommerce.ApiService.Features.Ordering.Domain.Events;
4+
using MicroCommerce.ApiService.Features.Ordering.Domain.ValueObjects;
5+
using MicroCommerce.ApiService.Features.Ordering.Infrastructure;
6+
using Microsoft.EntityFrameworkCore;
7+
8+
namespace MicroCommerce.ApiService.Features.Ordering.Application.Consumers;
9+
10+
/// <summary>
11+
/// Bridges the domain event (OrderSubmittedDomainEvent) to the checkout saga (CheckoutStarted).
12+
/// Domain events are thin (ID-only); this consumer loads order data and publishes the saga-initiating event.
13+
/// </summary>
14+
public sealed class OrderSubmittedConsumer(OrderingDbContext context)
15+
: IConsumer<OrderSubmittedDomainEvent>
16+
{
17+
public async Task Consume(ConsumeContext<OrderSubmittedDomainEvent> context1)
18+
{
19+
OrderId orderId = OrderId.From(context1.Message.OrderId);
20+
21+
Domain.Entities.Order? order = await context.Orders
22+
.Include(o => o.Items)
23+
.FirstOrDefaultAsync(o => o.Id == orderId);
24+
25+
if (order is null)
26+
{
27+
return;
28+
}
29+
30+
List<CheckoutItem> items = order.Items
31+
.Select(i => new CheckoutItem { ProductId = i.ProductId, Quantity = i.Quantity })
32+
.ToList();
33+
34+
await context1.Publish(new CheckoutStarted
35+
{
36+
OrderId = order.Id.Value,
37+
BuyerId = order.BuyerId,
38+
BuyerEmail = order.BuyerEmail,
39+
Items = items
40+
});
41+
}
42+
}

src/MicroCommerce.ApiService/Features/Ordering/Contracts.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)