From 4409d47b18ce0b1632ec685aa03a90fdcd3f4c1e Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Fri, 15 Nov 2024 12:20:48 +0700 Subject: [PATCH 1/5] Implement UpdateWritingSystem(before, after) Note that FwDataMiniLcmApi hasn't implemented the UpdateWritingSystem overload with (id, type, update), which is the (before, after) method is going to end up calling. So that still needs to happen. --- .../Api/FwDataMiniLcmApi.cs | 18 +++++- .../FwLiteProjectSync/DryRunMiniLcmApi.cs | 6 ++ backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs | 6 ++ backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs | 3 +- .../MiniLcm/SyncHelpers/SimpleStringDiff.cs | 16 +++++ .../MiniLcm/SyncHelpers/WritingSystemSync.cs | 59 +++++++++++++++++++ 6 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs create mode 100644 backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs index 8e132b2e6..fcba2d259 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs @@ -132,6 +132,11 @@ private WritingSystem FromLcmWritingSystem(CoreWritingSystemDefinition ws, int i }; } + public Task GetWritingSystem(WritingSystemId id, WritingSystemType type) + { + throw new NotImplementedException(); + } + internal void CompleteExemplars(WritingSystems writingSystems) { var wsExemplars = writingSystems.Vernacular.Concat(writingSystems.Analysis) @@ -185,7 +190,18 @@ public Task CreateWritingSystem(WritingSystemType type, WritingSy public Task UpdateWritingSystem(WritingSystemId id, WritingSystemType type, UpdateObjectInput update) { - throw new NotImplementedException(); + throw new NotImplementedException(); // TODO: This needs to be implemented now, because UpdateWritingSystem(before, after) needs to call this + } + + public async Task UpdateWritingSystem(WritingSystem before, WritingSystem after) + { + await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem", + "Revert WritingSystem", + async () => + { + await WritingSystemSync.Sync(after, before, this); + }); + return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException("unable to find writing system with id " + after.Id); } public IAsyncEnumerable GetPartsOfSpeech() diff --git a/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs b/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs index 25c317670..75cf73886 100644 --- a/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs +++ b/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs @@ -34,6 +34,12 @@ public async Task UpdateWritingSystem(WritingSystemId id, }).First(w => w.WsId == id); } + public Task UpdateWritingSystem(WritingSystem before, WritingSystem after) + { + DryRunRecords.Add(new DryRunRecord(nameof(UpdateEntry), $"Update {after.Type} writing system {after.Id}")); + return Task.FromResult(after); + } + public IAsyncEnumerable GetPartsOfSpeech() { return api.GetPartsOfSpeech(); diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index 241c8daad..4468f8d16 100644 --- a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs +++ b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs @@ -56,6 +56,12 @@ public async Task UpdateWritingSystem(WritingSystemId id, Writing return await dataModel.GetLatest(ws.Id) ?? throw new NullReferenceException(); } + public async Task UpdateWritingSystem(WritingSystem before, WritingSystem after) + { + await WritingSystemSync.Sync(after, before, this); + return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException("unable to find writing system with id " + after.WsId); + } + private WritingSystem? _defaultVernacularWs; private WritingSystem? _defaultAnalysisWs; private async Task GetWritingSystem(WritingSystemId id, WritingSystemType type) diff --git a/backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs b/backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs index 4685f6d37..6608c6335 100644 --- a/backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs +++ b/backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs @@ -11,7 +11,8 @@ public interface IMiniLcmWriteApi Task UpdateWritingSystem(WritingSystemId id, WritingSystemType type, UpdateObjectInput update); - + Task UpdateWritingSystem(WritingSystem before, WritingSystem after); + // Note there's no Task DeleteWritingSystem(Guid id) because deleting writing systems needs careful consideration, as it can cause a massive cascade of data deletion #region PartOfSpeech Task CreatePartOfSpeech(PartOfSpeech partOfSpeech); diff --git a/backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs b/backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs new file mode 100644 index 000000000..2708a6b23 --- /dev/null +++ b/backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs @@ -0,0 +1,16 @@ +using SystemTextJsonPatch.Operations; + +namespace MiniLcm.SyncHelpers; + +public static class SimpleStringDiff +{ + public static IEnumerable> GetStringDiff(string path, + string before, + string after) where T : class + { + if (before == after) yield break; + if (after is null) yield return new Operation("remove", $"/{path}", null); + else if (before is null) yield return new Operation("add", $"/{path}", null); + else yield return new Operation("replace", $"/{path}", null, after); + } +} diff --git a/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs new file mode 100644 index 000000000..cb5983907 --- /dev/null +++ b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs @@ -0,0 +1,59 @@ +using MiniLcm; +using MiniLcm.Models; +using MiniLcm.SyncHelpers; +using SystemTextJsonPatch; + +public static class WritingSystemSync +{ + public static async Task Sync(WritingSystem[] currentWritingSystems, + WritingSystem[] previousWritingSystems, + IMiniLcmApi api) + { + return await DiffCollection.Diff(api, + previousWritingSystems, + currentWritingSystems, + ws => ws.Id, + async (api, currentWs) => + { + await api.CreateWritingSystem(currentWs.Type, currentWs); + return 1; + }, + async (api, previousWs) => + { + // await api.DeleteWritingSystem(previousWs.Id); // Deleting writing systems is dangerous as it causes cascading data deletion. Needs careful thought. + // TODO: should we throw an exception? + return 0; + }, + async (api, previousWs, currentWs) => + { + return await Sync(currentWs, previousWs, api); + }); + } + + public static async Task Sync(WritingSystem afterWs, WritingSystem beforeWs, 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) + { + JsonPatchDocument patchDocument = new(); + patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.WsId), + previousWritingSystem.WsId, + currentWritingSystem.WsId)); + patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.Name), + previousWritingSystem.Name, + currentWritingSystem.Name)); + patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.Abbreviation), + previousWritingSystem.Abbreviation, + currentWritingSystem.Abbreviation)); + patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.Font), + previousWritingSystem.Font, + currentWritingSystem.Font)); + // TODO: Exemplars, Order, and do we need DeletedAt? + if (patchDocument.Operations.Count == 0) return null; + return new UpdateObjectInput(patchDocument); + } +} From 12db010e123fe8639b2791cfc8d76487579b9eb0 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Mon, 18 Nov 2024 11:39:33 +0700 Subject: [PATCH 2/5] Partial implementation of UpdateWritingSystemProxy GUID Id vs string WsId is still an issue, as is the Font/Fonts issue. --- .../Api/FwDataMiniLcmApi.cs | 13 ++++- .../UpdateProxy/UpdateWritingSystemProxy.cs | 58 +++++++++++++++++++ .../FwLite/MiniLcm/Models/WritingSystem.cs | 9 +-- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs index fcba2d259..c40403ba9 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs @@ -188,9 +188,18 @@ public Task CreateWritingSystem(WritingSystemType type, WritingSy return Task.FromResult(FromLcmWritingSystem(ws, index, type)); } - public Task UpdateWritingSystem(WritingSystemId id, WritingSystemType type, UpdateObjectInput update) + public async Task UpdateWritingSystem(WritingSystemId id, WritingSystemType type, UpdateObjectInput update) { - throw new NotImplementedException(); // TODO: This needs to be implemented now, because UpdateWritingSystem(before, after) needs to call this + Cache.ServiceLocator.WritingSystemManager.GetOrSet(id.Code, out var lcmWritingSystem); + await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem", + "Revert WritingSystem", + async () => + { + var updateProxy = new UpdateWritingSystemProxy(lcmWritingSystem, this) { Type = type }; // TODO: Doesn't compile, wants Font to be set. Also wants a GUID Id which we don't have... + update.Apply(updateProxy); + updateProxy.CommitUpdate(Cache); + }); + return await GetWritingSystem(id, type); } public async Task UpdateWritingSystem(WritingSystem before, WritingSystem after) diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs new file mode 100644 index 000000000..ee67bcf43 --- /dev/null +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs @@ -0,0 +1,58 @@ +using MiniLcm.Models; +using SIL.LCModel; +using SIL.LCModel.Core.WritingSystems; +using SIL.LCModel.DomainServices; + +namespace FwDataMiniLcmBridge.Api.UpdateProxy; + +public record UpdateWritingSystemProxy : WritingSystem +{ + private readonly CoreWritingSystemDefinition _origLcmWritingSystem; + private readonly CoreWritingSystemDefinition _workingLcmWritingSystem; + private readonly FwDataMiniLcmApi _lexboxLcmApi; + + public UpdateWritingSystemProxy(CoreWritingSystemDefinition lcmWritingSystem, FwDataMiniLcmApi lexboxLcmApi) + { + _origLcmWritingSystem = lcmWritingSystem; + _workingLcmWritingSystem = new CoreWritingSystemDefinition(lcmWritingSystem, cloneId: true); + _lexboxLcmApi = lexboxLcmApi; + } + + public void CommitUpdate(LcmCache cache) + { + if (_workingLcmWritingSystem.Id == _origLcmWritingSystem.Id) + { + cache.ServiceLocator.WritingSystemManager.Set(_workingLcmWritingSystem); + } + else + { + // Changing the ID of a writing system requires LCM to do a lot of work, so only go through that process if absolutely required + WritingSystemServices.MergeWritingSystems(cache, _workingLcmWritingSystem, _origLcmWritingSystem); + } + } + + public override required WritingSystemId WsId + { + get => _workingLcmWritingSystem.Id; + set => _workingLcmWritingSystem.Id = value; + } + + public override required string Name + { + get => _workingLcmWritingSystem.LanguageName; + set { } // Silently do nothing; name should be derived from WsId at all times, so if the name should change then so should the WsId + } + + public override required string Abbreviation + { + get => _workingLcmWritingSystem.Abbreviation; + set => _workingLcmWritingSystem.Abbreviation = value; + } + + // TODO: Change WritingSystem Font property to be a list of strings instead of a single string, then do something like this + // public override required List Fonts + // { + // get => _workingLcmWritingSystem.Fonts.Select(f => f.Name).ToList(); + // set => throw new NotImplementedException(); + // } +} diff --git a/backend/FwLite/MiniLcm/Models/WritingSystem.cs b/backend/FwLite/MiniLcm/Models/WritingSystem.cs index ed96bcddc..253f2dc28 100644 --- a/backend/FwLite/MiniLcm/Models/WritingSystem.cs +++ b/backend/FwLite/MiniLcm/Models/WritingSystem.cs @@ -3,10 +3,11 @@ public record WritingSystem: IObjectWithId { public required Guid Id { get; set; } - public required WritingSystemId WsId { get; set; } - public required string Name { get; set; } - public required string Abbreviation { get; set; } - public required string Font { get; set; } + public virtual required WritingSystemId WsId { get; set; } + public virtual required string Name { get; set; } + public virtual required string Abbreviation { get; set; } + // TODO: Font will need to become Fonts, a list or array of strings, to better match the LCM model + public virtual required string Font { get; set; } public DateTimeOffset? DeletedAt { get; set; } public required WritingSystemType Type { get; set; } From e534715a7a93cb32a6a97916a180009cb7063627 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Tue, 19 Nov 2024 13:49:00 +0700 Subject: [PATCH 3/5] Finish implementing UpdateWritingSystemProxy --- .../Api/FwDataMiniLcmApi.cs | 6 ++++- .../UpdateProxy/UpdateWritingSystemProxy.cs | 23 ++++++++++++++----- .../FwLite/MiniLcm/Models/WritingSystem.cs | 1 - 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs index c40403ba9..862807ad1 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs @@ -195,7 +195,11 @@ await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem", "Revert WritingSystem", async () => { - var updateProxy = new UpdateWritingSystemProxy(lcmWritingSystem, this) { Type = type }; // TODO: Doesn't compile, wants Font to be set. Also wants a GUID Id which we don't have... + var updateProxy = new UpdateWritingSystemProxy(lcmWritingSystem, this) + { + Id = Guid.Empty, + Type = type, + }; update.Apply(updateProxy); updateProxy.CommitUpdate(Cache); }); diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs index ee67bcf43..eaf849fcb 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateWritingSystemProxy.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using MiniLcm.Models; using SIL.LCModel; using SIL.LCModel.Core.WritingSystems; using SIL.LCModel.DomainServices; +using SIL.WritingSystems; namespace FwDataMiniLcmBridge.Api.UpdateProxy; @@ -11,10 +13,14 @@ public record UpdateWritingSystemProxy : WritingSystem private readonly CoreWritingSystemDefinition _workingLcmWritingSystem; private readonly FwDataMiniLcmApi _lexboxLcmApi; + [SetsRequiredMembers] public UpdateWritingSystemProxy(CoreWritingSystemDefinition lcmWritingSystem, FwDataMiniLcmApi lexboxLcmApi) { _origLcmWritingSystem = lcmWritingSystem; _workingLcmWritingSystem = new CoreWritingSystemDefinition(lcmWritingSystem, cloneId: true); + base.Abbreviation = Abbreviation = _origLcmWritingSystem.Abbreviation ?? ""; + base.Name = Name = _origLcmWritingSystem.LanguageName ?? ""; + base.Font = Font = _origLcmWritingSystem.DefaultFontName ?? ""; _lexboxLcmApi = lexboxLcmApi; } @@ -49,10 +55,15 @@ public override required string Abbreviation set => _workingLcmWritingSystem.Abbreviation = value; } - // TODO: Change WritingSystem Font property to be a list of strings instead of a single string, then do something like this - // public override required List Fonts - // { - // get => _workingLcmWritingSystem.Fonts.Select(f => f.Name).ToList(); - // set => throw new NotImplementedException(); - // } + public override required string Font + { + get => _workingLcmWritingSystem.DefaultFontName; + set + { + if (value != _workingLcmWritingSystem.DefaultFontName) + { + _workingLcmWritingSystem.DefaultFont = new FontDefinition(value); + } + } + } } diff --git a/backend/FwLite/MiniLcm/Models/WritingSystem.cs b/backend/FwLite/MiniLcm/Models/WritingSystem.cs index 253f2dc28..e23627cd7 100644 --- a/backend/FwLite/MiniLcm/Models/WritingSystem.cs +++ b/backend/FwLite/MiniLcm/Models/WritingSystem.cs @@ -6,7 +6,6 @@ public record WritingSystem: IObjectWithId public virtual required WritingSystemId WsId { get; set; } public virtual required string Name { get; set; } public virtual required string Abbreviation { get; set; } - // TODO: Font will need to become Fonts, a list or array of strings, to better match the LCM model public virtual required string Font { get; set; } public DateTimeOffset? DeletedAt { get; set; } From b3bbc4c8fdae00e9be020d535f74cd45c7650dbe Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Mon, 25 Nov 2024 14:43:49 +0700 Subject: [PATCH 4/5] mark up string diff as allowing null strings --- backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs b/backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs index 2708a6b23..37f9e7870 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/SimpleStringDiff.cs @@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers; public static class SimpleStringDiff { public static IEnumerable> GetStringDiff(string path, - string before, - string after) where T : class + string? before, + string? after) where T : class { if (before == after) yield break; if (after is null) yield return new Operation("remove", $"/{path}", null); From 237b32f177ec790d7898107ec4887369e292b447 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Wed, 27 Nov 2024 16:23:58 +0700 Subject: [PATCH 5/5] Address review comments --- .../FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs | 7 +++++-- .../FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs | 2 +- .../FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs | 4 ++-- .../MiniLcm/SyncHelpers/SemanticDomainSync.cs | 4 ++-- .../MiniLcm/SyncHelpers/WritingSystemSync.cs | 14 ++++++++------ 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs index 862807ad1..8b3fcb8e8 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs @@ -190,7 +190,10 @@ public Task CreateWritingSystem(WritingSystemType type, WritingSy public async Task UpdateWritingSystem(WritingSystemId id, WritingSystemType type, UpdateObjectInput update) { - Cache.ServiceLocator.WritingSystemManager.GetOrSet(id.Code, out var lcmWritingSystem); + if (!Cache.ServiceLocator.WritingSystemManager.TryGet(id.Code, out var lcmWritingSystem)) + { + throw new InvalidOperationException($"Writing system {id.Code} not found"); + } await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem", "Revert WritingSystem", async () => @@ -214,7 +217,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem", { await WritingSystemSync.Sync(after, before, this); }); - return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException("unable to find writing system with id " + after.Id); + return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException($"unable to find {after.Type} writing system with id {after.WsId}"); } public IAsyncEnumerable GetPartsOfSpeech() diff --git a/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs b/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs index 75cf73886..d229b7aff 100644 --- a/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs +++ b/backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs @@ -36,7 +36,7 @@ public async Task UpdateWritingSystem(WritingSystemId id, public Task UpdateWritingSystem(WritingSystem before, WritingSystem after) { - DryRunRecords.Add(new DryRunRecord(nameof(UpdateEntry), $"Update {after.Type} writing system {after.Id}")); + DryRunRecords.Add(new DryRunRecord(nameof(UpdateEntry), $"Update {after.Type} writing system {after.WsId}")); return Task.FromResult(after); } diff --git a/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs index a37e88cc8..f5bff17e0 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs @@ -1,8 +1,8 @@ -using MiniLcm; using MiniLcm.Models; -using MiniLcm.SyncHelpers; using SystemTextJsonPatch; +namespace MiniLcm.SyncHelpers; + public static class PartOfSpeechSync { public static async Task Sync(PartOfSpeech[] currentPartsOfSpeech, diff --git a/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs index 1219db9aa..d1e150d95 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs @@ -1,8 +1,8 @@ -using MiniLcm; using MiniLcm.Models; -using MiniLcm.SyncHelpers; using SystemTextJsonPatch; +namespace MiniLcm.SyncHelpers; + public static class SemanticDomainSync { public static async Task Sync(SemanticDomain[] currentSemanticDomains, diff --git a/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs index cb5983907..1930059ce 100644 --- a/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs +++ b/backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs @@ -1,8 +1,8 @@ -using MiniLcm; using MiniLcm.Models; -using MiniLcm.SyncHelpers; using SystemTextJsonPatch; +namespace MiniLcm.SyncHelpers; + public static class WritingSystemSync { public static async Task Sync(WritingSystem[] currentWritingSystems, @@ -12,7 +12,7 @@ public static async Task Sync(WritingSystem[] currentWritingSystems, return await DiffCollection.Diff(api, previousWritingSystems, currentWritingSystems, - ws => ws.Id, + ws => (ws.WsId, ws.Type), async (api, currentWs) => { await api.CreateWritingSystem(currentWs.Type, currentWs); @@ -40,9 +40,11 @@ public static async Task Sync(WritingSystem afterWs, WritingSystem beforeWs public static UpdateObjectInput? WritingSystemDiffToUpdate(WritingSystem previousWritingSystem, WritingSystem currentWritingSystem) { JsonPatchDocument patchDocument = new(); - patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.WsId), - previousWritingSystem.WsId, - currentWritingSystem.WsId)); + if (previousWritingSystem.WsId != currentWritingSystem.WsId) + { + // TODO: Throw? Or silently ignore? + throw new InvalidOperationException($"Tried to change immutable WsId from {previousWritingSystem.WsId} to {currentWritingSystem.WsId}"); + } patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff(nameof(WritingSystem.Name), previousWritingSystem.Name, currentWritingSystem.Name));