Skip to content

Commit

Permalink
fix(integration): add more efficient method retrievingpersistentlocalid
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Jun 27, 2024
1 parent 1b0a75e commit c42cd8d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public IntegrationModule(
{
var logger = loggerFactory.CreateLogger<IntegrationModule>();
services.AddScoped<IPersistentLocalIdFinder, PersistentLocalIdFinder>(_ =>
new PersistentLocalIdFinder(configuration.GetConnectionString("Events")));
new PersistentLocalIdFinder(configuration.GetConnectionString("Events"), configuration.GetConnectionString("LegacyProjections")));
var connectionString = configuration.GetConnectionString("IntegrationProjections");

services.AddScoped<IAddresses, Addresses>(_ => new Addresses(connectionString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ public interface IPersistentLocalIdFinder
public class PersistentLocalIdFinder : IPersistentLocalIdFinder
{
private readonly string _eventsConnectionString;
private readonly string _legacyProjectionsConnectionString;

public PersistentLocalIdFinder(string eventsConnectionString)
public PersistentLocalIdFinder(string eventsConnectionString, string legacyProjectionsConnectionString)
{
_eventsConnectionString = eventsConnectionString;
_legacyProjectionsConnectionString = legacyProjectionsConnectionString;
}

public async Task<int?> FindBuildingPersistentLocalId(Guid buildingId)
{
await using var projectionsConnection = new SqlConnection(_legacyProjectionsConnectionString);

var sqlProjections = @"
SELECT TOP 1 PersistentLocalId
FROM [building-registry].[BuildingRegistryLegacy].[BuildingSyndicationWithCount]
WHERE BuildingId = @BuildingId AND PersistentLocalId IS NOT NULL";

var buildingPersistentLocalIdByProjection = await projectionsConnection.QuerySingleOrDefaultAsync<int?>(
sqlProjections, new { BuildingId = buildingId });

if(buildingPersistentLocalIdByProjection.HasValue)
return buildingPersistentLocalIdByProjection;

await using var connection = new SqlConnection(_eventsConnectionString);

var sql = @"
Expand All @@ -40,6 +55,21 @@ INNER JOIN [building-registry-events].[BuildingRegistry].[Messages] as m

public async Task<int?> FindBuildingUnitPersistentLocalId(Guid buildingId, Guid buildingUnitId)
{
await using var projectionsConnection = new SqlConnection(_legacyProjectionsConnectionString);

var sqlProjections = @"
SELECT TOP 1 bu.PersistentLocalId
FROM [building-registry].[BuildingRegistryLegacy].[BuildingUnitSyndicationWithCount] bu
INNER JOIN [building-registry].[BuildingRegistryLegacy].[BuildingSyndicationWithCount] b
on b.Position = bu.Position and b.BuildingId = @BuildingId
WHERE bu.BuildingUnitId = @BuildingUnitId AND bu.PersistentLocalId IS NOT NULL";

var buildingUnitPersistentLocalIdByProjection = await projectionsConnection.QuerySingleOrDefaultAsync<int?>(
sqlProjections, new { BuildingId = buildingId, BuildingUnitId = buildingUnitId });

if(buildingUnitPersistentLocalIdByProjection.HasValue)
return buildingUnitPersistentLocalIdByProjection;

await using var connection = new SqlConnection(_eventsConnectionString);

var sql = @"
Expand Down

0 comments on commit c42cd8d

Please sign in to comment.