Skip to content

Commit

Permalink
Merge pull request #124 from CodebreakerApp/116-cosmos-library-updates
Browse files Browse the repository at this point in the history
116 cosmos library updates
  • Loading branch information
christiannagel authored Dec 26, 2023
2 parents 66ae4d7 + 245eba3 commit e160944
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</PropertyGroup>

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

<ItemGroup>
Expand Down
38 changes: 20 additions & 18 deletions src/services/gameapi/Codebreaker.Data.Cosmos/GamesCosmosContext.cs
Original file line number Diff line number Diff line change
@@ -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<GamesCosmosContext> 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)
{
Expand All @@ -19,15 +15,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

gameModel.Property<string>(PartitionKey);
gameModel.HasPartitionKey(PartitionKey);
gameModel.HasKey(nameof(Game.GameId), PartitionKey);
gameModel.HasKey(nameof(Game.Id), PartitionKey);

gameModel.HasDiscriminator<string>("Discriminator")
.HasValue<Game>("Gamev2");

gameModel.Property(g => g.FieldValues)
.HasConversion(_fieldValueConverter, _fieldValueComparer);
.HasConversion(s_fieldValueConverter, s_fieldValueComparer);
}

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

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 =
Expand All @@ -47,21 +46,24 @@ public async Task AddMoveAsync(Game game, Move _, CancellationToken cancellation
await SaveChangesAsync(cancellationToken);
}

public async Task<bool> DeleteGameAsync(Guid gameId, CancellationToken cancellationToken = default)
public async Task<bool> 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);
await SaveChangesAsync(cancellationToken);
return true;
}

public async Task<Game?> GetGameAsync(Guid gameId, CancellationToken cancellationToken = default)
public async Task<Game?> 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;
}

Expand All @@ -85,7 +87,7 @@ public async Task<IEnumerable<Game>> 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);
Expand All @@ -103,7 +105,7 @@ public async Task<IEnumerable<Game>> GetGamesAsync(GamesQuery gamesQuery, Cancel
public async Task<Game> UpdateGameAsync(Game game, CancellationToken cancellationToken = default)
{
SetPartitionKey(game);
Games.Update(game);
Games.Add(game);
await SaveChangesAsync(cancellationToken);
return game;
}
Expand Down
6 changes: 5 additions & 1 deletion src/services/gameapi/Codebreaker.Data.Cosmos/Usings.cs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
9 changes: 9 additions & 0 deletions src/services/gameapi/Codebreaker.Data.Cosmos/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit e160944

Please sign in to comment.