Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Update(before, after) API for example sentences #1266

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,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 @@ -905,6 +911,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 @@ -27,7 +27,7 @@
DryRunRecords.Add(new DryRunRecord(nameof(UpdateWritingSystem),
$"Update writing system {type}, changes: {update.Summarize()}"));
var ws = await api.GetWritingSystems();
return (type switch

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FwHeadless / publish-fw-headless

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FW Lite and run tests

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Linux

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Linux

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Windows

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Windows

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.
{
WritingSystemType.Vernacular => ws.Vernacular,
WritingSystemType.Analysis => ws.Analysis
Expand Down Expand Up @@ -182,6 +182,11 @@
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 @@ -195,11 +200,17 @@
{
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 @@ -13,7 +13,7 @@

namespace LcmCrdt;

public class CrdtMiniLcmApi(DataModel dataModel, CurrentProjectService projectService, LcmCrdtDbContext dbContext) : IMiniLcmApi

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FwHeadless / publish-fw-headless

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FwHeadless / publish-fw-headless

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FW Lite and run tests

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Linux

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Linux

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Windows

Parameter 'dbContext' is unread.

Check warning on line 16 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Windows

Parameter 'dbContext' is unread.
{
private Guid ClientId { get; } = projectService.ProjectData.ClientId;
public ProjectData ProjectData => projectService.ProjectData;
Expand Down Expand Up @@ -305,7 +305,7 @@
]);
return await GetEntry(entry.Id) ?? throw new NullReferenceException();

async IAsyncEnumerable<AddEntryComponentChange> ToComplexFormComponents(IList<ComplexFormComponent> complexFormComponents)

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FwHeadless / publish-fw-headless

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FW Lite and run tests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Linux

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Linux

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Windows

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 308 in backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Windows

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
foreach (var complexFormComponent in complexFormComponents)
{
Expand Down Expand Up @@ -442,6 +442,14 @@
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 @@ -453,6 +461,15 @@
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 @@ -53,6 +53,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);
}
}
Loading