From a66b19f9202e29e876c05f3850be2a038f5e99be Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Tue, 26 Dec 2023 18:25:25 +0100 Subject: [PATCH] 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; }