Skip to content

Commit

Permalink
attempt to use get at commit api when updating an entry
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Nov 1, 2024
1 parent df91f0e commit 883984e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
31 changes: 17 additions & 14 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,24 @@ public async Task<Entry> UpdateEntry(Guid id,
public async Task<Entry> UpdateEntry(Entry entry)
{
var commitId = entry.GetVersionGuid();

var commitHybridDate = await dbContext.Set<Commit>()
.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<Entry>(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<Sense>(s.GetVersionGuid(), s.Id);
beforeSense.ExampleSentences = await s.ExampleSentences.ToAsyncEnumerable()
.SelectAwait(async es =>
{
var beforeExampleSentence = await dataModel.GetAtCommit<ExampleSentence>(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);
}
Expand Down
20 changes: 17 additions & 3 deletions backend/FwLite/MiniLcm.Tests/UpdateEntryTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace MiniLcm.Tests;
using MiniLcm.Tests.Helpers;

namespace MiniLcm.Tests;

public abstract class UpdateEntryTestsBase : MiniLcmTestBase
{
Expand Down Expand Up @@ -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]
Expand All @@ -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());
}
}

0 comments on commit 883984e

Please sign in to comment.