Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rabbit events #348

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Application/Application.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Cfo.Cats.Application</RootNamespace>
Expand All @@ -11,6 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Humanizer.Core.uk" Version="2.14.1" />
<PackageReference Include="MassTransit.Abstractions" Version="8.3.3" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Ardalis.Specification" Version="8.0.0" />
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="8.0.0" />
Expand Down
9 changes: 9 additions & 0 deletions src/Application/Common/IntegrationEvents/IntegrationEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace Cfo.Cats.Application.Common.IntegrationEvents;

public abstract record IntegrationEvent
{
[JsonInclude] public Guid Id { get; private init; } = Guid.CreateVersion7();
[JsonInclude] public DateTime CreationDate { get; private init; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Cfo.Cats.Application.Features.Participants.IntegrationEvents;
using MassTransit;

namespace Cfo.Cats.Application.Features.Participants.Consumers;

public class LogParticipantTransitionConsumer(ILogger<LogParticipantTransitionConsumer> logger) : IConsumer<ParticipantTransitionedIntegrationEvent>
{
public Task Consume(ConsumeContext<ParticipantTransitionedIntegrationEvent> context)
{
var (participantId, from, to) = context.Message;
logger.LogInformation($"Participant {participantId} transitioned from {from} to {to}");
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Cfo.Cats.Application.Features.Participants.IntegrationEvents;
using Cfo.Cats.Domain.Events;
using MassTransit;

namespace Cfo.Cats.Application.Features.Participants.EventHandlers;

public class PublishEnrolmentApproval(IBus bus) : INotificationHandler<ParticipantTransitionedDomainEvent>
{
public async Task Handle(ParticipantTransitionedDomainEvent notification, CancellationToken cancellationToken)
{
var e = new ParticipantTransitionedIntegrationEvent(
notification.Item.Id,
notification.From.ToString(),
notification.To.ToString()
);
await bus.Publish(e, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Cfo.Cats.Application.Common.IntegrationEvents;

namespace Cfo.Cats.Application.Features.Participants.IntegrationEvents;

public record ParticipantTransitionedIntegrationEvent(string ParticipantId, string From, string To) : IntegrationEvent;
8 changes: 4 additions & 4 deletions src/Aspire/Aspire.AppHost/Aspire.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.SqlServer" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.SqlServer" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Server.Ui\Server.Ui.csproj"></ProjectReference>
<ProjectReference Include="..\..\Server.Ui\Server.Ui.csproj"></ProjectReference>
</ItemGroup>

</Project>
25 changes: 15 additions & 10 deletions src/Aspire/Aspire.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
var builder = DistributedApplication.CreateBuilder(args);

var password = builder.AddParameter("password", secret: true);
var sqlPassword = builder.AddParameter("sqlPassword", secret: true);
var rabbitUser = builder.AddParameter("rabbitUser", secret: true);
var rabbitPassword = builder.AddParameter("rabbitPassword", secret: true);

var sqlServer = builder.AddSqlServer("sql", password, 1433)
var db = builder.AddSqlServer("sql", sqlPassword, 1433)
.WithDataVolume("cats-aspire-data")
.WithLifetime(ContainerLifetime.Persistent);


var database = sqlServer.AddDatabase("CatsDb");
.WithLifetime(ContainerLifetime.Persistent)
.AddDatabase("CatsDb");

var rabbit = builder.AddRabbitMQ("rabbit",
userName: rabbitUser,
password: rabbitPassword)
.WithManagementPlugin()
.WithLifetime(ContainerLifetime.Persistent);

builder.AddProject<Projects.Server_Ui>("cats")
.WithReference(database)
.WaitFor(database);

.WithReference(db)
.WithReference(rabbit)
.WaitFor(db);

builder.Build().Run();
builder.Build().Run();
6 changes: 4 additions & 2 deletions src/Aspire/Aspire.AppHost/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
}
},
"Parameters": {
"password": "P@ssword123!"
"sqlPassword": "P@ssword123!",
"rabbitUser": "guest",
"rabbitPassword": "guest"
}
}
}
21 changes: 21 additions & 0 deletions src/Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Cfo.Cats.Application.Common.Interfaces.Locations;
using Cfo.Cats.Application.Common.Interfaces.MultiTenant;
using Cfo.Cats.Application.Common.Interfaces.Serialization;
using Cfo.Cats.Application.Features.Participants.Consumers;
using Cfo.Cats.Application.Features.Participants.Queries;
using Cfo.Cats.Application.SecurityConstants;
using Cfo.Cats.Domain.Identity;
Expand All @@ -15,6 +16,7 @@
using Cfo.Cats.Infrastructure.Services.Locations;
using Cfo.Cats.Infrastructure.Services.MultiTenant;
using Cfo.Cats.Infrastructure.Services.Serialization;
using MassTransit;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand Down Expand Up @@ -70,6 +72,25 @@ IConfiguration configuration
.AddSingleton<IRightToWorkSettings>(s =>
s.GetRequiredService<RightToWorkSettings>()
);

services.AddMassTransit(x =>
{
x.AddConsumer<LogParticipantTransitionConsumer>();


x.AddEntityFrameworkOutbox<ApplicationDbContext>(o =>
{
o.UseSqlServer(true);
o.UseBusOutbox();
});

x.UsingRabbitMq((context, cfg) =>
{
cfg.Host(configuration.GetConnectionString("rabbit"));
cfg.ConfigureEndpoints(context);
});
});

return services;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<PackageReference Include="AWSSDK.S3" Version="3.7.410.1" />
<PackageReference Include="GovukNotify" Version="7.2.0" />
<PackageReference Include="IPAddressRange" Version="6.1.0" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.3.3" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.3.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
Expand Down
16 changes: 16 additions & 0 deletions src/Infrastructure/Persistence/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using Cfo.Cats.Domain.Entities.Inductions;
using Cfo.Cats.Domain.Entities.Notifications;
using Cfo.Cats.Domain.Entities.Payables;
using MassTransit;
using Microsoft.EntityFrameworkCore;
using MassTransit.EntityFrameworkCoreIntegration;

namespace Cfo.Cats.Infrastructure.Persistence;

Expand Down Expand Up @@ -92,6 +95,19 @@ protected override void OnModelCreating(ModelBuilder builder)


builder.ApplyGlobalFilters<ISoftDelete>(s => s.Deleted == null);

builder.AddInboxStateEntity();
builder.AddOutboxMessageEntity();
builder.AddOutboxStateEntity();

builder.Entity<InboxState>()
.ToTable(nameof(InboxState), nameof(MassTransit));

builder.Entity<OutboxMessage>()
.ToTable(nameof(OutboxMessage), nameof(MassTransit));

builder.Entity<OutboxState>()
.ToTable(nameof(OutboxState), nameof(MassTransit));
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Expand Down
Loading
Loading