Skip to content

Commit

Permalink
Add Update(before, after) API for example sentences (#1266)
Browse files Browse the repository at this point in the history
Adds a new UpdateExampleSentence(Guid entryId, Guid senseId,
ExampleSentence before, ExampleSentence after) method to the
IMiniLcmWriteApi interface.

Also adds a GetExampleSentence(Guid entryId, Guid senseId, Guid id)
method to the IMiniLcmReadApi interface.
  • Loading branch information
rmunn authored Nov 28, 2024
1 parent 7f157e7 commit 6476ba6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 12 deletions.
20 changes: 20 additions & 0 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,12 @@ public Task DeleteSense(Guid entryId, Guid senseId)
return Task.CompletedTask;
}

public Task<ExampleSentence?> GetExampleSentence(Guid entryId, Guid senseId, Guid id)
{
var lcmExampleSentence = ExampleSentenceRepository.GetObject(id);
return Task.FromResult(lcmExampleSentence is null ? null : FromLexExampleSentence(senseId, lcmExampleSentence));
}

internal void CreateExampleSentence(ILexSense lexSense, ExampleSentence exampleSentence)
{
var lexExampleSentence = LexExampleSentenceFactory.Create(exampleSentence.Id, lexSense);
Expand Down Expand Up @@ -955,6 +961,20 @@ public Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
return Task.FromResult(FromLexExampleSentence(senseId, lexExampleSentence));
}

public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
Guid senseId,
ExampleSentence before,
ExampleSentence after)
{
await Cache.DoUsingNewOrCurrentUOW("Update Example Sentence",
"Revert Example Sentence",
async () =>
{
await ExampleSentenceSync.Sync(entryId, senseId, after, before, this);
});
return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException("unable to find example sentence with id " + after.Id);
}

public Task DeleteExampleSentence(Guid entryId, Guid senseId, Guid exampleSentenceId)
{
var lexExampleSentence = ExampleSentenceRepository.GetObject(exampleSentenceId);
Expand Down
21 changes: 16 additions & 5 deletions backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ public Task RemoveSemanticDomainFromSense(Guid senseId, Guid semanticDomainId)
return Task.CompletedTask;
}

public Task<ExampleSentence?> GetExampleSentence(Guid entryId, Guid senseId, Guid id)
{
return api.GetExampleSentence(entryId, senseId, id);
}

public Task<ExampleSentence> CreateExampleSentence(Guid entryId, Guid senseId, ExampleSentence exampleSentence)
{
DryRunRecords.Add(new DryRunRecord(nameof(CreateExampleSentence), $"Create example sentence {exampleSentence.Sentence}"));
Expand All @@ -201,11 +206,17 @@ public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
{
DryRunRecords.Add(new DryRunRecord(nameof(UpdateExampleSentence),
$"Update example sentence {exampleSentenceId}, changes: {update.Summarize()}"));
var entry = await GetEntry(entryId) ??
throw new NullReferenceException($"unable to find entry with id {entryId}");
var sense = entry.Senses.First(s => s.Id == senseId);
var exampleSentence = sense.ExampleSentences.First(s => s.Id == exampleSentenceId);
return exampleSentence;
var exampleSentence = await GetExampleSentence(entryId, senseId, exampleSentenceId);
return exampleSentence ?? throw new NullReferenceException($"unable to find example sentence with id {exampleSentenceId}");
}

public Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
Guid senseId,
ExampleSentence before,
ExampleSentence after)
{
DryRunRecords.Add(new DryRunRecord(nameof(UpdateExampleSentence), $"Update example sentence {after.Id}"));
return Task.FromResult(after);
}

public Task DeleteExampleSentence(Guid entryId, Guid senseId, Guid exampleSentenceId)
Expand Down
17 changes: 17 additions & 0 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,14 @@ public async Task<ExampleSentence> CreateExampleSentence(Guid entryId,
return await dataModel.GetLatest<ExampleSentence>(exampleSentence.Id) ?? throw new NullReferenceException();
}

public async Task<ExampleSentence?> GetExampleSentence(Guid entryId, Guid senseId, Guid id)
{
var exampleSentence = await ExampleSentences.AsTracking(false)
.AsQueryable()
.SingleOrDefaultAsync(e => e.Id == id);
return exampleSentence;
}

public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
Guid senseId,
Guid exampleSentenceId,
Expand All @@ -470,6 +478,15 @@ public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
return await dataModel.GetLatest<ExampleSentence>(exampleSentenceId) ?? throw new NullReferenceException();
}

public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
Guid senseId,
ExampleSentence before,
ExampleSentence after)
{
await ExampleSentenceSync.Sync(entryId, senseId, after, before, this);
return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException();
}

public async Task DeleteExampleSentence(Guid entryId, Guid senseId, Guid exampleSentenceId)
{
await dataModel.AddChange(ClientId, new DeleteChange<ExampleSentence>(exampleSentenceId));
Expand Down
1 change: 1 addition & 0 deletions backend/FwLite/MiniLcm/IMiniLcmReadApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IMiniLcmReadApi
Task<Entry?> GetEntry(Guid id);
Task<PartOfSpeech?> GetPartOfSpeech(Guid id);
Task<SemanticDomain?> GetSemanticDomain(Guid id);
Task<ExampleSentence?> GetExampleSentence(Guid entryId, Guid senseId, Guid id);
}

public record QueryOptions(
Expand Down
4 changes: 4 additions & 0 deletions backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
Guid senseId,
Guid exampleSentenceId,
UpdateObjectInput<ExampleSentence> update);
Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
Guid senseId,
ExampleSentence before,
ExampleSentence after);

Task DeleteExampleSentence(Guid entryId, Guid senseId, Guid exampleSentenceId);
#endregion
Expand Down
21 changes: 14 additions & 7 deletions backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ public static async Task<int> Sync(Guid entryId,
return 1;
};
Func<IMiniLcmApi, ExampleSentence, ExampleSentence, Task<int>> replace =
async (api, beforeExampleSentence, afterExampleSentence) =>
{
var updateObjectInput = DiffToUpdate(beforeExampleSentence, afterExampleSentence);
if (updateObjectInput is null) return 0;
await api.UpdateExampleSentence(entryId, senseId, beforeExampleSentence.Id, updateObjectInput);
return 1;
};
(api, beforeExampleSentence, afterExampleSentence) =>
Sync(entryId, senseId, afterExampleSentence, beforeExampleSentence, api);
return await DiffCollection.Diff(api,
beforeExampleSentences,
afterExampleSentences,
Expand All @@ -37,6 +32,18 @@ public static async Task<int> Sync(Guid entryId,
replace);
}

public static async Task<int> Sync(Guid entryId,
Guid senseId,
ExampleSentence afterExampleSentence,
ExampleSentence beforeExampleSentence,
IMiniLcmApi api)
{
var updateObjectInput = DiffToUpdate(beforeExampleSentence, afterExampleSentence);
if (updateObjectInput is null) return 0;
await api.UpdateExampleSentence(entryId, senseId, beforeExampleSentence.Id, updateObjectInput);
return 1;
}

public static UpdateObjectInput<ExampleSentence>? DiffToUpdate(ExampleSentence beforeExampleSentence,
ExampleSentence afterExampleSentence)
{
Expand Down
11 changes: 11 additions & 0 deletions backend/LfClassicData/LfClassicMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,15 @@ private static SemanticDomain ToSemanticDomain(Entities.OptionListItem item)
if (entry is null) return null;
return ToEntry(entry);
}

public async Task<ExampleSentence?> GetExampleSentence(Guid entryId, Guid senseId, Guid id)
{
var entry = await Entries.Find(e => e.Guid == entryId).FirstOrDefaultAsync();
if (entry is null) return null;
var sense = entry.Senses?.FirstOrDefault(s => s?.Guid == senseId);
if (sense is null) return null;
var exampleSentence = sense.Examples?.FirstOrDefault(e => e?.Guid == id);
if (exampleSentence is null) return null;
return ToExampleSentence(sense.Guid, exampleSentence);
}
}

0 comments on commit 6476ba6

Please sign in to comment.