From 0512a49d11edb1075a66d6d7fe6c447fcc79c73b Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Wed, 15 Jan 2025 12:53:45 +0100 Subject: [PATCH 1/2] Remove order validation --- backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index a3e6716ec..70a85ea36 100644 --- a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs +++ b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs @@ -355,15 +355,11 @@ private IEnumerable CreateEntryChanges(Entry entry, Dictionary { - if (s.Order != default) // we don't anticipate this being necessary, so we'll be strict for now - throw new InvalidOperationException("Order should not be provided when creating a sense"); s.Order = i + 1; return CreateSenseChanges(entry.Id, s); }) @@ -486,8 +480,6 @@ private async IAsyncEnumerable CreateSenseChanges(Guid entryId, Sense s var exampleOrder = 1; foreach (var exampleSentence in sense.ExampleSentences) { - if (exampleSentence.Order != default) // we don't anticipate this being necessary, so we'll be strict for now - throw new InvalidOperationException("Order should not be provided when creating an example sentence"); exampleSentence.Order = exampleOrder++; yield return new CreateExampleSentenceChange(exampleSentence, sense.Id); } @@ -505,11 +497,8 @@ private async IAsyncEnumerable CreateSenseChanges(Guid entryId, Sense s public async Task CreateSense(Guid entryId, Sense sense, BetweenPosition? between = null) { - if (sense.Order != default) // we don't anticipate this being necessary, so we'll be strict for now - throw new InvalidOperationException("Order should not be provided when creating a sense"); - - sense.Order = await OrderPicker.PickOrder(Senses.Where(s => s.EntryId == entryId), between); await validators.ValidateAndThrow(sense); + sense.Order = await OrderPicker.PickOrder(Senses.Where(s => s.EntryId == entryId), between); await dataModel.AddChanges(ClientId, await CreateSenseChanges(entryId, sense).ToArrayAsync()); return await dataModel.GetLatest(sense.Id) ?? throw new NullReferenceException(); } @@ -558,9 +547,6 @@ public async Task CreateExampleSentence(Guid entryId, BetweenPosition? between = null) { await validators.ValidateAndThrow(exampleSentence); - if (exampleSentence.Order != default) // we don't anticipate this being necessary, so we'll be strict for now - throw new InvalidOperationException("Order should not be provided when creating an example sentence"); - exampleSentence.Order = await OrderPicker.PickOrder(ExampleSentences.Where(s => s.SenseId == senseId), between); await dataModel.AddChange(ClientId, new CreateExampleSentenceChange(exampleSentence, senseId)); return await dataModel.GetLatest(exampleSentence.Id) ?? throw new NullReferenceException(); From 68b6d99a868d83d3d1bc041115b9eefc1b0c864a Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Wed, 15 Jan 2025 12:55:09 +0100 Subject: [PATCH 2/2] Add forgotten example sentence sorting --- backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs | 4 +++- backend/FwLite/LcmCrdt/QueryHelpers.cs | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index 70a85ea36..69fbf734a 100644 --- a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs +++ b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs @@ -492,7 +492,9 @@ private async IAsyncEnumerable CreateSenseChanges(Guid entryId, Sense s .ThenLoad(s => s.ExampleSentences) .AsQueryable() .SingleOrDefaultAsync(e => e.Id == entryId); - return entry?.Senses.FirstOrDefault(s => s.Id == id); + var sense = entry?.Senses.FirstOrDefault(s => s.Id == id); + sense?.ApplySortOrder(); + return sense; } public async Task CreateSense(Guid entryId, Sense sense, BetweenPosition? between = null) diff --git a/backend/FwLite/LcmCrdt/QueryHelpers.cs b/backend/FwLite/LcmCrdt/QueryHelpers.cs index ae07b18d6..3e76c9a3e 100644 --- a/backend/FwLite/LcmCrdt/QueryHelpers.cs +++ b/backend/FwLite/LcmCrdt/QueryHelpers.cs @@ -7,11 +7,16 @@ public static void ApplySortOrder(this Entry entry) entry.Senses.ApplySortOrder(); foreach (var sense in entry.Senses) { - sense.ExampleSentences.ApplySortOrder(); + sense.ApplySortOrder(); } } - public static void ApplySortOrder(this List items) where T : IOrderable + public static void ApplySortOrder(this Sense sense) + { + sense.ExampleSentences.ApplySortOrder(); + } + + private static void ApplySortOrder(this List items) where T : IOrderable { items.Sort((x, y) => {