Skip to content

Commit

Permalink
Add Update(before, after) API for senses
Browse files Browse the repository at this point in the history
  • Loading branch information
rmunn committed Dec 4, 2024
1 parent effc25b commit 62b7cc8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,12 @@ private void ApplySenseToLexSense(Sense sense, ILexSense lexSense)
}
}

public Task<Sense?> GetSense(Guid entryId, Guid id)
{
var lcmSense = SenseRepository.GetObject(id);
return Task.FromResult(lcmSense is null ? null : FromLexSense(lcmSense));
}

public Task<Sense> CreateSense(Guid entryId, Sense sense)
{
if (sense.Id == default) sense.Id = Guid.NewGuid();
Expand Down Expand Up @@ -938,6 +944,17 @@ public Task<Sense> UpdateSense(Guid entryId, Guid senseId, UpdateObjectInput<Sen
return Task.FromResult(FromLexSense(lexSense));
}

public async Task<Sense> UpdateSense(Guid entryId, Sense before, Sense after)
{
await Cache.DoUsingNewOrCurrentUOW("Update Sense",
"Revert Sense",
async () =>
{
await SenseSync.Sync(entryId, after, before, this);
});
return await GetSense(entryId, after.Id) ?? throw new NullReferenceException("unable to find sense with id " + after.Id);
}

public Task AddSemanticDomainToSense(Guid senseId, SemanticDomain semanticDomain)
{
UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW("Add Semantic Domain to Sense",
Expand Down
12 changes: 12 additions & 0 deletions backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ public async Task RemoveComplexFormType(Guid entryId, Guid complexFormTypeId)
await Task.CompletedTask;
}

public Task<Sense?> GetSense(Guid entryId, Guid id)
{
return api.GetSense(entryId, id);
}

public Task<Sense> CreateSense(Guid entryId, Sense sense)
{
DryRunRecords.Add(new DryRunRecord(nameof(CreateSense), $"Create sense {sense.Gloss}"));
Expand All @@ -206,6 +211,13 @@ public async Task<Sense> UpdateSense(Guid entryId, Guid senseId, UpdateObjectInp
return sense;
}

public async Task<Sense> UpdateSense(Guid entryId, Sense before, Sense after)
{
DryRunRecords.Add(new DryRunRecord(nameof(UpdateSense),
$"Update sense {after.Id}"));
return await GetSense(entryId, after.Id) ?? throw new NullReferenceException($"unable to find sense with id {after.Id}");
}

public Task DeleteSense(Guid entryId, Guid senseId)
{
DryRunRecords.Add(new DryRunRecord(nameof(DeleteSense), $"Delete sense {senseId}"));
Expand Down
16 changes: 16 additions & 0 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,16 @@ private async IAsyncEnumerable<IChange> CreateSenseChanges(Guid entryId, Sense s
}
}

public async Task<Sense?> GetSense(Guid entryId, Guid id)
{
var entry = await Entries.AsTracking(false)
.LoadWith(e => e.Senses)
.ThenLoad(s => s.ExampleSentences)
.AsQueryable()
.SingleOrDefaultAsync(e => e.Id == entryId);
return entry?.Senses.FirstOrDefault(s => s.Id == id);
}

public async Task<Sense> CreateSense(Guid entryId, Sense sense)
{
await dataModel.AddChanges(ClientId, await CreateSenseChanges(entryId, sense).ToArrayAsync());
Expand All @@ -480,6 +490,12 @@ public async Task<Sense> UpdateSense(Guid entryId,
return await dataModel.GetLatest<Sense>(senseId) ?? throw new NullReferenceException();
}

public async Task<Sense> UpdateSense(Guid entryId, Sense before, Sense after)
{
await SenseSync.Sync(entryId, after, before, this);
return await GetSense(entryId, after.Id) ?? throw new NullReferenceException("unable to find sense with id " + after.Id);
}

public async Task DeleteSense(Guid entryId, Guid senseId)
{
await dataModel.AddChange(ClientId, new DeleteChange<Sense>(senseId));
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 @@ -13,6 +13,7 @@ public interface IMiniLcmReadApi
IAsyncEnumerable<Entry> GetEntries(QueryOptions? options = null);
IAsyncEnumerable<Entry> SearchEntries(string query, QueryOptions? options = null);
Task<Entry?> GetEntry(Guid id);
Task<Sense?> GetSense(Guid entryId, Guid id);
Task<PartOfSpeech?> GetPartOfSpeech(Guid id);
Task<SemanticDomain?> GetSemanticDomain(Guid id);
Task<ExampleSentence?> GetExampleSentence(Guid entryId, Guid senseId, Guid id);
Expand Down
1 change: 1 addition & 0 deletions backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Task<WritingSystem> UpdateWritingSystem(WritingSystemId id,
#region Sense
Task<Sense> CreateSense(Guid entryId, Sense sense);
Task<Sense> UpdateSense(Guid entryId, Guid senseId, UpdateObjectInput<Sense> update);
Task<Sense> UpdateSense(Guid entryId, Sense before, Sense after);
Task DeleteSense(Guid entryId, Guid senseId);
Task AddSemanticDomainToSense(Guid senseId, SemanticDomain semanticDomain);
Task RemoveSemanticDomainFromSense(Guid senseId, Guid semanticDomainId);
Expand Down
9 changes: 9 additions & 0 deletions backend/LfClassicData/LfClassicMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@ private static SemanticDomain ToSemanticDomain(Entities.OptionListItem item)
return ToEntry(entry);
}

public async Task<Sense?> GetSense(Guid entryId, 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 == id);
if (sense is null) return null;
return ToSense(entryId, sense);
}

public async Task<ExampleSentence?> GetExampleSentence(Guid entryId, Guid senseId, Guid id)
{
var entry = await Entries.Find(e => e.Guid == entryId).FirstOrDefaultAsync();
Expand Down

0 comments on commit 62b7cc8

Please sign in to comment.