From 883984e95d7241937ba9e06669bc6ea152dc3af4 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Fri, 1 Nov 2024 09:36:18 +0700 Subject: [PATCH] attempt to use get at commit api when updating an entry --- backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs | 31 ++++++++++--------- .../MiniLcm.Tests/UpdateEntryTestsBase.cs | 20 ++++++++++-- backend/harmony | 2 +- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index 4f3f11adc..26e23723d 100644 --- a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs +++ b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs @@ -356,21 +356,24 @@ public async Task UpdateEntry(Guid id, public async Task UpdateEntry(Entry entry) { var commitId = entry.GetVersionGuid(); - - var commitHybridDate = await dbContext.Set() - .Where(c => c.Id == commitId) - .Select(c => c.HybridDateTime) - .SingleAsyncEF(); - //todo this will not work for properties not in the snapshot, but we don't have a way to get the full snapshot yet - //todo also, add api for getting an entity at a specific commit - var snapshot = await dataModel.GetEntitySnapshotAtTime(commitHybridDate.DateTime.AddSeconds(1), entry.Id); - var before = snapshot?.Entity.DbObject as Entry; + //todo consider using GetEntry and validate the versions, this could let us update senses + var before = await dataModel.GetAtCommit(commitId, entry.Id); ArgumentNullException.ThrowIfNull(before); - //workaround to avoid syncing senses, which are not in the snapshot - before.Senses = []; - //don't want to modify what was passed in, so make a copy - entry = (Entry)entry.Copy(); - entry.Senses = []; + //workaround to sync senses, which are not in the snapshot, however this will not work for senses that have been removed + before.Senses = await entry.Senses.ToAsyncEnumerable() + .SelectAwait(async s => + { + var beforeSense = await dataModel.GetAtCommit(s.GetVersionGuid(), s.Id); + beforeSense.ExampleSentences = await s.ExampleSentences.ToAsyncEnumerable() + .SelectAwait(async es => + { + var beforeExampleSentence = await dataModel.GetAtCommit(es.GetVersionGuid(), es.Id); + return beforeExampleSentence; + }) + .ToListAsync(); + return beforeSense; + }) + .ToListAsync(); await EntrySync.Sync(entry, before, this); return await GetEntry(entry.Id) ?? throw new NullReferenceException("unable to find entry with id " + entry.Id); } diff --git a/backend/FwLite/MiniLcm.Tests/UpdateEntryTestsBase.cs b/backend/FwLite/MiniLcm.Tests/UpdateEntryTestsBase.cs index bb8ad6097..70037d836 100644 --- a/backend/FwLite/MiniLcm.Tests/UpdateEntryTestsBase.cs +++ b/backend/FwLite/MiniLcm.Tests/UpdateEntryTestsBase.cs @@ -1,4 +1,6 @@ -namespace MiniLcm.Tests; +using MiniLcm.Tests.Helpers; + +namespace MiniLcm.Tests; public abstract class UpdateEntryTestsBase : MiniLcmTestBase { @@ -73,7 +75,7 @@ public async Task UpdateEntry_Works() entry.LexemeForm["en"] = "updated"; var updatedEntry = await Api.UpdateEntry(entry); updatedEntry.LexemeForm["en"].Should().Be("updated"); - updatedEntry.Should().BeEquivalentTo(entry, options => options.Excluding(e => e.Version)); + updatedEntry.Should().BeEquivalentTo(entry, options => options.ExcludingVersion()); } [Fact] @@ -84,6 +86,18 @@ public async Task UpdateEntry_SupportsSenseChanges() entry.Senses[0].Gloss["en"] = "updated"; var updatedEntry = await Api.UpdateEntry(entry); updatedEntry.Senses[0].Gloss["en"].Should().Be("updated"); - updatedEntry.Should().BeEquivalentTo(entry, options => options.Excluding(e => e.Version)); + updatedEntry.Should().BeEquivalentTo(entry, options => options.ExcludingVersion()); + } + + [Fact] + public async Task UpdateEntry_SupportsRemovingSenseChanges() + { + var entry = await Api.GetEntry(Entry1Id); + ArgumentNullException.ThrowIfNull(entry); + var senseCount = entry.Senses.Count; + entry.Senses.RemoveAt(0); + var updatedEntry = await Api.UpdateEntry(entry); + updatedEntry.Senses.Should().HaveCount(senseCount - 1); + updatedEntry.Should().BeEquivalentTo(entry, options => options.ExcludingVersion()); } } diff --git a/backend/harmony b/backend/harmony index 8749ac516..33b1aba76 160000 --- a/backend/harmony +++ b/backend/harmony @@ -1 +1 @@ -Subproject commit 8749ac516ce09c4c36da81e32831ef2a6aefc290 +Subproject commit 33b1aba763633e8fc63f97b1d02332d1e1739c5a