mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-11 02:20:20 +08:00
add sample for multi-consumers for user-created
This commit is contained in:
parent
7e5b6c5f65
commit
a7e461bd4c
@ -17,10 +17,6 @@ public static class Extensions
|
||||
{
|
||||
public static WebApplicationBuilder AddCustomSerilog(this WebApplicationBuilder builder)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
.CreateBootstrapLogger();
|
||||
|
||||
builder.Host.UseSerilog((context, loggerConfiguration) =>
|
||||
{
|
||||
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||
@ -32,11 +28,11 @@ public static class Extensions
|
||||
: LogEventLevel.Information;
|
||||
|
||||
loggerConfiguration
|
||||
.WriteTo.Console()
|
||||
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(loggOptions.ElasticUri))
|
||||
{
|
||||
AutoRegisterTemplate = true,
|
||||
IndexFormat = $"{appOptions.Name}-{environment?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}"
|
||||
IndexFormat =
|
||||
$"{appOptions.Name}-{environment?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}"
|
||||
})
|
||||
.WriteTo.SpectreConsole(loggOptions.LogTemplate, logLevel)
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Error)
|
||||
|
||||
@ -2,10 +2,7 @@ using Booking;
|
||||
using Booking.Configuration;
|
||||
using Booking.Data;
|
||||
using Booking.Extensions;
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.EFCore;
|
||||
using BuildingBlocks.EventStoreDB;
|
||||
using BuildingBlocks.EventStoreDB.Projections;
|
||||
using BuildingBlocks.HealthCheck;
|
||||
using BuildingBlocks.IdsGenerator;
|
||||
using BuildingBlocks.Jwt;
|
||||
@ -15,7 +12,6 @@ using BuildingBlocks.MassTransit;
|
||||
using BuildingBlocks.Mongo;
|
||||
using BuildingBlocks.OpenTelemetry;
|
||||
using BuildingBlocks.PersistMessageProcessor;
|
||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
||||
using BuildingBlocks.Swagger;
|
||||
using BuildingBlocks.Web;
|
||||
using Figgle;
|
||||
@ -38,7 +34,7 @@ builder.Services.AddPersistMessage(configuration);
|
||||
builder.Services.AddMongoDbContext<BookingReadDbContext>(configuration);
|
||||
|
||||
builder.AddCustomSerilog();
|
||||
builder.Services.AddCore();
|
||||
builder.Services.AddCore(configuration);
|
||||
builder.Services.AddJwt();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
@ -1,13 +1,19 @@
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.Utils;
|
||||
using BuildingBlocks.Web;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Booking.Extensions;
|
||||
|
||||
public static class CoreExtensions
|
||||
{
|
||||
public static IServiceCollection AddCore(this IServiceCollection services)
|
||||
public static IServiceCollection AddCore(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddOptions<AppOptions>()
|
||||
.Bind(configuration.GetSection(nameof(AppOptions)))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
services.AddScoped<ICurrentUserProvider, CurrentUserProvider>();
|
||||
services.AddTransient<IEventMapper, EventMapper>();
|
||||
services.AddScoped<IEventDispatcher, EventDispatcher>();
|
||||
|
||||
@ -40,7 +40,7 @@ builder.Services.AddMongoDbContext<FlightReadDbContext>(configuration);
|
||||
builder.Services.AddPersistMessage(configuration);
|
||||
|
||||
builder.AddCustomSerilog();
|
||||
builder.Services.AddCore();
|
||||
builder.Services.AddCore(configuration);
|
||||
builder.Services.AddJwt();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddCustomSwagger(configuration, typeof(FlightRoot).Assembly);
|
||||
|
||||
@ -1,13 +1,19 @@
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.Utils;
|
||||
using BuildingBlocks.Web;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Flight.Extensions;
|
||||
|
||||
public static class CoreExtensions
|
||||
{
|
||||
public static IServiceCollection AddCore(this IServiceCollection services)
|
||||
public static IServiceCollection AddCore(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddOptions<AppOptions>()
|
||||
.Bind(configuration.GetSection(nameof(AppOptions)))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
services.AddScoped<ICurrentUserProvider, CurrentUserProvider>();
|
||||
services.AddTransient<IEventMapper, EventMapper>();
|
||||
services.AddScoped<IEventDispatcher, EventDispatcher>();
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using BuildingBlocks.Contracts.EventBus.Messages;
|
||||
using MassTransit;
|
||||
|
||||
namespace Flight;
|
||||
|
||||
public class FlightConsumer : IConsumer<FlightCreated>
|
||||
{
|
||||
public Task Consume(ConsumeContext<FlightCreated> context)
|
||||
{
|
||||
Console.WriteLine("This consumer is for test");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
using System.Threading.Tasks;
|
||||
using BuildingBlocks.Contracts.EventBus.Messages;
|
||||
using BuildingBlocks.Web;
|
||||
using Humanizer;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flight.Identity.Consumers.RegisterNewUser;
|
||||
|
||||
public class RegisterNewUserConsumerHandler : IConsumer<UserCreated>
|
||||
{
|
||||
private readonly AppOptions _options;
|
||||
private readonly ILogger<RegisterNewUserConsumerHandler> _logger;
|
||||
|
||||
public RegisterNewUserConsumerHandler(IOptions<AppOptions> options,
|
||||
ILogger<RegisterNewUserConsumerHandler> logger)
|
||||
{
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Consume(ConsumeContext<UserCreated> context)
|
||||
{
|
||||
_logger.LogInformation($"this is a test consumer for {nameof(UserCreated).Underscore()} in {_options.Name}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@ -31,7 +31,7 @@ Console.WriteLine(FiggleFonts.Standard.Render(appOptions.Name));
|
||||
builder.Services.AddPersistMessage(configuration);
|
||||
builder.Services.AddCustomDbContext<IdentityContext>(configuration);
|
||||
builder.Services.AddScoped<IDataSeeder, IdentityDataSeeder>();
|
||||
builder.Services.AddCore();
|
||||
builder.Services.AddCore(configuration);
|
||||
builder.AddCustomSerilog();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddCustomSwagger(configuration, typeof(IdentityRoot).Assembly);
|
||||
|
||||
@ -1 +0,0 @@
|
||||
{"Version":1,"Id":"278877ED714C54A83CDAEF062491E880","Created":"2022-05-10T18:36:41.851832Z","Algorithm":"RS256","IsX509Certificate":false,"Data":"CfDJ8Pyl4Q0BTFBNj7ITVoMSIQjZOI0m28DbhLN9FfcLU2RNyyq6bhVmtnMTE-lHtjLLyF4A5xI-Ah4YV38OKLaGxRgNCET8ahafy1N879KJMQE9AT9iOI3hkMsKT2aBPlQS9Qzm0S_PEzdDHMrY9y9rhdXZwG7hisjMGtuAXqCcCEQuFOXOn12yGLPMVtCmIVriElJ2evkem9Ccnpy02wywUSlM9bJ46w2a0MXRzTFvC-TGuEYYcShrij_quEqKqTdcmnO-gafbKrJBCwkrbpi0lZzmu5LMbPjYsj1GM4Sn0df3koL4ShP3-hI-kQYqmOBsHB742IHBMeIo_iIbfkeefCihTI6gUq7JgV6SpEM0zIbTLCH-fykB7wVlsmN8xa-X9fONpEQ9ZJfuLh0f9LiTrgjuCP2wgH_btlFmIN1dZb3o2PHLD1CfAkLrPAgMmNH1Wlc6XlMcD4aM9yLNVQX6JV4Wg4tfteolPuKGTznwk27ePZZ3iyEcEHW36A4OwnkA_n-4gjkpj7XCzS8NP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658ZMrj4rrIrG2U6s1sT6AdmLgt90svSYWqB9UmJuSHL4vq5bBkoXVEBzeGhPralPCdKZaX4Iq1lPBskWI8B7bd_XcqVvww7b9TOzQ0z6UOuURLXCjH_t595AVI8pVpxJenQ_W1ATaNBU8S0TpMioaI3LiMUjDg55ART-m6ywSzOzp3jWfwyTzgVJEDy65Ob2dUS7Qo1jvDIUIDHRobEmqj-lKQz7S7Xs6jFuDqsMMoHLImxcxCPNoWN13dbTNzCkz9JqnOGmm27b-jBd5lkIpn_4fgRo4rHdLVUXrrYx1cvdRyhqyMU8Qw1FAwf5KZ37fNBZQSyV-17ww_THTTN6-SO5fcGzsHZyyoj93qFvjROATMFaEU9J1WBtFbKg5jep8lpFvhtTiuuHVpdLxjmR3rJWEiDbjggnt66_a5MN9wlffXb7Dei9purK2KDYcbDcZIJya3ZjXhgYXk-cgLqDPsCCfc4m8gG9eNymy4AiB-golUBeB4FSUnp1O4wOqB7iA6enrDR5m-lFns0tWqvzG_77S40ewUMMIjHFZNtX3ILgzccrUtqSzb8fA-0cTSKH6oXxIl71jWe8Y7XFc5uH_T6Z1dQsWlN6y666sc75T3-35c1z4VHx8zzb5CU0JaWUX9zbvHuhKKxVKMzr-1FSBvi0OM77QU0FPqKBLieYVOqOPxCIVhbAguCEbXmItsbWiusx3yUHiKZnGCq2EkYLbJw3HgENX3kgayuuSbJOnc2XR8A-0eb1_8z5KEUCLzoG_ntZ5ay7WTtu5gBfBssSufXLfACrMXWyS2_bymuyr7F2QPOa6E-qLo6jQZgPjQivrxkTZN6CC_rmpva-KpEBmFQ1g2BD1g9i","DataProtected":true}
|
||||
@ -1,12 +1,18 @@
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.Web;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Identity.Extensions;
|
||||
|
||||
public static class CoreExtensions
|
||||
{
|
||||
public static IServiceCollection AddCore(this IServiceCollection services)
|
||||
public static IServiceCollection AddCore(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddOptions<AppOptions>()
|
||||
.Bind(configuration.GetSection(nameof(AppOptions)))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
services.AddTransient<IEventMapper, EventMapper>();
|
||||
services.AddScoped<IEventDispatcher, EventDispatcher>();
|
||||
|
||||
|
||||
@ -34,7 +34,7 @@ builder.Services.AddMongoDbContext<PassengerReadDbContext>(configuration);
|
||||
builder.Services.AddPersistMessage(configuration);
|
||||
|
||||
builder.AddCustomSerilog();
|
||||
builder.Services.AddCore();
|
||||
builder.Services.AddCore(configuration);
|
||||
builder.Services.AddJwt();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddCustomSwagger(configuration, typeof(PassengerRoot).Assembly);
|
||||
|
||||
@ -1,13 +1,21 @@
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.Utils;
|
||||
using BuildingBlocks.Web;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Passenger.Extensions;
|
||||
|
||||
public static class CoreExtensions
|
||||
{
|
||||
public static IServiceCollection AddCore(this IServiceCollection services)
|
||||
public static IServiceCollection AddCore(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
configuration.GetSection(nameof(AppOptions)).Bind(nameof(AppOptions));
|
||||
|
||||
services.AddOptions<AppOptions>()
|
||||
.Bind(configuration.GetSection(nameof(AppOptions)))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
services.AddScoped<ICurrentUserProvider, CurrentUserProvider>();
|
||||
services.AddTransient<IEventMapper, EventMapper>();
|
||||
services.AddScoped<IEventDispatcher, EventDispatcher>();
|
||||
|
||||
@ -3,30 +3,43 @@ using BuildingBlocks.Contracts.EventBus.Messages;
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.Core.Event;
|
||||
using BuildingBlocks.IdsGenerator;
|
||||
using BuildingBlocks.Web;
|
||||
using Humanizer;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Passenger.Data;
|
||||
using Passenger.Passengers.Events.Domain;
|
||||
|
||||
namespace Passenger.Identity.RegisterNewUser;
|
||||
namespace Passenger.Identity.Consumers.RegisterNewUser;
|
||||
|
||||
public class RegisterNewUserConsumerHandler : IConsumer<UserCreated>
|
||||
{
|
||||
private readonly PassengerDbContext _passengerDbContext;
|
||||
private readonly IEventDispatcher _eventDispatcher;
|
||||
private readonly ILogger<RegisterNewUserConsumerHandler> _logger;
|
||||
private readonly AppOptions _options;
|
||||
|
||||
public RegisterNewUserConsumerHandler(PassengerDbContext passengerDbContext,
|
||||
IEventDispatcher eventDispatcher)
|
||||
IEventDispatcher eventDispatcher,
|
||||
ILogger<RegisterNewUserConsumerHandler> logger,
|
||||
IOptions<AppOptions> options)
|
||||
{
|
||||
_passengerDbContext = passengerDbContext;
|
||||
_eventDispatcher = eventDispatcher;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<UserCreated> context)
|
||||
{
|
||||
Guard.Against.Null(context.Message, nameof(UserCreated));
|
||||
|
||||
var passengerExist = await _passengerDbContext.Passengers.AnyAsync(x => x.PassportNumber == context.Message.PassportNumber);
|
||||
_logger.LogInformation($"consumer for {nameof(UserCreated).Underscore()} in {_options.Name}");
|
||||
|
||||
var passengerExist =
|
||||
await _passengerDbContext.Passengers.AnyAsync(x => x.PassportNumber == context.Message.PassportNumber);
|
||||
|
||||
if (passengerExist)
|
||||
return;
|
||||
@ -14,6 +14,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Data\Migrations" />
|
||||
<Folder Include="Identity\Consumers" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user