From c6e0afbff632b0dc4ebbfd693d98819f1b251f47 Mon Sep 17 00:00:00 2001 From: Rik De Peuter Date: Tue, 23 Jul 2024 17:22:25 +0200 Subject: [PATCH] chore: check if address link exists before adding in projections --- .../ParcelLatestItemProjections.cs | 51 +++++------ .../ParcelVersion/ParcelVersionProjections.cs | 91 +++++++++++-------- 2 files changed, 78 insertions(+), 64 deletions(-) diff --git a/src/ParcelRegistry.Projections.Integration/ParcelLatestItem/ParcelLatestItemProjections.cs b/src/ParcelRegistry.Projections.Integration/ParcelLatestItem/ParcelLatestItemProjections.cs index 5e5fed7a..d9624d02 100644 --- a/src/ParcelRegistry.Projections.Integration/ParcelLatestItem/ParcelLatestItemProjections.cs +++ b/src/ParcelRegistry.Projections.Integration/ParcelLatestItem/ParcelLatestItemProjections.cs @@ -99,24 +99,13 @@ await context.FindAndUpdateParcel( When>(async (context, message, ct) => { - await context - .ParcelLatestItemAddresses - .AddAsync(new ParcelLatestItemAddress( - message.Message.ParcelId, - message.Message.AddressPersistentLocalId, - message.Message.CaPaKey), ct); + await AddParcelAddress(context, message.Message.ParcelId, message.Message.CaPaKey, message.Message.AddressPersistentLocalId, ct); }); When>(async (context, message, ct) => { await RemoveParcelAddress(context, message.Message.ParcelId, message.Message.PreviousAddressPersistentLocalId, ct); - - await context - .ParcelLatestItemAddresses - .AddAsync(new ParcelLatestItemAddress( - message.Message.ParcelId, - message.Message.NewAddressPersistentLocalId, - message.Message.CaPaKey), ct); + await AddParcelAddress(context, message.Message.ParcelId, message.Message.CaPaKey, message.Message.NewAddressPersistentLocalId, ct); }); When>(async (context, message, ct) => @@ -162,19 +151,7 @@ await context foreach (var addressPersistentLocalId in message.Message.AttachedAddressPersistentLocalIds) { - var relation = await context - .ParcelLatestItemAddresses - .FindAsync([message.Message.ParcelId, addressPersistentLocalId], ct); - - if (relation is null) - { - await context.ParcelLatestItemAddresses.AddAsync( - new ParcelLatestItemAddress( - message.Message.ParcelId, - addressPersistentLocalId, - message.Message.CaPaKey), - ct); - } + await AddParcelAddress(context, message.Message.ParcelId, message.Message.CaPaKey, addressPersistentLocalId, ct); } }); @@ -215,6 +192,28 @@ private static async Task RemoveParcelAddress( } } + private static async Task AddParcelAddress( + IntegrationContext context, + Guid parcelId, + string caPaKey, + int addressPersistentLocalId, + CancellationToken ct) + { + var newAddress = await context + .ParcelLatestItemAddresses + .FindAsync([parcelId, addressPersistentLocalId], cancellationToken: ct); + + if (newAddress is null || context.Entry(newAddress).State == EntityState.Deleted) + { + await context + .ParcelLatestItemAddresses + .AddAsync(new ParcelLatestItemAddress( + parcelId, + addressPersistentLocalId, + caPaKey), ct); + } + } + private static void UpdateVersionTimestamp(ParcelLatestItem parcel, Instant versionTimestamp) => parcel.VersionTimestamp = versionTimestamp; } diff --git a/src/ParcelRegistry.Projections.Integration/ParcelVersion/ParcelVersionProjections.cs b/src/ParcelRegistry.Projections.Integration/ParcelVersion/ParcelVersionProjections.cs index 08108e7b..e8b1222e 100644 --- a/src/ParcelRegistry.Projections.Integration/ParcelVersion/ParcelVersionProjections.cs +++ b/src/ParcelRegistry.Projections.Integration/ParcelVersion/ParcelVersionProjections.cs @@ -9,6 +9,7 @@ using Converters; using Infrastructure; using Legacy.Events; + using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Parcel; using Parcel.Events; @@ -114,13 +115,12 @@ await context.CreateNewParcelVersion( message, _ => { }, ct); - await context - .ParcelVersionAddresses - .AddAsync(new ParcelVersionAddress( - message.Position, - message.Message.ParcelId, - message.Message.AddressPersistentLocalId, - message.Message.CaPaKey), ct); + await AddParcelAddress(context, + message.Position, + message.Message.ParcelId, + message.Message.CaPaKey, + message.Message.AddressPersistentLocalId, + ct); }); When>(async (context, message, ct) => @@ -136,13 +136,12 @@ await RemoveParcelAddress(context, message.Message.PreviousAddressPersistentLocalId, ct); - await context - .ParcelVersionAddresses - .AddAsync(new ParcelVersionAddress( - message.Position, - message.Message.ParcelId, - message.Message.NewAddressPersistentLocalId, - message.Message.CaPaKey), ct); + await AddParcelAddress(context, + message.Position, + message.Message.ParcelId, + message.Message.CaPaKey, + message.Message.NewAddressPersistentLocalId, + ct); }); When>(async (context, message, ct) => @@ -169,7 +168,7 @@ await context.CreateNewParcelVersion( .ParcelVersionAddresses .FindAsync([message.Position, message.Message.ParcelId, message.Message.NewAddressPersistentLocalId], cancellationToken: ct); - if (newAddress is null) + if (newAddress is null || context.Entry(newAddress).State == EntityState.Deleted) { await context .ParcelVersionAddresses @@ -194,32 +193,21 @@ await context.CreateNewParcelVersion( foreach (var addressPersistentLocalId in message.Message.DetachedAddressPersistentLocalIds) { - var relation = await context - .ParcelVersionAddresses - .FindAsync([message.Position, message.Message.ParcelId, addressPersistentLocalId], ct); - - if (relation is not null) - { - context.ParcelVersionAddresses.Remove(relation); - } + await RemoveParcelAddress(context, + message.Position, + message.Message.ParcelId, + addressPersistentLocalId, + ct); } foreach (var addressPersistentLocalId in message.Message.AttachedAddressPersistentLocalIds) { - var relation = await context - .ParcelVersionAddresses - .FindAsync([message.Position, message.Message.ParcelId, addressPersistentLocalId], ct); - - if (relation is null) - { - await context.ParcelVersionAddresses.AddAsync( - new ParcelVersionAddress( - message.Position, - message.Message.ParcelId, - addressPersistentLocalId, - message.Message.CaPaKey), - ct); - } + await AddParcelAddress(context, + message.Position, + message.Message.ParcelId, + message.Message.CaPaKey, + addressPersistentLocalId, + ct); } }); @@ -455,6 +443,30 @@ await context.CreateNewParcelVersion( #endregion + private static async Task AddParcelAddress( + IntegrationContext context, + long position, + Guid parcelId, + string caPaKey, + int addressPersistentLocalId, + CancellationToken ct) + { + var newAddress = await context + .ParcelVersionAddresses + .FindAsync([position, parcelId, addressPersistentLocalId], cancellationToken: ct); + + if (newAddress is null || context.Entry(newAddress).State == EntityState.Deleted) + { + await context + .ParcelVersionAddresses + .AddAsync(new ParcelVersionAddress( + position, + parcelId, + addressPersistentLocalId, + caPaKey), ct); + } + } + private static async Task RemoveParcelAddress( IntegrationContext context, long position, @@ -467,7 +479,10 @@ private static async Task RemoveParcelAddress( .FindAsync(new object?[] { position, parcelId, addressPersistentLocalId }, cancellationToken: ct); - context.ParcelVersionAddresses.Remove(versionAddress); + if (versionAddress is not null) + { + context.ParcelVersionAddresses.Remove(versionAddress); + } } } }