diff --git a/src/services/gameapi/Codebreaker.Data.Cosmos/Codebreaker.Data.Cosmos.csproj b/src/services/gameapi/Codebreaker.Data.Cosmos/Codebreaker.Data.Cosmos.csproj
index bbb7157f..7a38f461 100644
--- a/src/services/gameapi/Codebreaker.Data.Cosmos/Codebreaker.Data.Cosmos.csproj
+++ b/src/services/gameapi/Codebreaker.Data.Cosmos/Codebreaker.Data.Cosmos.csproj
@@ -17,8 +17,8 @@
-
-
+
+
diff --git a/src/services/gameapi/Codebreaker.Data.Cosmos/GamesCosmosContext.cs b/src/services/gameapi/Codebreaker.Data.Cosmos/GamesCosmosContext.cs
index 58fbe48a..fc7aeea5 100644
--- a/src/services/gameapi/Codebreaker.Data.Cosmos/GamesCosmosContext.cs
+++ b/src/services/gameapi/Codebreaker.Data.Cosmos/GamesCosmosContext.cs
@@ -1,16 +1,12 @@
-using Codebreaker.Data.Cosmos.Utilities;
-using Codebreaker.GameAPIs.Data;
-
-using Microsoft.EntityFrameworkCore;
-
-namespace Codebreaker.Data.Cosmos;
+namespace Codebreaker.Data.Cosmos;
public class GamesCosmosContext(DbContextOptions options) : DbContext(options), IGamesRepository
{
+ private static readonly FieldValueValueConverter s_fieldValueConverter = new();
+ private static readonly FieldValueComparer s_fieldValueComparer = new();
+
private const string PartitionKey = nameof(PartitionKey);
private const string ContainerName = "GamesV3";
- private readonly FieldValueValueConverter _fieldValueConverter = new();
- private readonly FieldValueComparer _fieldValueComparer = new();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@@ -19,15 +15,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
gameModel.Property(PartitionKey);
gameModel.HasPartitionKey(PartitionKey);
- gameModel.HasKey(nameof(Game.GameId), PartitionKey);
+ gameModel.HasKey(nameof(Game.Id), PartitionKey);
+
+ gameModel.HasDiscriminator("Discriminator")
+ .HasValue("Gamev2");
gameModel.Property(g => g.FieldValues)
- .HasConversion(_fieldValueConverter, _fieldValueComparer);
+ .HasConversion(s_fieldValueConverter, s_fieldValueComparer);
}
public DbSet Games => Set();
- public static string ComputePartitionKey(Game game) => game.GameId.ToString();
+ public static string ComputePartitionKey(Game game) => game.Id.ToString();
public void SetPartitionKey(Game game) =>
Entry(game).Property(PartitionKey).CurrentValue =
@@ -47,9 +46,12 @@ public async Task AddMoveAsync(Game game, Move _, CancellationToken cancellation
await SaveChangesAsync(cancellationToken);
}
- public async Task DeleteGameAsync(Guid gameId, CancellationToken cancellationToken = default)
+ public async Task DeleteGameAsync(Guid id, CancellationToken cancellationToken = default)
{
- var game = await Games.FindAsync(new object[] { gameId, gameId.ToString() }, cancellationToken);
+ var game = await Games
+ .WithPartitionKey(id.ToString())
+ .SingleOrDefaultAsync(g => g.Id == id, cancellationToken);
+
if (game is null)
return false;
Games.Remove(game);
@@ -57,11 +59,11 @@ public async Task DeleteGameAsync(Guid gameId, CancellationToken cancellat
return true;
}
- public async Task GetGameAsync(Guid gameId, CancellationToken cancellationToken = default)
+ public async Task GetGameAsync(Guid id, CancellationToken cancellationToken = default)
{
var game = await Games
- .WithPartitionKey(gameId.ToString())
- .SingleOrDefaultAsync(g => g.GameId == gameId, cancellationToken);
+ .WithPartitionKey(id.ToString())
+ .SingleOrDefaultAsync(g => g.Id == id, cancellationToken);
return game;
}
@@ -85,7 +87,7 @@ public async Task> GetGamesAsync(GamesQuery gamesQuery, Cancel
if (gamesQuery.RunningOnly)
query = query.Where(g => g.EndTime == null);
- if (gamesQuery.Ended)
+ if (gamesQuery.Ended == true)
{
query = query.Where(g => g.EndTime != null)
.OrderBy(g => g.Duration);
@@ -103,7 +105,7 @@ public async Task> GetGamesAsync(GamesQuery gamesQuery, Cancel
public async Task UpdateGameAsync(Game game, CancellationToken cancellationToken = default)
{
SetPartitionKey(game);
- Games.Update(game);
+ Games.Add(game);
await SaveChangesAsync(cancellationToken);
return game;
}
diff --git a/src/services/gameapi/Codebreaker.Data.Cosmos/Usings.cs b/src/services/gameapi/Codebreaker.Data.Cosmos/Usings.cs
index c78963ce..f314b248 100644
--- a/src/services/gameapi/Codebreaker.Data.Cosmos/Usings.cs
+++ b/src/services/gameapi/Codebreaker.Data.Cosmos/Usings.cs
@@ -1 +1,5 @@
-global using Codebreaker.GameAPIs.Models;
+global using Codebreaker.Data.Cosmos.Utilities;
+global using Codebreaker.GameAPIs.Data;
+global using Codebreaker.GameAPIs.Models;
+
+global using Microsoft.EntityFrameworkCore;
\ No newline at end of file
diff --git a/src/services/gameapi/Codebreaker.Data.Cosmos/Utilities/FieldValueValueConverter.cs b/src/services/gameapi/Codebreaker.Data.Cosmos/Utilities/FieldValueValueConverter.cs
index 38007dbd..76322af1 100644
--- a/src/services/gameapi/Codebreaker.Data.Cosmos/Utilities/FieldValueValueConverter.cs
+++ b/src/services/gameapi/Codebreaker.Data.Cosmos/Utilities/FieldValueValueConverter.cs
@@ -1,6 +1,6 @@
-using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using System.Text.Json;
-using System.Text.Json;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Codebreaker.Data.Cosmos.Utilities;
diff --git a/src/services/gameapi/Codebreaker.Data.Cosmos/docs/readme.md b/src/services/gameapi/Codebreaker.Data.Cosmos/docs/readme.md
index 773d7976..64bcef5b 100644
--- a/src/services/gameapi/Codebreaker.Data.Cosmos/docs/readme.md
+++ b/src/services/gameapi/Codebreaker.Data.Cosmos/docs/readme.md
@@ -5,3 +5,12 @@ This library contains the data backend for Codebreaker for Azure Cosmos DB using
See https://github.com/codebreakerapp for more information on the complete solution.
See [Codebreakerlight](https://github.com/codebreakerapp/codebreakerlight) for a simple version of the Codebreaker solution with a Wiki to create your own Codebreaker service.
+
+## Types available in this package
+
+
+| Type | Description |
+| --- | --- |
+| `GamesCosmosContext` | This class implements `IGamesRepository` |
+
+Configure this class to be injected for `IGamesRepository` in your DI container when Codebreaker games data should be stored with Azure Cosmos DB.