From d38737af38ba02188758a235ffbbb98c882f3963 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Wed, 18 Dec 2024 17:49:24 -1000 Subject: [PATCH] Make sync API consistent: always (before, after) (#1303) * Switch API order to (before, after) everywhere * Update names: previous => before, current => after --- .../Api/FwDataMiniLcmApi.cs | 8 ++-- .../FwLiteProjectSync.Tests/EntrySyncTests.cs | 14 +++--- .../UpdateDiffTests.cs | 6 +-- .../CrdtFwdataProjectSyncService.cs | 20 ++++----- backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs | 8 ++-- .../SyncHelpers/ComplexFormTypeSync.cs | 4 +- .../FwLite/MiniLcm/SyncHelpers/EntrySync.cs | 24 +++++----- .../SyncHelpers/ExampleSentenceSync.cs | 6 +-- .../MiniLcm/SyncHelpers/PartOfSpeechSync.cs | 26 +++++------ .../MiniLcm/SyncHelpers/SemanticDomainSync.cs | 26 +++++------ .../FwLite/MiniLcm/SyncHelpers/SenseSync.cs | 8 ++-- .../MiniLcm/SyncHelpers/WritingSystemSync.cs | 44 +++++++++---------- 12 files changed, 97 insertions(+), 97 deletions(-) diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs index 0a60d09a9..80eebd829 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs @@ -224,7 +224,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem", "Revert WritingSystem", async () => { - await WritingSystemSync.Sync(after, before, this); + await WritingSystemSync.Sync(before, after, this); }); return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException($"unable to find {after.Type} writing system with id {after.WsId}"); } @@ -856,7 +856,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Entry", "Revert entry", async () => { - await EntrySync.Sync(after, before, this); + await EntrySync.Sync(before, after, this); }); return await GetEntry(after.Id) ?? throw new NullReferenceException("unable to find entry with id " + after.Id); } @@ -990,7 +990,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Sense", "Revert Sense", async () => { - await SenseSync.Sync(entryId, after, before, this); + await SenseSync.Sync(entryId, before, after, this); }); return await GetSense(entryId, after.Id) ?? throw new NullReferenceException("unable to find sense with id " + after.Id); } @@ -1106,7 +1106,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Example Sentence", "Revert Example Sentence", async () => { - await ExampleSentenceSync.Sync(entryId, senseId, after, before, this); + await ExampleSentenceSync.Sync(entryId, senseId, before, after, this); }); return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException("unable to find example sentence with id " + after.Id); } diff --git a/backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs b/backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs index 83dfa17a3..fcdfe555d 100644 --- a/backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs +++ b/backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs @@ -33,13 +33,13 @@ public async Task CanSyncRandomEntries() var createdEntry = await _fixture.CrdtApi.CreateEntry(await AutoFaker.EntryReadyForCreation(_fixture.CrdtApi)); var after = await AutoFaker.EntryReadyForCreation(_fixture.CrdtApi, entryId: createdEntry.Id); - after.Senses = AutoFaker.Faker.Random.Shuffle([ + after.Senses = [.. AutoFaker.Faker.Random.Shuffle([ // copy some senses over, so moves happen ..AutoFaker.Faker.Random.ListItems(createdEntry.Senses), - ..(after.Senses) - ]).ToList(); + ..after.Senses + ])]; - await EntrySync.Sync(after, createdEntry, _fixture.CrdtApi); + await EntrySync.Sync(createdEntry, after, _fixture.CrdtApi); var actual = await _fixture.CrdtApi.GetEntry(after.Id); actual.Should().NotBeNull(); actual.Should().BeEquivalentTo(after, options => options @@ -71,7 +71,7 @@ public async Task CanChangeComplexFormVisSync_Components() after.Components[0].ComponentEntryId = component2.Id; after.Components[0].ComponentHeadword = component2.Headword(); - await EntrySync.Sync(after, complexForm, _fixture.CrdtApi); + await EntrySync.Sync(complexForm, after, _fixture.CrdtApi); var actual = await _fixture.CrdtApi.GetEntry(after.Id); actual.Should().NotBeNull(); @@ -103,7 +103,7 @@ public async Task CanChangeComplexFormViaSync_ComplexForms() after.ComplexForms[0].ComplexFormEntryId = complexForm2.Id; after.ComplexForms[0].ComplexFormHeadword = complexForm2.Headword(); - await EntrySync.Sync(after, component, _fixture.CrdtApi); + await EntrySync.Sync(component, after, _fixture.CrdtApi); var actual = await _fixture.CrdtApi.GetEntry(after.Id); actual.Should().NotBeNull(); @@ -117,7 +117,7 @@ public async Task CanChangeComplexFormTypeViaSync() var entry = await _fixture.CrdtApi.CreateEntry(new() { LexemeForm = { { "en", "complexForm1" } } }); var after = (Entry) entry.Copy(); after.ComplexFormTypes = [complexFormType]; - await EntrySync.Sync(after, entry, _fixture.CrdtApi); + await EntrySync.Sync(entry, after, _fixture.CrdtApi); var actual = await _fixture.CrdtApi.GetEntry(after.Id); actual.Should().NotBeNull(); diff --git a/backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs b/backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs index 1f3523981..6c3663531 100644 --- a/backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs +++ b/backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs @@ -27,7 +27,7 @@ public void EntryDiffShouldUpdateAllFields() var entryDiffToUpdate = EntrySync.EntryDiffToUpdate(before, after); ArgumentNullException.ThrowIfNull(entryDiffToUpdate); entryDiffToUpdate.Apply(before); - before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id) + before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id) .Excluding(x => x.DeletedAt).Excluding(x => x.Senses) .Excluding(x => x.Components) .Excluding(x => x.ComplexForms) @@ -35,11 +35,11 @@ public void EntryDiffShouldUpdateAllFields() } [Fact] - public async Task SenseDiffShouldUpdateAllFields() + public void SenseDiffShouldUpdateAllFields() { var before = new Sense(); var after = AutoFaker.Generate(); - var senseDiffToUpdate = await SenseSync.SenseDiffToUpdate(before, after); + var senseDiffToUpdate = SenseSync.SenseDiffToUpdate(before, after); ArgumentNullException.ThrowIfNull(senseDiffToUpdate); senseDiffToUpdate.Apply(before); before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id).Excluding(x => x.EntryId).Excluding(x => x.DeletedAt).Excluding(x => x.ExampleSentences).Excluding(x => x.SemanticDomains)); diff --git a/backend/FwLite/FwLiteProjectSync/CrdtFwdataProjectSyncService.cs b/backend/FwLite/FwLiteProjectSync/CrdtFwdataProjectSyncService.cs index f167e2444..073ee374f 100644 --- a/backend/FwLite/FwLiteProjectSync/CrdtFwdataProjectSyncService.cs +++ b/backend/FwLite/FwLiteProjectSync/CrdtFwdataProjectSyncService.cs @@ -65,26 +65,26 @@ private async Task Sync(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi, } var currentFwDataWritingSystems = await fwdataApi.GetWritingSystems(); - var crdtChanges = await WritingSystemSync.Sync(currentFwDataWritingSystems, projectSnapshot.WritingSystems, crdtApi); - var fwdataChanges = await WritingSystemSync.Sync(await crdtApi.GetWritingSystems(), currentFwDataWritingSystems, fwdataApi); + var crdtChanges = await WritingSystemSync.Sync(projectSnapshot.WritingSystems, currentFwDataWritingSystems, crdtApi); + var fwdataChanges = await WritingSystemSync.Sync(currentFwDataWritingSystems, await crdtApi.GetWritingSystems(), fwdataApi); var currentFwDataPartsOfSpeech = await fwdataApi.GetPartsOfSpeech().ToArrayAsync(); - crdtChanges += await PartOfSpeechSync.Sync(currentFwDataPartsOfSpeech, projectSnapshot.PartsOfSpeech, crdtApi); - fwdataChanges += await PartOfSpeechSync.Sync(await crdtApi.GetPartsOfSpeech().ToArrayAsync(), currentFwDataPartsOfSpeech, fwdataApi); + crdtChanges += await PartOfSpeechSync.Sync(projectSnapshot.PartsOfSpeech, currentFwDataPartsOfSpeech, crdtApi); + fwdataChanges += await PartOfSpeechSync.Sync(currentFwDataPartsOfSpeech, await crdtApi.GetPartsOfSpeech().ToArrayAsync(), fwdataApi); var currentFwDataSemanticDomains = await fwdataApi.GetSemanticDomains().ToArrayAsync(); - crdtChanges += await SemanticDomainSync.Sync(currentFwDataSemanticDomains, projectSnapshot.SemanticDomains, crdtApi); - fwdataChanges += await SemanticDomainSync.Sync(await crdtApi.GetSemanticDomains().ToArrayAsync(), currentFwDataSemanticDomains, fwdataApi); + crdtChanges += await SemanticDomainSync.Sync(projectSnapshot.SemanticDomains, currentFwDataSemanticDomains, crdtApi); + fwdataChanges += await SemanticDomainSync.Sync(currentFwDataSemanticDomains, await crdtApi.GetSemanticDomains().ToArrayAsync(), fwdataApi); var currentFwDataComplexFormTypes = await fwdataApi.GetComplexFormTypes().ToArrayAsync(); - crdtChanges += await ComplexFormTypeSync.Sync(currentFwDataComplexFormTypes, projectSnapshot.ComplexFormTypes, crdtApi); - fwdataChanges += await ComplexFormTypeSync.Sync(await crdtApi.GetComplexFormTypes().ToArrayAsync(), currentFwDataComplexFormTypes, fwdataApi); + crdtChanges += await ComplexFormTypeSync.Sync(projectSnapshot.ComplexFormTypes, currentFwDataComplexFormTypes, crdtApi); + fwdataChanges += await ComplexFormTypeSync.Sync(currentFwDataComplexFormTypes, await crdtApi.GetComplexFormTypes().ToArrayAsync(), fwdataApi); var currentFwDataEntries = await fwdataApi.GetAllEntries().ToArrayAsync(); - crdtChanges += await EntrySync.Sync(currentFwDataEntries, projectSnapshot.Entries, crdtApi); + crdtChanges += await EntrySync.Sync(projectSnapshot.Entries, currentFwDataEntries, crdtApi); LogDryRun(crdtApi, "crdt"); - fwdataChanges += await EntrySync.Sync(await crdtApi.GetAllEntries().ToArrayAsync(), currentFwDataEntries, fwdataApi); + fwdataChanges += await EntrySync.Sync(currentFwDataEntries, await crdtApi.GetAllEntries().ToArrayAsync(), fwdataApi); LogDryRun(fwdataApi, "fwdata"); //todo push crdt changes to lexbox diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index c13168e3c..860fb677e 100644 --- a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs +++ b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs @@ -69,7 +69,7 @@ public async Task UpdateWritingSystem(WritingSystemId id, Writing public async Task UpdateWritingSystem(WritingSystem before, WritingSystem after) { - await WritingSystemSync.Sync(after, before, this); + await WritingSystemSync.Sync(before, after, this); return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException("unable to find writing system with id " + after.WsId); } @@ -448,7 +448,7 @@ public async Task UpdateEntry(Guid id, public async Task UpdateEntry(Entry before, Entry after) { - await EntrySync.Sync(after, before, this); + await EntrySync.Sync(before, after, this); return await GetEntry(after.Id) ?? throw new NullReferenceException("unable to find entry with id " + after.Id); } @@ -509,7 +509,7 @@ public async Task UpdateSense(Guid entryId, public async Task UpdateSense(Guid entryId, Sense before, Sense after) { - await SenseSync.Sync(entryId, after, before, this); + await SenseSync.Sync(entryId, before, after, this); return await GetSense(entryId, after.Id) ?? throw new NullReferenceException("unable to find sense with id " + after.Id); } @@ -566,7 +566,7 @@ public async Task UpdateExampleSentence(Guid entryId, ExampleSentence before, ExampleSentence after) { - await ExampleSentenceSync.Sync(entryId, senseId, after, before, this); + await ExampleSentenceSync.Sync(entryId, senseId, before, after, this); return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException(); } diff --git a/backend/FwLite/MiniLcm/SyncHelpers/ComplexFormTypeSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/ComplexFormTypeSync.cs index 05a92442f..93078f2c6 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/ComplexFormTypeSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/ComplexFormTypeSync.cs @@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers; public static class ComplexFormTypeSync { - public static async Task Sync(ComplexFormType[] afterComplexFormTypes, - ComplexFormType[] beforeComplexFormTypes, + public static async Task Sync(ComplexFormType[] beforeComplexFormTypes, + ComplexFormType[] afterComplexFormTypes, IMiniLcmApi api) { return await DiffCollection.Diff( diff --git a/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs b/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs index 15dd6463a..ffef37afa 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs @@ -6,24 +6,24 @@ namespace MiniLcm.SyncHelpers; public static class EntrySync { - public static async Task Sync(Entry[] afterEntries, - Entry[] beforeEntries, + public static async Task Sync(Entry[] beforeEntries, + Entry[] afterEntries, IMiniLcmApi api) { return await DiffCollection.DiffAddThenUpdate(beforeEntries, afterEntries, new EntriesDiffApi(api)); } - public static async Task Sync(Entry afterEntry, Entry beforeEntry, IMiniLcmApi api) + public static async Task Sync(Entry beforeEntry, Entry afterEntry, IMiniLcmApi api) { try { var updateObjectInput = EntryDiffToUpdate(beforeEntry, afterEntry); if (updateObjectInput is not null) await api.UpdateEntry(afterEntry.Id, updateObjectInput); - var changes = await SensesSync(afterEntry.Id, afterEntry.Senses, beforeEntry.Senses, api); + var changes = await SensesSync(afterEntry.Id, beforeEntry.Senses, afterEntry.Senses, api); - changes += await Sync(afterEntry.Components, beforeEntry.Components, api); - changes += await Sync(afterEntry.ComplexForms, beforeEntry.ComplexForms, api); - changes += await Sync(afterEntry.Id, afterEntry.ComplexFormTypes, beforeEntry.ComplexFormTypes, api); + changes += await Sync(beforeEntry.Components, afterEntry.Components, api); + changes += await Sync(beforeEntry.ComplexForms, afterEntry.ComplexForms, api); + changes += await Sync(afterEntry.Id, beforeEntry.ComplexFormTypes, afterEntry.ComplexFormTypes, api); return changes + (updateObjectInput is null ? 0 : 1); } catch (Exception e) @@ -33,8 +33,8 @@ public static async Task Sync(Entry afterEntry, Entry beforeEntry, IMiniLcm } private static async Task Sync(Guid entryId, - IList afterComplexFormTypes, IList beforeComplexFormTypes, + IList afterComplexFormTypes, IMiniLcmApi api) { return await DiffCollection.Diff( @@ -43,7 +43,7 @@ private static async Task Sync(Guid entryId, new ComplexFormTypesDiffApi(api, entryId)); } - private static async Task Sync(IList afterComponents, IList beforeComponents, IMiniLcmApi api) + private static async Task Sync(IList beforeComponents, IList afterComponents, IMiniLcmApi api) { return await DiffCollection.Diff( beforeComponents, @@ -53,8 +53,8 @@ private static async Task Sync(IList afterComponents, } private static async Task SensesSync(Guid entryId, - IList afterSenses, IList beforeSenses, + IList afterSenses, IMiniLcmApi api) { return await DiffCollection.DiffOrderable(beforeSenses, afterSenses, new SensesDiffApi(api, entryId)); @@ -96,7 +96,7 @@ public override async Task Remove(Entry entry) public override Task Replace(Entry before, Entry after) { - return Sync(after, before, api); + return Sync(before, after, api); } } @@ -184,7 +184,7 @@ public async Task Remove(Sense sense) public Task Replace(Sense before, Sense after) { - return SenseSync.Sync(entryId, after, before, api); + return SenseSync.Sync(entryId, before, after, api); } } } diff --git a/backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs index f685e9650..c2cdf2d2e 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs @@ -7,8 +7,8 @@ public static class ExampleSentenceSync { public static async Task Sync(Guid entryId, Guid senseId, - IList afterExampleSentences, IList beforeExampleSentences, + IList afterExampleSentences, IMiniLcmApi api) { return await DiffCollection.Diff( @@ -19,8 +19,8 @@ public static async Task Sync(Guid entryId, public static async Task Sync(Guid entryId, Guid senseId, - ExampleSentence afterExampleSentence, ExampleSentence beforeExampleSentence, + ExampleSentence afterExampleSentence, IMiniLcmApi api) { var updateObjectInput = DiffToUpdate(beforeExampleSentence, afterExampleSentence); @@ -66,7 +66,7 @@ public override async Task Remove(ExampleSentence beforeExampleSentence) public override Task Replace(ExampleSentence beforeExampleSentence, ExampleSentence afterExampleSentence) { - return Sync(entryId, senseId, afterExampleSentence, beforeExampleSentence, api); + return Sync(entryId, senseId, beforeExampleSentence, afterExampleSentence, api); } } } diff --git a/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs index be6eff5a1..539d8b38e 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs @@ -5,13 +5,13 @@ namespace MiniLcm.SyncHelpers; public static class PartOfSpeechSync { - public static async Task Sync(PartOfSpeech[] currentPartsOfSpeech, - PartOfSpeech[] previousPartsOfSpeech, + public static async Task Sync(PartOfSpeech[] beforePartsOfSpeech, + PartOfSpeech[] afterPartsOfSpeech, IMiniLcmApi api) { return await DiffCollection.Diff( - previousPartsOfSpeech, - currentPartsOfSpeech, + beforePartsOfSpeech, + afterPartsOfSpeech, new PartsOfSpeechDiffApi(api)); } @@ -24,16 +24,16 @@ public static async Task Sync(PartOfSpeech before, return updateObjectInput is null ? 0 : 1; } - public static UpdateObjectInput? PartOfSpeechDiffToUpdate(PartOfSpeech previousPartOfSpeech, PartOfSpeech currentPartOfSpeech) + public static UpdateObjectInput? PartOfSpeechDiffToUpdate(PartOfSpeech beforePartOfSpeech, PartOfSpeech afterPartOfSpeech) { JsonPatchDocument patchDocument = new(); patchDocument.Operations.AddRange(MultiStringDiff.GetMultiStringDiff(nameof(PartOfSpeech.Name), - previousPartOfSpeech.Name, - currentPartOfSpeech.Name)); + beforePartOfSpeech.Name, + afterPartOfSpeech.Name)); // TODO: Once we add abbreviations to MiniLcm's PartOfSpeech objects, then: // patchDocument.Operations.AddRange(GetMultiStringDiff(nameof(PartOfSpeech.Abbreviation), - // previousPartOfSpeech.Abbreviation, - // currentPartOfSpeech.Abbreviation)); + // beforePartOfSpeech.Abbreviation, + // afterPartOfSpeech.Abbreviation)); if (patchDocument.Operations.Count == 0) return null; return new UpdateObjectInput(patchDocument); } @@ -46,15 +46,15 @@ public override async Task Add(PartOfSpeech currentPos) return 1; } - public override async Task Remove(PartOfSpeech previousPos) + public override async Task Remove(PartOfSpeech beforePos) { - await api.DeletePartOfSpeech(previousPos.Id); + await api.DeletePartOfSpeech(beforePos.Id); return 1; } - public override Task Replace(PartOfSpeech previousPos, PartOfSpeech currentPos) + public override Task Replace(PartOfSpeech beforePos, PartOfSpeech afterPos) { - return Sync(previousPos, currentPos, api); + return Sync(beforePos, afterPos, api); } } } diff --git a/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs index b6bc793fc..c2fcfcaf9 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs @@ -5,13 +5,13 @@ namespace MiniLcm.SyncHelpers; public static class SemanticDomainSync { - public static async Task Sync(SemanticDomain[] currentSemanticDomains, - SemanticDomain[] previousSemanticDomains, + public static async Task Sync(SemanticDomain[] beforeSemanticDomains, + SemanticDomain[] afterSemanticDomains, IMiniLcmApi api) { return await DiffCollection.Diff( - previousSemanticDomains, - currentSemanticDomains, + beforeSemanticDomains, + afterSemanticDomains, new SemanticDomainsDiffApi(api)); } @@ -24,16 +24,16 @@ public static async Task Sync(SemanticDomain before, return updateObjectInput is null ? 0 : 1; } - public static UpdateObjectInput? SemanticDomainDiffToUpdate(SemanticDomain previousSemanticDomain, SemanticDomain currentSemanticDomain) + public static UpdateObjectInput? SemanticDomainDiffToUpdate(SemanticDomain beforeSemanticDomain, SemanticDomain afterSemanticDomain) { JsonPatchDocument patchDocument = new(); patchDocument.Operations.AddRange(MultiStringDiff.GetMultiStringDiff(nameof(SemanticDomain.Name), - previousSemanticDomain.Name, - currentSemanticDomain.Name)); + beforeSemanticDomain.Name, + afterSemanticDomain.Name)); // TODO: Once we add abbreviations to MiniLcm's SemanticDomain objects, then: // patchDocument.Operations.AddRange(GetMultiStringDiff(nameof(SemanticDomain.Abbreviation), - // previousSemanticDomain.Abbreviation, - // currentSemanticDomain.Abbreviation)); + // beforeSemanticDomain.Abbreviation, + // afterSemanticDomain.Abbreviation)); if (patchDocument.Operations.Count == 0) return null; return new UpdateObjectInput(patchDocument); } @@ -46,15 +46,15 @@ public override async Task Add(SemanticDomain currentSemDom) return 1; } - public override async Task Remove(SemanticDomain previousSemDom) + public override async Task Remove(SemanticDomain beforeSemDom) { - await api.DeleteSemanticDomain(previousSemDom.Id); + await api.DeleteSemanticDomain(beforeSemDom.Id); return 1; } - public override Task Replace(SemanticDomain previousSemDom, SemanticDomain currentSemDom) + public override Task Replace(SemanticDomain beforeSemDom, SemanticDomain afterSemDom) { - return Sync(previousSemDom, currentSemDom, api); + return Sync(beforeSemDom, afterSemDom, api); } } } diff --git a/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs index 6c028cd56..32619f2e0 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs @@ -6,16 +6,16 @@ namespace MiniLcm.SyncHelpers; public static class SenseSync { public static async Task Sync(Guid entryId, - Sense afterSense, Sense beforeSense, + Sense afterSense, IMiniLcmApi api) { - var updateObjectInput = await SenseDiffToUpdate(beforeSense, afterSense); + var updateObjectInput = SenseDiffToUpdate(beforeSense, afterSense); if (updateObjectInput is not null) await api.UpdateSense(entryId, beforeSense.Id, updateObjectInput); var changes = await ExampleSentenceSync.Sync(entryId, beforeSense.Id, - afterSense.ExampleSentences, beforeSense.ExampleSentences, + afterSense.ExampleSentences, api); changes += await DiffCollection.Diff( beforeSense.SemanticDomains, @@ -24,7 +24,7 @@ public static async Task Sync(Guid entryId, return changes + (updateObjectInput is null ? 0 : 1); } - public static async Task?> SenseDiffToUpdate(Sense beforeSense, Sense afterSense) + public static UpdateObjectInput? SenseDiffToUpdate(Sense beforeSense, Sense afterSense) { JsonPatchDocument patchDocument = new(); patchDocument.Operations.AddRange( diff --git a/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs index 548edd0e9..0a24e3429 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs @@ -5,47 +5,47 @@ namespace MiniLcm.SyncHelpers; public static class WritingSystemSync { - public static async Task Sync(WritingSystems currentWritingSystems, - WritingSystems previousWritingSystems, + public static async Task Sync(WritingSystems beforeWritingSystems, + WritingSystems afterWritingSystems, IMiniLcmApi api) { - return await Sync(currentWritingSystems.Vernacular, previousWritingSystems.Vernacular, api) + - await Sync(currentWritingSystems.Analysis, previousWritingSystems.Analysis, api); + return await Sync(beforeWritingSystems.Vernacular, afterWritingSystems.Vernacular, api) + + await Sync(beforeWritingSystems.Analysis, afterWritingSystems.Analysis, api); } - public static async Task Sync(WritingSystem[] currentWritingSystems, - WritingSystem[] previousWritingSystems, + public static async Task Sync(WritingSystem[] beforeWritingSystems, + WritingSystem[] afterWritingSystems, IMiniLcmApi api) { return await DiffCollection.Diff( - previousWritingSystems, - currentWritingSystems, + beforeWritingSystems, + afterWritingSystems, new WritingSystemsDiffApi(api)); } - public static async Task Sync(WritingSystem afterWs, WritingSystem beforeWs, IMiniLcmApi api) + public static async Task Sync(WritingSystem beforeWs, WritingSystem afterWs, IMiniLcmApi api) { var updateObjectInput = WritingSystemDiffToUpdate(beforeWs, afterWs); if (updateObjectInput is not null) await api.UpdateWritingSystem(afterWs.WsId, afterWs.Type, updateObjectInput); return updateObjectInput is null ? 0 : 1; } - public static UpdateObjectInput? WritingSystemDiffToUpdate(WritingSystem previousWritingSystem, WritingSystem currentWritingSystem) + public static UpdateObjectInput? WritingSystemDiffToUpdate(WritingSystem beforeWritingSystem, WritingSystem afterWritingSystem) { JsonPatchDocument patchDocument = new(); - if (previousWritingSystem.WsId != currentWritingSystem.WsId) + if (beforeWritingSystem.WsId != afterWritingSystem.WsId) { // TODO: Throw? Or silently ignore? - throw new InvalidOperationException($"Tried to change immutable WsId from {previousWritingSystem.WsId} to {currentWritingSystem.WsId}"); + throw new InvalidOperationException($"Tried to change immutable WsId from {beforeWritingSystem.WsId} to {afterWritingSystem.WsId}"); } patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.Name), - previousWritingSystem.Name, - currentWritingSystem.Name)); + beforeWritingSystem.Name, + afterWritingSystem.Name)); patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.Abbreviation), - previousWritingSystem.Abbreviation, - currentWritingSystem.Abbreviation)); + beforeWritingSystem.Abbreviation, + afterWritingSystem.Abbreviation)); patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.Font), - previousWritingSystem.Font, - currentWritingSystem.Font)); + beforeWritingSystem.Font, + afterWritingSystem.Font)); // TODO: Exemplars, Order, and do we need DeletedAt? if (patchDocument.Operations.Count == 0) return null; return new UpdateObjectInput(patchDocument); @@ -64,16 +64,16 @@ public override async Task Add(WritingSystem currentWs) return 1; } - public override Task Remove(WritingSystem beforeDomain) + public override Task Remove(WritingSystem beforeWs) { - // await api.DeleteWritingSystem(previousWs.Id); // Deleting writing systems is dangerous as it causes cascading data deletion. Needs careful thought. + // await api.DeleteWritingSystem(beforeWs.Id); // Deleting writing systems is dangerous as it causes cascading data deletion. Needs careful thought. // TODO: should we throw an exception? return Task.FromResult(0); } - public override Task Replace(WritingSystem previousWs, WritingSystem currentWs) + public override Task Replace(WritingSystem beforeWs, WritingSystem afterWs) { - return Sync(currentWs, previousWs, api); + return Sync(beforeWs, afterWs, api); } } }