From 98c43ed14ffad83bae0245737f7c7ed5e1e326b2 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Wed, 4 Dec 2024 12:21:25 +0700 Subject: [PATCH] Switch API order to (before, after) everywhere --- .../Api/FwDataMiniLcmApi.cs | 8 +++---- .../FwLiteProjectSync.Tests/EntrySyncTests.cs | 8 +++---- .../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 | 4 ++-- .../MiniLcm/SyncHelpers/SemanticDomainSync.cs | 4 ++-- .../FwLite/MiniLcm/SyncHelpers/SenseSync.cs | 5 ++-- .../MiniLcm/SyncHelpers/WritingSystemSync.cs | 16 ++++++------- 11 files changed, 53 insertions(+), 54 deletions(-) diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs index c5ffc2974..c2f48f72b 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs @@ -225,7 +225,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}"); } @@ -857,7 +857,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); } @@ -950,7 +950,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); } @@ -1048,7 +1048,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 b2377ae5c..a615d50fb 100644 --- a/backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs +++ b/backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs @@ -22,7 +22,7 @@ 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); - 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); @@ -53,7 +53,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(); @@ -85,7 +85,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(); @@ -99,7 +99,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/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 3955ab957..e95558082 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); } @@ -435,7 +435,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); } @@ -492,7 +492,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); } @@ -543,7 +543,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 e7ae771cf..7a5e52b50 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(api, diff --git a/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs b/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs index c8f2db5a3..54943ffea 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs @@ -6,8 +6,8 @@ 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) { Func> add = static async (api, afterEntry) => @@ -23,21 +23,21 @@ public static async Task Sync(Entry[] afterEntries, await api.DeleteEntry(beforeEntry.Id); return 1; }; - Func> replace = static async (api, beforeEntry, afterEntry) => await Sync(afterEntry, beforeEntry, api); + Func> replace = static async (api, beforeEntry, afterEntry) => await Sync(beforeEntry, afterEntry, api); return await DiffCollection.DiffAddThenUpdate(api, beforeEntries, afterEntries, entry => entry.Id, add, remove, replace); } - 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) @@ -47,8 +47,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(api, @@ -69,7 +69,7 @@ private static async Task Sync(Guid entryId, static (api, beforeComplexFormType, afterComplexFormType) => Task.FromResult(0)); } - 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(api, beforeComponents, @@ -110,8 +110,8 @@ static async (api, beforeComponent) => } private static async Task SensesSync(Guid entryId, - IList afterSenses, IList beforeSenses, + IList afterSenses, IMiniLcmApi api) { Func> add = async (api, afterSense) => @@ -124,7 +124,7 @@ private static async Task SensesSync(Guid entryId, await api.DeleteSense(entryId, beforeSense.Id); return 1; }; - Func> replace = async (api, beforeSense, afterSense) => await SenseSync.Sync(entryId, afterSense, beforeSense, api); + Func> replace = async (api, beforeSense, afterSense) => await SenseSync.Sync(entryId, beforeSense, afterSense, api); return await DiffCollection.Diff(api, beforeSenses, afterSenses, add, remove, replace); } diff --git a/backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs index ef2e6cf1a..123dda087 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) { Func> add = async (api, afterExampleSentence) => @@ -23,7 +23,7 @@ public static async Task Sync(Guid entryId, }; Func> replace = (api, beforeExampleSentence, afterExampleSentence) => - Sync(entryId, senseId, afterExampleSentence, beforeExampleSentence, api); + Sync(entryId, senseId, beforeExampleSentence, afterExampleSentence, api); return await DiffCollection.Diff(api, beforeExampleSentences, afterExampleSentences, @@ -34,8 +34,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); diff --git a/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs index 9a7cf0bba..ccc16dfcd 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs @@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers; public static class PartOfSpeechSync { - public static async Task Sync(PartOfSpeech[] currentPartsOfSpeech, - PartOfSpeech[] previousPartsOfSpeech, + public static async Task Sync(PartOfSpeech[] previousPartsOfSpeech, + PartOfSpeech[] currentPartsOfSpeech, IMiniLcmApi api) { return await DiffCollection.Diff(api, diff --git a/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs index 3892e8ed1..582386660 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs @@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers; public static class SemanticDomainSync { - public static async Task Sync(SemanticDomain[] currentSemanticDomains, - SemanticDomain[] previousSemanticDomains, + public static async Task Sync(SemanticDomain[] previousSemanticDomains, + SemanticDomain[] currentSemanticDomains, IMiniLcmApi api) { return await DiffCollection.Diff(api, diff --git a/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs index 181107105..cf4739c7a 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs @@ -6,17 +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); if (updateObjectInput is not null) await api.UpdateSense(entryId, beforeSense.Id, updateObjectInput); var changes = await ExampleSentenceSync.Sync(entryId, beforeSense.Id, - afterSense.ExampleSentences, beforeSense.ExampleSentences, - api); + afterSense.ExampleSentences, api); changes += await DiffCollection.Diff(api, beforeSense.SemanticDomains, afterSense.SemanticDomains, diff --git a/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs index 142ae97e7..34cb2124a 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs @@ -5,15 +5,15 @@ namespace MiniLcm.SyncHelpers; public static class WritingSystemSync { - public static async Task Sync(WritingSystems currentWritingSystems, - WritingSystems previousWritingSystems, + public static async Task Sync(WritingSystems previousWritingSystems, + WritingSystems currentWritingSystems, IMiniLcmApi api) { - return await Sync(currentWritingSystems.Vernacular, previousWritingSystems.Vernacular, api) + - await Sync(currentWritingSystems.Analysis, previousWritingSystems.Analysis, api); + return await Sync(previousWritingSystems.Vernacular, currentWritingSystems.Vernacular, api) + + await Sync(previousWritingSystems.Analysis, currentWritingSystems.Analysis, api); } - public static async Task Sync(WritingSystem[] currentWritingSystems, - WritingSystem[] previousWritingSystems, + public static async Task Sync(WritingSystem[] previousWritingSystems, + WritingSystem[] currentWritingSystems, IMiniLcmApi api) { return await DiffCollection.Diff(api, @@ -33,11 +33,11 @@ public static async Task Sync(WritingSystem[] currentWritingSystems, }, async (api, previousWs, currentWs) => { - return await Sync(currentWs, previousWs, api); + return await Sync(previousWs, currentWs, 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);