Skip to content

Commit e160944

Browse files
Merge pull request #124 from CodebreakerApp/116-cosmos-library-updates
116 cosmos library updates
2 parents 66ae4d7 + 245eba3 commit e160944

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

src/services/gameapi/Codebreaker.Data.Cosmos/Codebreaker.Data.Cosmos.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="CNinnovation.Codebreaker.BackendModels" Version="3.5.0-beta.16" />
21-
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="8.0.0-rc.2.23480.1" />
20+
<PackageReference Include="CNinnovation.Codebreaker.BackendModels" Version="3.6.0-beta.19" />
21+
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="8.0.0" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

src/services/gameapi/Codebreaker.Data.Cosmos/GamesCosmosContext.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
using Codebreaker.Data.Cosmos.Utilities;
2-
using Codebreaker.GameAPIs.Data;
3-
4-
using Microsoft.EntityFrameworkCore;
5-
6-
namespace Codebreaker.Data.Cosmos;
1+
namespace Codebreaker.Data.Cosmos;
72

83
public class GamesCosmosContext(DbContextOptions<GamesCosmosContext> options) : DbContext(options), IGamesRepository
94
{
5+
private static readonly FieldValueValueConverter s_fieldValueConverter = new();
6+
private static readonly FieldValueComparer s_fieldValueComparer = new();
7+
108
private const string PartitionKey = nameof(PartitionKey);
119
private const string ContainerName = "GamesV3";
12-
private readonly FieldValueValueConverter _fieldValueConverter = new();
13-
private readonly FieldValueComparer _fieldValueComparer = new();
1410

1511
protected override void OnModelCreating(ModelBuilder modelBuilder)
1612
{
@@ -19,15 +15,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
1915

2016
gameModel.Property<string>(PartitionKey);
2117
gameModel.HasPartitionKey(PartitionKey);
22-
gameModel.HasKey(nameof(Game.GameId), PartitionKey);
18+
gameModel.HasKey(nameof(Game.Id), PartitionKey);
19+
20+
gameModel.HasDiscriminator<string>("Discriminator")
21+
.HasValue<Game>("Gamev2");
2322

2423
gameModel.Property(g => g.FieldValues)
25-
.HasConversion(_fieldValueConverter, _fieldValueComparer);
24+
.HasConversion(s_fieldValueConverter, s_fieldValueComparer);
2625
}
2726

2827
public DbSet<Game> Games => Set<Game>();
2928

30-
public static string ComputePartitionKey(Game game) => game.GameId.ToString();
29+
public static string ComputePartitionKey(Game game) => game.Id.ToString();
3130

3231
public void SetPartitionKey(Game game) =>
3332
Entry(game).Property(PartitionKey).CurrentValue =
@@ -47,21 +46,24 @@ public async Task AddMoveAsync(Game game, Move _, CancellationToken cancellation
4746
await SaveChangesAsync(cancellationToken);
4847
}
4948

50-
public async Task<bool> DeleteGameAsync(Guid gameId, CancellationToken cancellationToken = default)
49+
public async Task<bool> DeleteGameAsync(Guid id, CancellationToken cancellationToken = default)
5150
{
52-
var game = await Games.FindAsync(new object[] { gameId, gameId.ToString() }, cancellationToken);
51+
var game = await Games
52+
.WithPartitionKey(id.ToString())
53+
.SingleOrDefaultAsync(g => g.Id == id, cancellationToken);
54+
5355
if (game is null)
5456
return false;
5557
Games.Remove(game);
5658
await SaveChangesAsync(cancellationToken);
5759
return true;
5860
}
5961

60-
public async Task<Game?> GetGameAsync(Guid gameId, CancellationToken cancellationToken = default)
62+
public async Task<Game?> GetGameAsync(Guid id, CancellationToken cancellationToken = default)
6163
{
6264
var game = await Games
63-
.WithPartitionKey(gameId.ToString())
64-
.SingleOrDefaultAsync(g => g.GameId == gameId, cancellationToken);
65+
.WithPartitionKey(id.ToString())
66+
.SingleOrDefaultAsync(g => g.Id == id, cancellationToken);
6567
return game;
6668
}
6769

@@ -85,7 +87,7 @@ public async Task<IEnumerable<Game>> GetGamesAsync(GamesQuery gamesQuery, Cancel
8587
if (gamesQuery.RunningOnly)
8688
query = query.Where(g => g.EndTime == null);
8789

88-
if (gamesQuery.Ended)
90+
if (gamesQuery.Ended == true)
8991
{
9092
query = query.Where(g => g.EndTime != null)
9193
.OrderBy(g => g.Duration);
@@ -103,7 +105,7 @@ public async Task<IEnumerable<Game>> GetGamesAsync(GamesQuery gamesQuery, Cancel
103105
public async Task<Game> UpdateGameAsync(Game game, CancellationToken cancellationToken = default)
104106
{
105107
SetPartitionKey(game);
106-
Games.Update(game);
108+
Games.Add(game);
107109
await SaveChangesAsync(cancellationToken);
108110
return game;
109111
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
global using Codebreaker.GameAPIs.Models;
1+
global using Codebreaker.Data.Cosmos.Utilities;
2+
global using Codebreaker.GameAPIs.Data;
3+
global using Codebreaker.GameAPIs.Models;
4+
5+
global using Microsoft.EntityFrameworkCore;

src/services/gameapi/Codebreaker.Data.Cosmos/Utilities/FieldValueValueConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
1+
using System.Text.Json;
22

3-
using System.Text.Json;
3+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
44

55
namespace Codebreaker.Data.Cosmos.Utilities;
66

src/services/gameapi/Codebreaker.Data.Cosmos/docs/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ This library contains the data backend for Codebreaker for Azure Cosmos DB using
55
See https://github.com/codebreakerapp for more information on the complete solution.
66

77
See [Codebreakerlight](https://github.com/codebreakerapp/codebreakerlight) for a simple version of the Codebreaker solution with a Wiki to create your own Codebreaker service.
8+
9+
## Types available in this package
10+
11+
12+
| Type | Description |
13+
| --- | --- |
14+
| `GamesCosmosContext` | This class implements `IGamesRepository` |
15+
16+
Configure this class to be injected for `IGamesRepository` in your DI container when Codebreaker games data should be stored with Azure Cosmos DB.

0 commit comments

Comments
 (0)