Skip to content

Commit

Permalink
Remove order validation and add forgotten sorting (#1362)
Browse files Browse the repository at this point in the history
* Remove order validation

* Add forgotten example sentence sorting
  • Loading branch information
myieye authored Jan 15, 2025
1 parent da83cd0 commit d0e337d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
20 changes: 4 additions & 16 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,11 @@ private IEnumerable<IChange> CreateEntryChanges(Entry entry, Dictionary<Guid, Se
sense.PartOfSpeechId = partOfSpeech.Id;
sense.PartOfSpeech = partOfSpeech.Name["en"] ?? string.Empty;
}
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 = senseOrder++;
yield return new CreateSenseChange(sense, entry.Id);
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);
}
Expand All @@ -379,8 +375,6 @@ await dataModel.AddChanges(ClientId,
..await entry.Senses.ToAsyncEnumerable()
.SelectMany((s, i) =>
{
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);
})
Expand Down Expand Up @@ -486,8 +480,6 @@ private async IAsyncEnumerable<IChange> 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);
}
Expand All @@ -500,16 +492,15 @@ private async IAsyncEnumerable<IChange> 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<Sense> 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>(sense.Id) ?? throw new NullReferenceException();
}
Expand Down Expand Up @@ -558,9 +549,6 @@ public async Task<ExampleSentence> 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>(exampleSentence.Id) ?? throw new NullReferenceException();
Expand Down
9 changes: 7 additions & 2 deletions backend/FwLite/LcmCrdt/QueryHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(this List<T> items) where T : IOrderable
public static void ApplySortOrder(this Sense sense)
{
sense.ExampleSentences.ApplySortOrder();
}

private static void ApplySortOrder<T>(this List<T> items) where T : IOrderable
{
items.Sort((x, y) =>
{
Expand Down

0 comments on commit d0e337d

Please sign in to comment.