mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-17 16:36:25 +08:00
.
This commit is contained in:
parent
b395a6b9f4
commit
a702916b91
@ -4,5 +4,5 @@ namespace BuildingBlocks.Core.Event;
|
||||
public enum EventType
|
||||
{
|
||||
IntegrationEvent = 1,
|
||||
DomainEvent = 2,
|
||||
DomainEvent = 2
|
||||
}
|
||||
|
||||
@ -2,5 +2,4 @@ namespace BuildingBlocks.Core.Event;
|
||||
|
||||
public interface IDomainEvent : IEvent
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@ -30,36 +30,43 @@ public sealed class EventDispatcher : IEventDispatcher
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public async Task SendAsync(IDomainEvent domainEvent,
|
||||
CancellationToken cancellationToken = default) => await SendAsync(new[] {domainEvent}, cancellationToken);
|
||||
|
||||
public async Task SendAsync(IReadOnlyList<IDomainEvent> domainEvents, CancellationToken cancellationToken = default)
|
||||
public async Task SendAsync<T>(IReadOnlyList<T> events, CancellationToken cancellationToken = default)
|
||||
where T : IEvent
|
||||
{
|
||||
if (domainEvents is null) return;
|
||||
|
||||
var integrationEvents = await MapDomainEventToIntegrationEventAsync(domainEvents).ConfigureAwait(false);
|
||||
|
||||
if (integrationEvents.Count == 0) return;
|
||||
|
||||
foreach (var integrationEvent in integrationEvents)
|
||||
async Task PublishIntegrationEvent(IReadOnlyList<IIntegrationEvent> integrationEvents)
|
||||
{
|
||||
await _persistMessageProcessor.PublishMessageAsync(new MessageEnvelope(integrationEvent, SetHeaders()),
|
||||
cancellationToken);
|
||||
foreach (var integrationEvent in integrationEvents)
|
||||
{
|
||||
await _persistMessageProcessor.PublishMessageAsync(new MessageEnvelope(integrationEvent, SetHeaders()),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
if (events.Count > 0)
|
||||
{
|
||||
switch (events)
|
||||
{
|
||||
case IReadOnlyList<IDomainEvent> domainEvents:
|
||||
{
|
||||
var integrationEvents = await MapDomainEventToIntegrationEventAsync(domainEvents)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
await PublishIntegrationEvent(integrationEvents);
|
||||
break;
|
||||
}
|
||||
|
||||
case IReadOnlyList<IIntegrationEvent> integrationEvents:
|
||||
await PublishIntegrationEvent(integrationEvents);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendAsync<T>(T @event, CancellationToken cancellationToken = default)
|
||||
where T : IEvent =>
|
||||
await SendAsync(new[] {@event}, cancellationToken);
|
||||
|
||||
public async Task SendAsync(IIntegrationEvent integrationEvent,
|
||||
CancellationToken cancellationToken = default) => await SendAsync(new[] {integrationEvent}, cancellationToken);
|
||||
|
||||
public async Task SendAsync(IReadOnlyList<IIntegrationEvent> integrationEvents,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (integrationEvents is null) return;
|
||||
|
||||
await _persistMessageProcessor.PublishMessageAsync(new MessageEnvelope(integrationEvents, SetHeaders()),
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
private Task<IReadOnlyList<IIntegrationEvent>> MapDomainEventToIntegrationEventAsync(
|
||||
IReadOnlyList<IDomainEvent> events)
|
||||
|
||||
@ -4,9 +4,8 @@ namespace BuildingBlocks.Core;
|
||||
|
||||
public interface IEventDispatcher
|
||||
{
|
||||
public Task SendAsync(IReadOnlyList<IDomainEvent> domainEvents, CancellationToken cancellationToken = default);
|
||||
public Task SendAsync(IDomainEvent domainEvent, CancellationToken cancellationToken = default);
|
||||
|
||||
public Task SendAsync(IIntegrationEvent integrationEvent, CancellationToken cancellationToken = default);
|
||||
public Task SendAsync(IReadOnlyList<IIntegrationEvent> integrationEvents, CancellationToken cancellationToken = default);
|
||||
public Task SendAsync<T>(IReadOnlyList<T> events, CancellationToken cancellationToken = default)
|
||||
where T : IEvent;
|
||||
public Task SendAsync<T>(T @event, CancellationToken cancellationToken = default)
|
||||
where T : IEvent;
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using System.Data;
|
||||
using System.Text.Json;
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.Core.Event;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
@ -1,11 +1,15 @@
|
||||
using System.Threading;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Ardalis.GuardClauses;
|
||||
using BuildingBlocks.Core.CQRS;
|
||||
using Flight.Data;
|
||||
using Flight.Flights.Exceptions;
|
||||
using Flight.Flights.Models.Reads;
|
||||
using MapsterMapper;
|
||||
using MediatR;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
|
||||
namespace Flight.Flights.Features.CreateFlight.Reads;
|
||||
|
||||
@ -28,6 +32,12 @@ public class CreateFlightMongoCommandHandler : ICommandHandler<CreateFlightMongo
|
||||
|
||||
var flightReadModel = _mapper.Map<FlightReadModel>(command);
|
||||
|
||||
var flight = await _flightReadDbContext.Flight.AsQueryable()
|
||||
.FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);
|
||||
|
||||
if (flight is not null)
|
||||
throw new FlightAlreadyExistException();
|
||||
|
||||
await _flightReadDbContext.Flight.InsertOneAsync(flightReadModel, cancellationToken: cancellationToken);
|
||||
|
||||
return Unit.Value;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user