diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Codebreaker.Data.SqlServer.csproj b/src/services/gameapi/Codebreaker.Data.SqlServer/Codebreaker.Data.SqlServer.csproj index 19630c5a..909a0a68 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/Codebreaker.Data.SqlServer.csproj +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/Codebreaker.Data.SqlServer.csproj @@ -4,7 +4,7 @@ CNinnovation.Codebreaker.SqlServer net8.0 enable - preview + latest enable Codebreaker;CNinnovation;SqlServer @@ -18,8 +18,8 @@ - - + + diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/GameConfiguration.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/GameConfiguration.cs index b5dde781..486acac6 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/GameConfiguration.cs +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/GameConfiguration.cs @@ -6,8 +6,12 @@ internal class GameConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { - builder.HasKey(g => g.GameId); - + builder.HasKey(g => g.Id); + + builder.HasMany(g => g.Moves) + .WithOne() + .HasForeignKey("GameId"); + builder.Property(g => g.GameType).HasMaxLength(20); builder.Property(g => g.PlayerName).HasMaxLength(60); diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/MoveConfiguration.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/MoveConfiguration.cs index af43fca3..10b12e06 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/MoveConfiguration.cs +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/MoveConfiguration.cs @@ -5,7 +5,7 @@ internal class MoveConfiguration : IEntityTypeConfiguration public void Configure(EntityTypeBuilder builder) { // shadow property for the foreign key - builder.Property(GamesSqlServerContext.GameId); + builder.Property("GameId"); builder.Property(g => g.GuessPegs).HasMaxLength(120); builder.Property(g => g.KeyPegs).HasMaxLength(60); diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/GamesSqlServerContext.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/GamesSqlServerContext.cs index 6dda9014..4f41f881 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/GamesSqlServerContext.cs +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/GamesSqlServerContext.cs @@ -4,18 +4,11 @@ namespace Codebreaker.Data.SqlServer; public class GamesSqlServerContext(DbContextOptions options) : DbContext(options), IGamesRepository { - internal const string GameId = nameof(GameId); - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("codebreaker"); modelBuilder.ApplyConfiguration(new GameConfiguration()); modelBuilder.ApplyConfiguration(new MoveConfiguration()); - - modelBuilder.Entity() - .HasMany(g => g.Moves) - .WithOne() - .HasForeignKey(GameId); } public DbSet Games => Set(); @@ -35,22 +28,20 @@ public async Task AddMoveAsync(Game game, Move move, CancellationToken cancellat 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 }, cancellationToken); - if (game is null) - return false; - Games.Remove(game); - await SaveChangesAsync(cancellationToken); - return true; + var affected = await Games + .Where(g => g.Id == id) + .ExecuteDeleteAsync(cancellationToken); + return affected == 1; } - public async Task GetGameAsync(Guid gameId, CancellationToken cancellationToken = default) + public async Task GetGameAsync(Guid id, CancellationToken cancellationToken = default) { var game = await Games .Include("Moves") .TagWith(nameof(GetGameAsync)) - .SingleOrDefaultAsync(g => g.GameId == gameId, cancellationToken); + .SingleOrDefaultAsync(g => g.Id == id, cancellationToken); return game; } diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114237_InitGames.Designer.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095734_InitGames.Designer.cs similarity index 93% rename from src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114237_InitGames.Designer.cs rename to src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095734_InitGames.Designer.cs index b6f56e10..5477aa9d 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114237_InitGames.Designer.cs +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095734_InitGames.Designer.cs @@ -12,7 +12,7 @@ namespace Codebreaker.Data.SqlServer.Migrations { [DbContext(typeof(GamesSqlServerContext))] - [Migration("20230709114237_InitGames")] + [Migration("20231225095734_InitGames")] partial class InitGames { /// @@ -21,14 +21,14 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasDefaultSchema("codebreaker") - .HasAnnotation("ProductVersion", "8.0.0-preview.5.23280.1") + .HasAnnotation("ProductVersion", "8.0.0") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); modelBuilder.Entity("Codebreaker.GameAPIs.Models.Game", b => { - b.Property("GameId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); @@ -74,14 +74,14 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("StartTime") .HasColumnType("datetime2"); - b.HasKey("GameId"); + b.HasKey("Id"); b.ToTable("Games", "codebreaker"); }); modelBuilder.Entity("Codebreaker.GameAPIs.Models.Move", b => { - b.Property("MoveId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); @@ -101,7 +101,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("MoveNumber") .HasColumnType("int"); - b.HasKey("MoveId"); + b.HasKey("Id"); b.HasIndex("GameId"); diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114237_InitGames.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095734_InitGames.cs similarity index 89% rename from src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114237_InitGames.cs rename to src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095734_InitGames.cs index 27435ff9..f455fbd2 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114237_InitGames.cs +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095734_InitGames.cs @@ -19,7 +19,7 @@ protected override void Up(MigrationBuilder migrationBuilder) schema: "codebreaker", columns: table => new { - GameId = table.Column(type: "uniqueidentifier", nullable: false), + Id = table.Column(type: "uniqueidentifier", nullable: false), GameType = table.Column(type: "nvarchar(20)", maxLength: 20, nullable: false), PlayerName = table.Column(type: "nvarchar(60)", maxLength: 60, nullable: false), StartTime = table.Column(type: "datetime2", nullable: false), @@ -34,7 +34,7 @@ protected override void Up(MigrationBuilder migrationBuilder) }, constraints: table => { - table.PrimaryKey("PK_Games", x => x.GameId); + table.PrimaryKey("PK_Games", x => x.Id); }); migrationBuilder.CreateTable( @@ -42,7 +42,7 @@ protected override void Up(MigrationBuilder migrationBuilder) schema: "codebreaker", columns: table => new { - MoveId = table.Column(type: "uniqueidentifier", nullable: false), + Id = table.Column(type: "uniqueidentifier", nullable: false), MoveNumber = table.Column(type: "int", nullable: false), GuessPegs = table.Column(type: "nvarchar(120)", maxLength: 120, nullable: false), KeyPegs = table.Column(type: "nvarchar(60)", maxLength: 60, nullable: false), @@ -50,13 +50,13 @@ protected override void Up(MigrationBuilder migrationBuilder) }, constraints: table => { - table.PrimaryKey("PK_Moves", x => x.MoveId); + table.PrimaryKey("PK_Moves", x => x.Id); table.ForeignKey( name: "FK_Moves_Games_GameId", column: x => x.GameId, principalSchema: "codebreaker", principalTable: "Games", - principalColumn: "GameId", + principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114311_AddPlayerIsAuthenticated.Designer.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095931_AddPlayerIsAuthenticated.Designer.cs similarity index 93% rename from src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114311_AddPlayerIsAuthenticated.Designer.cs rename to src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095931_AddPlayerIsAuthenticated.Designer.cs index 23ac8780..65f60b3a 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114311_AddPlayerIsAuthenticated.Designer.cs +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095931_AddPlayerIsAuthenticated.Designer.cs @@ -12,7 +12,7 @@ namespace Codebreaker.Data.SqlServer.Migrations { [DbContext(typeof(GamesSqlServerContext))] - [Migration("20230709114311_AddPlayerIsAuthenticated")] + [Migration("20231225095931_AddPlayerIsAuthenticated")] partial class AddPlayerIsAuthenticated { /// @@ -21,14 +21,14 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasDefaultSchema("codebreaker") - .HasAnnotation("ProductVersion", "8.0.0-preview.5.23280.1") + .HasAnnotation("ProductVersion", "8.0.0") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); modelBuilder.Entity("Codebreaker.GameAPIs.Models.Game", b => { - b.Property("GameId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); @@ -77,14 +77,14 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("StartTime") .HasColumnType("datetime2"); - b.HasKey("GameId"); + b.HasKey("Id"); b.ToTable("Games", "codebreaker"); }); modelBuilder.Entity("Codebreaker.GameAPIs.Models.Move", b => { - b.Property("MoveId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); @@ -104,7 +104,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("MoveNumber") .HasColumnType("int"); - b.HasKey("MoveId"); + b.HasKey("Id"); b.HasIndex("GameId"); diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114311_AddPlayerIsAuthenticated.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095931_AddPlayerIsAuthenticated.cs similarity index 100% rename from src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114311_AddPlayerIsAuthenticated.cs rename to src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095931_AddPlayerIsAuthenticated.cs diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/GamesSqlServerContextModelSnapshot.cs b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/GamesSqlServerContextModelSnapshot.cs index 280f0f6a..97148817 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/GamesSqlServerContextModelSnapshot.cs +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/GamesSqlServerContextModelSnapshot.cs @@ -18,14 +18,14 @@ protected override void BuildModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasDefaultSchema("codebreaker") - .HasAnnotation("ProductVersion", "8.0.0-preview.5.23280.1") + .HasAnnotation("ProductVersion", "8.0.0") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); modelBuilder.Entity("Codebreaker.GameAPIs.Models.Game", b => { - b.Property("GameId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); @@ -74,14 +74,14 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("StartTime") .HasColumnType("datetime2"); - b.HasKey("GameId"); + b.HasKey("Id"); b.ToTable("Games", "codebreaker"); }); modelBuilder.Entity("Codebreaker.GameAPIs.Models.Move", b => { - b.Property("MoveId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); @@ -101,7 +101,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("MoveNumber") .HasColumnType("int"); - b.HasKey("MoveId"); + b.HasKey("Id"); b.HasIndex("GameId"); diff --git a/src/services/gameapi/Codebreaker.Data.SqlServer/docs/readme.md b/src/services/gameapi/Codebreaker.Data.SqlServer/docs/readme.md index 949dbfc1..3d66a4f0 100644 --- a/src/services/gameapi/Codebreaker.Data.SqlServer/docs/readme.md +++ b/src/services/gameapi/Codebreaker.Data.SqlServer/docs/readme.md @@ -5,3 +5,12 @@ This library contains the data backend for Codebreaker for SQL Server using EF C 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 | +| --- | --- | +| `GamesSqlServerContext` | This class implements `IGamesRepository` | + +Configure this class to be injected for `IGamesRepository` in your DI container when Codebreaker games data should be stored in SQL Server.