Skip to content

Commit

Permalink
make GetObjectBySnapshotId generic
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Oct 15, 2024
1 parent 7d81ade commit 7ac1cd4
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/SIL.Harmony.Tests/DefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task CanAddADefinitionToAWord()
await WriteNextChange(NewDefinition(wordId, "a greeting", "verb"));
var snapshot = await DataModel.GetProjectSnapshot();
var definitionSnapshot = snapshot.Snapshots.Values.Single(s => s.IsType<Definition>());
var definition = (Definition)await DataModel.GetBySnapshotId(definitionSnapshot.Id);
var definition = await DataModel.GetBySnapshotId<Definition>(definitionSnapshot.Id);
definition.Text.Should().Be("a greeting");
definition.WordId.Should().Be(wordId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.Harmony.Tests/ExampleSentenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task CanAddAnExampleSentenceToAWord()
await WriteNextChange(NewExampleSentence(definitionId, "Hello, world!"));
var snapshot = await DataModel.GetProjectSnapshot();
var exampleSentenceSnapshot = snapshot.Snapshots.Values.Single(s => s.IsType<Example>());
var exampleSentence = (Example)await DataModel.GetBySnapshotId(exampleSentenceSnapshot.Id);
var exampleSentence = await DataModel.GetBySnapshotId<Example>(exampleSentenceSnapshot.Id);
exampleSentence.Text.Should().Be("Hello, world!");
exampleSentence.DefinitionId.Should().Be(definitionId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.Harmony.Tests/ModelSnapshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task ModelSnapshotShowsMultipleChanges()
var secondChange = await WriteNextChange(SetWord(entityId, "second"));
var snapshot = await DataModel.GetProjectSnapshot();
var simpleSnapshot = snapshot.Snapshots.Values.First();
var entry = (Word) await DataModel.GetBySnapshotId(simpleSnapshot.Id);
var entry = await DataModel.GetBySnapshotId<Word>(simpleSnapshot.Id);
entry.Text.Should().Be("second");
snapshot.LastChange.Should().Be(secondChange.DateTime);
}
Expand Down
8 changes: 4 additions & 4 deletions src/SIL.Harmony.Tests/SyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public async Task CanSyncSimpleChange()
var client1Snapshot = await _client1.DataModel.GetProjectSnapshot();
var client2Snapshot = await _client2.DataModel.GetProjectSnapshot();
client1Snapshot.LastCommitHash.Should().Be(client2Snapshot.LastCommitHash);
var client2Entity1 = (Word) await _client2.DataModel.GetBySnapshotId(client2Snapshot.Snapshots[entity1Id].Id);
var client2Entity1 = await _client2.DataModel.GetBySnapshotId<Word>(client2Snapshot.Snapshots[entity1Id].Id);
client2Entity1.Text.Should().Be("entity1");
var client1Entity2 = (Word) await _client1.DataModel.GetBySnapshotId(client1Snapshot.Snapshots[entity2Id].Id);
var client1Entity2 = await _client1.DataModel.GetBySnapshotId<Word>(client1Snapshot.Snapshots[entity2Id].Id);
client1Entity2.Text.Should().Be("entity2");
}

Expand Down Expand Up @@ -93,12 +93,12 @@ public async Task SyncMultipleClientChanges(int clientCount)
serverSnapshot.Snapshots.Should().HaveCount(clientCount + 1);
foreach (var entitySnapshot in serverSnapshot.Snapshots.Values)
{
var serverEntity = (Word) await _client1.DataModel.GetBySnapshotId(entitySnapshot.Id);
var serverEntity = await _client1.DataModel.GetBySnapshotId<Word>(entitySnapshot.Id);
foreach (var client in clients)
{
var clientSnapshot = await client.DataModel.GetProjectSnapshot();
var simpleSnapshot = clientSnapshot.Snapshots.Should().ContainKey(entitySnapshot.EntityId).WhoseValue;
var entity = (Word) await client.DataModel.GetBySnapshotId(simpleSnapshot.Id);
var entity = await client.DataModel.GetBySnapshotId<Word>(simpleSnapshot.Id);
entity.Should().BeEquivalentTo(serverEntity);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SIL.Harmony/DataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ public IQueryable<T> GetLatestObjects<T>() where T : class
return q;
}

public async Task<object> GetBySnapshotId(Guid snapshotId)
public async Task<T> GetBySnapshotId<T>(Guid snapshotId)
{
return await _crdtRepository.GetObjectBySnapshotId(snapshotId);
return await _crdtRepository.GetObjectBySnapshotId<T>(snapshotId);
}

public async Task<Dictionary<Guid, ObjectSnapshot>> GetSnapshotsAt(DateTimeOffset dateTime)
Expand Down
4 changes: 2 additions & 2 deletions src/SIL.Harmony/Db/CrdtRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ public async Task<Commit[]> GetCommitsAfter(Commit? commit)
.LastOrDefaultAsync(s => s.EntityId == objectId && (ignoreChangesAfter == null || s.Commit.DateTime <= ignoreChangesAfter));
}

public async Task<object> GetObjectBySnapshotId(Guid snapshotId)
public async Task<T> GetObjectBySnapshotId<T>(Guid snapshotId)
{
var entity = await Snapshots
.Where(s => s.Id == snapshotId)
.Select(s => s.Entity)
.SingleOrDefaultAsync()
?? throw new ArgumentException($"unable to find snapshot with id {snapshotId}");
return entity;
return (T) entity;
}

public async Task<T?> GetCurrent<T>(Guid objectId) where T: class
Expand Down

0 comments on commit 7ac1cd4

Please sign in to comment.