Skip to content

Commit

Permalink
handle deleted semantic domains and part of speech in CreateSenseChange
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Jan 14, 2025
1 parent 46135af commit e515f4a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions backend/FwLite/LcmCrdt/Changes/CreateSenseChange.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using LcmCrdt.Utils;
using SIL.Harmony;
using SIL.Harmony.Changes;
using SIL.Harmony.Entities;
Expand Down Expand Up @@ -40,8 +41,8 @@ public override async ValueTask<Sense> NewEntity(Commit commit, ChangeContext co
Order = Order,
Definition = Definition ?? new MultiString(),
Gloss = Gloss ?? new MultiString(),
PartOfSpeechId = PartOfSpeechId,
SemanticDomains = SemanticDomains ?? [],
PartOfSpeechId = await context.DeletedAsNull(PartOfSpeechId),
SemanticDomains = await context.FilterDeleted(SemanticDomains ?? []).ToArrayAsync(),
DeletedAt = await context.IsObjectDeleted(EntryId) ? commit.DateTime : (DateTime?)null
};
}
Expand Down
27 changes: 27 additions & 0 deletions backend/FwLite/LcmCrdt/Utils/ChangeContextHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SIL.Harmony.Changes;

namespace LcmCrdt.Utils;

public static class ChangeContextHelpers
{
public static async ValueTask<bool> IsObjectDeleted(this ChangeContext context, Guid? entityId)
{
return entityId is null || await context.IsObjectDeleted(entityId.Value);
}

public static async ValueTask<Guid?> DeletedAsNull(this ChangeContext context, Guid? entityId)
{
if (entityId is null) return null;
return await context.IsObjectDeleted(entityId.Value) ? null : entityId;
}

public static async IAsyncEnumerable<T> FilterDeleted<T>(this ChangeContext context, IEnumerable<T> entities)
where T : IObjectWithId
{
foreach (var objectWithId in entities)
{
if (await context.IsObjectDeleted(objectWithId.Id)) continue;
yield return objectWithId;
}
}
}

0 comments on commit e515f4a

Please sign in to comment.