Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tstesyn committed Dec 27, 2024
1 parent bd17501 commit fd39fc6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
23 changes: 9 additions & 14 deletions Btms.Business/Services/Linking/LinkingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Btms.Model.ChangeLog;
using Btms.Model.Ipaffs;
using Btms.Model.Relationships;
using Json.Path;
using Microsoft.Extensions.Logging;

namespace Btms.Business.Services.Linking;
Expand Down Expand Up @@ -136,32 +135,28 @@ await dbContext.Notifications.Update(notification, notification._Etag, transacti

private async Task CheckMovementForRemovedLinks(MovementLinkContext linkContext,
CancellationToken cancellationToken = default)
{
var jp = JsonPath.Parse($"$.{nameof(Movement._MatchReferences)}");
var pathResult = jp.Evaluate(linkContext!.ChangeSet?.Previous);

var chedRefs = pathResult?.Matches?
.Select(m => m.Value)?
.SelectMany(x => x!.AsArray())
.Select(x => $"{x!.AsValue()}").ToList();

{
var chedRefs = linkContext.ChangeSet?.GetPreviousValue<List<string>>($"$.{nameof(Movement._MatchReferences)}");

if (chedRefs?.Count > 0)
{
var removedRefs = chedRefs.Select(x => linkContext.PersistedMovement._MatchReferences.Contains(x) == false);
var removedRefs = chedRefs.Except(linkContext.PersistedMovement._MatchReferences).ToList();
if (removedRefs.Any())
{
foreach (var chedRef in chedRefs)
{
await RemoveMovementLinkFromNotification(linkContext.PersistedMovement.Id, chedRef, cancellationToken);
await RemoveMovementLinkFromNotification(linkContext.PersistedMovement.Id, chedRef,
cancellationToken);
}
}
}
}

private async Task RemoveMovementLinkFromNotification(string? movementId, string chedRef, CancellationToken cancellationToken = default)
private async Task RemoveMovementLinkFromNotification(string? movementId, string chedRef,
CancellationToken cancellationToken = default)
{
var notification = dbContext.Notifications.SingleOrDefault(x => x._MatchReference == chedRef);

if (notification != null)
{
var relationshipLink = notification.Relationships.Movements.Data?
Expand Down
1 change: 1 addition & 0 deletions Btms.Model/Btms.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="JsonApiDotNetCore" Version="5.6.0" />
<PackageReference Include="JsonApiDotNetCore.MongoDb" Version="5.6.0" />
<PackageReference Include="JsonPath.Net" Version="2.0.0" />
<PackageReference Include="Macross.Json.Extensions" Version="3.0.0" />
<PackageReference Include="MongoDB.Bson" Version="2.30.0" />
<PackageReference Include="JsonPatch.Net" Version="3.1.1" />
Expand Down
14 changes: 14 additions & 0 deletions Btms.Model/ChangeLog/ChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.Json.Serialization;
using Btms.Model.Extensions;
using Json.Patch;
using Json.Path;

namespace Btms.Model.ChangeLog;

Expand Down Expand Up @@ -30,4 +31,17 @@ public static ChangeSet CreateChangeSet<T>(T current, T previous)

return new ChangeSet(new JsonPatch(operations), previousNode!);
}

public T? GetPreviousValue<T>(string path)
{
var jp = JsonPath.Parse($"$.{nameof(Movement._MatchReferences)}");
var pathResult = jp.Evaluate(Previous);

if (pathResult.Matches.Any())
{
return pathResult.Matches.First().Value.Deserialize<T>();
}

return default;
}
}

0 comments on commit fd39fc6

Please sign in to comment.