-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
100 additions
and
30 deletions.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
code/src/MicroCommerce.ApiService/Domain/Events/DomainEventBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
using MassTransit; | ||
|
||
namespace MicroCommerce.ApiService.Domain.Events; | ||
|
||
public abstract record DomainEventBase : IDomainEvent | ||
public abstract record DomainEventBase : IDomainEvent, CorrelatedBy<Guid> | ||
{ | ||
public string EventType => GetType().FullName!; | ||
public Guid EventId { get; } = Guid.CreateVersion7(); | ||
public DateTimeOffset CreatedAt { get; } = DateTimeOffset.UtcNow; | ||
public Guid CorrelationId { get; init; } = Guid.CreateVersion7(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
code/src/MicroCommerce.ApiService/Features/DomainEvents/OrderCreatedDomainEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using MassTransit; | ||
using MicroCommerce.ApiService.Domain.Events; | ||
using RedLockNet; | ||
|
||
namespace MicroCommerce.ApiService.Features.DomainEvents; | ||
|
||
public class OrderCreatedDomainEvent : IDomainEvent | ||
{ | ||
public OrderCreatedDomainEvent(Guid cartId) | ||
{ | ||
CartId = cartId; | ||
} | ||
|
||
public Guid CartId { get; } | ||
} | ||
|
||
public class OrderCreatedDomainEventConsumer : IConsumer<OrderCreatedDomainEvent> | ||
{ | ||
private readonly IDistributedLockFactory _distributedLockFactory; | ||
private readonly ILogger<OrderCreatedDomainEventConsumer> _logger; | ||
|
||
public OrderCreatedDomainEventConsumer(ILogger<OrderCreatedDomainEventConsumer> logger, IDistributedLockFactory distributedLockFactory) | ||
{ | ||
_logger = logger; | ||
_distributedLockFactory = distributedLockFactory; | ||
} | ||
|
||
public async Task Consume(ConsumeContext<OrderCreatedDomainEvent> context) | ||
{ | ||
var @event = context.Message; | ||
|
||
_logger.LogInformation("Order created for cart {CartId}", @event.CartId); | ||
|
||
await using var lockHandle = await _distributedLockFactory.CreateLockAsync(@event.CartId.ToString(), TimeSpan.FromSeconds(30)); | ||
|
||
if (!lockHandle.IsAcquired) | ||
{ | ||
_logger.LogWarning("Failed to acquire lock for cart {CartId}", @event.CartId); | ||
return; | ||
} | ||
|
||
_logger.LogInformation("Lock acquired for cart {CartId}", @event.CartId); | ||
|
||
// Process order | ||
|
||
_logger.LogInformation("Order processed for cart {CartId}", @event.CartId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
code/src/MicroCommerce.ApiService/Services/QueryableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace MicroCommerce.ApiService.Services; | ||
|
||
public static class QueryableExtensions | ||
{ | ||
public static IQueryable<T> WhereIf<T>(this IQueryable<T> queryable, bool condition, Expression<Func<T, bool>> predicate) | ||
{ | ||
return condition ? queryable.Where(predicate) : queryable; | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters