From 04fd3c44e701d662fa2ca99cd4ff1de60c3f2602 Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Tue, 26 Dec 2023 18:02:29 +0100 Subject: [PATCH 1/4] enhance readme #116 --- .../gameapi/Codebreaker.Data.Cosmos/docs/readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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. From 369a7b43e151599887c85424c7af53b4269a340d Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Tue, 26 Dec 2023 18:03:01 +0100 Subject: [PATCH 2/4] bump packages --- .../Codebreaker.Data.Cosmos/Codebreaker.Data.Cosmos.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 @@ - - + + From a66b19f9202e29e876c05f3850be2a038f5e99be Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Tue, 26 Dec 2023 18:25:25 +0100 Subject: [PATCH 3/4] GameId --> Id #116 --- .../GamesCosmosContext.cs | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) 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; } From 245eba3b451410b58cfea6164f2274dd769e2888 Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Tue, 26 Dec 2023 18:25:40 +0100 Subject: [PATCH 4/4] cleanup --- src/services/gameapi/Codebreaker.Data.Cosmos/Usings.cs | 6 +++++- .../Utilities/FieldValueValueConverter.cs | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) 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;