Skip to content

Commit

Permalink
Add Update(before, after) API for possibilities (#1269)
Browse files Browse the repository at this point in the history
* Parts of speech:

Adds a new UpdatePartOfSpeech(PartOfSpeech before, PartOfSpeech after)
method to the IMiniLcmWriteApi interface.

No need to add a GetPartOfSpeech(Guid id) method to the IMiniLcmReadApi
interface, as it was already there.

* Semantic domains:

Adds a new UpdateSemanticDomain(SemanticDomain before, SemanticDomain
after) method to the IMiniLcmWriteApi interface.

No need to add a GetSemanticDomain(Guid id) method to the
IMiniLcmReadApi interface, as it was already there.
  • Loading branch information
rmunn authored Nov 28, 2024
1 parent 6476ba6 commit 9ed13d9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
12 changes: 12 additions & 0 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ public Task<PartOfSpeech> UpdatePartOfSpeech(Guid id, UpdateObjectInput<PartOfSp
return Task.FromResult(FromLcmPartOfSpeech(lcmPartOfSpeech));
}

public async Task<PartOfSpeech> UpdatePartOfSpeech(PartOfSpeech before, PartOfSpeech after)
{
await PartOfSpeechSync.Sync(before, after, this);
return await GetPartOfSpeech(after.Id) ?? throw new NullReferenceException($"unable to find part of speech with id {after.Id}");
}

public Task DeletePartOfSpeech(Guid id)
{
UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW("Delete Part of Speech",
Expand Down Expand Up @@ -341,6 +347,12 @@ public Task<SemanticDomain> UpdateSemanticDomain(Guid id, UpdateObjectInput<Sema
return Task.FromResult(FromLcmSemanticDomain(lcmSemanticDomain));
}

public async Task<SemanticDomain> UpdateSemanticDomain(SemanticDomain before, SemanticDomain after)
{
await SemanticDomainSync.Sync(before, after, this);
return await GetSemanticDomain(after.Id) ?? throw new NullReferenceException($"unable to find semantic domain with id {after.Id}");
}

public Task DeleteSemanticDomain(Guid id)
{
UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW("Delete Semantic Domain",
Expand Down
16 changes: 14 additions & 2 deletions backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public Task<PartOfSpeech> UpdatePartOfSpeech(Guid id, UpdateObjectInput<PartOfSp
return GetPartOfSpeech(id)!;
}

public Task<PartOfSpeech> UpdatePartOfSpeech(PartOfSpeech before, PartOfSpeech after)
{
DryRunRecords.Add(new DryRunRecord(nameof(UpdatePartOfSpeech), $"Update part of speech {after.Id}"));
return Task.FromResult(after);
}

public Task DeletePartOfSpeech(Guid id)
{
DryRunRecords.Add(new DryRunRecord(nameof(DeletePartOfSpeech), $"Delete part of speech {id}"));
Expand All @@ -87,13 +93,19 @@ public Task<SemanticDomain> CreateSemanticDomain(SemanticDomain semanticDomain)

public Task<SemanticDomain> UpdateSemanticDomain(Guid id, UpdateObjectInput<SemanticDomain> update)
{
DryRunRecords.Add(new DryRunRecord(nameof(UpdateSemanticDomain), $"Update part of speech {id}"));
DryRunRecords.Add(new DryRunRecord(nameof(UpdateSemanticDomain), $"Update semantic domain {id}"));
return GetSemanticDomain(id)!;
}

public Task<SemanticDomain> UpdateSemanticDomain(SemanticDomain before, SemanticDomain after)
{
DryRunRecords.Add(new DryRunRecord(nameof(UpdateSemanticDomain), $"Update semantic domain {after.Id}"));
return Task.FromResult(after);
}

public Task DeleteSemanticDomain(Guid id)
{
DryRunRecords.Add(new DryRunRecord(nameof(DeleteSemanticDomain), $"Delete part of speech {id}"));
DryRunRecords.Add(new DryRunRecord(nameof(DeleteSemanticDomain), $"Delete semantic domain {id}"));
return Task.CompletedTask;
}

Expand Down
12 changes: 12 additions & 0 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public async Task<PartOfSpeech> UpdatePartOfSpeech(Guid id, UpdateObjectInput<Pa
return await GetPartOfSpeech(id) ?? throw new NullReferenceException();
}

public async Task<PartOfSpeech> UpdatePartOfSpeech(PartOfSpeech before, PartOfSpeech after)
{
await PartOfSpeechSync.Sync(before, after, this);
return await GetPartOfSpeech(after.Id) ?? throw new NullReferenceException($"unable to find part of speech with id {after.Id}");
}

public async Task DeletePartOfSpeech(Guid id)
{
await dataModel.AddChange(ClientId, new DeleteChange<PartOfSpeech>(id));
Expand Down Expand Up @@ -134,6 +140,12 @@ public async Task<SemanticDomain> UpdateSemanticDomain(Guid id, UpdateObjectInpu
return await GetSemanticDomain(id) ?? throw new NullReferenceException();
}

public async Task<SemanticDomain> UpdateSemanticDomain(SemanticDomain before, SemanticDomain after)
{
await SemanticDomainSync.Sync(before, after, this);
return await GetSemanticDomain(after.Id) ?? throw new NullReferenceException($"unable to find semantic domain with id {after.Id}");
}

public async Task DeleteSemanticDomain(Guid id)
{
await dataModel.AddChange(ClientId, new DeleteChange<SemanticDomain>(id));
Expand Down
2 changes: 2 additions & 0 deletions backend/FwLite/MiniLcm/IMiniLcmWriteApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ Task<WritingSystem> UpdateWritingSystem(WritingSystemId id,
#region PartOfSpeech
Task<PartOfSpeech> CreatePartOfSpeech(PartOfSpeech partOfSpeech);
Task<PartOfSpeech> UpdatePartOfSpeech(Guid id, UpdateObjectInput<PartOfSpeech> update);
Task<PartOfSpeech> UpdatePartOfSpeech(PartOfSpeech before, PartOfSpeech after);
Task DeletePartOfSpeech(Guid id);
#endregion

#region SemanticDomain
Task<SemanticDomain> CreateSemanticDomain(SemanticDomain semanticDomain);
Task<SemanticDomain> UpdateSemanticDomain(Guid id, UpdateObjectInput<SemanticDomain> update);
Task<SemanticDomain> UpdateSemanticDomain(SemanticDomain before, SemanticDomain after);
Task DeleteSemanticDomain(Guid id);
#endregion

Expand Down
16 changes: 10 additions & 6 deletions backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ public static async Task<int> Sync(PartOfSpeech[] currentPartsOfSpeech,
await api.DeletePartOfSpeech(previousPos.Id);
return 1;
},
async (api, previousPos, currentPos) =>
{
var updateObjectInput = PartOfSpeechDiffToUpdate(previousPos, currentPos);
if (updateObjectInput is not null) await api.UpdatePartOfSpeech(currentPos.Id, updateObjectInput);
return updateObjectInput is null ? 0 : 1;
});
(api, previousPos, currentPos) => Sync(previousPos, currentPos, api));
}

public static async Task<int> Sync(PartOfSpeech before,
PartOfSpeech after,
IMiniLcmApi api)
{
var updateObjectInput = PartOfSpeechDiffToUpdate(before, after);
if (updateObjectInput is not null) await api.UpdatePartOfSpeech(after.Id, updateObjectInput);
return updateObjectInput is null ? 0 : 1;
}

public static UpdateObjectInput<PartOfSpeech>? PartOfSpeechDiffToUpdate(PartOfSpeech previousPartOfSpeech, PartOfSpeech currentPartOfSpeech)
Expand Down
16 changes: 10 additions & 6 deletions backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ public static async Task<int> Sync(SemanticDomain[] currentSemanticDomains,
await api.DeleteSemanticDomain(previousPos.Id);
return 1;
},
async (api, previousPos, currentPos) =>
{
var updateObjectInput = SemanticDomainDiffToUpdate(previousPos, currentPos);
if (updateObjectInput is not null) await api.UpdateSemanticDomain(currentPos.Id, updateObjectInput);
return updateObjectInput is null ? 0 : 1;
});
(api, previousSemdom, currentSemdom) => Sync(previousSemdom, currentSemdom, api));
}

public static async Task<int> Sync(SemanticDomain before,
SemanticDomain after,
IMiniLcmApi api)
{
var updateObjectInput = SemanticDomainDiffToUpdate(before, after);
if (updateObjectInput is not null) await api.UpdateSemanticDomain(after.Id, updateObjectInput);
return updateObjectInput is null ? 0 : 1;
}

public static UpdateObjectInput<SemanticDomain>? SemanticDomainDiffToUpdate(SemanticDomain previousSemanticDomain, SemanticDomain currentSemanticDomain)
Expand Down

0 comments on commit 9ed13d9

Please sign in to comment.