diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index a3e6716ec..69fbf734a 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); } @@ -500,16 +492,15 @@ 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) { - 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 +549,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(); 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) => {