Skip to content

Commit 66ae4d7

Browse files
Merge pull request #123 from CodebreakerApp/122-sqlserverlibupdates
122 updates SQL Server library
2 parents 4392afc + efdc718 commit 66ae4d7

10 files changed

+48
-44
lines changed

src/services/gameapi/Codebreaker.Data.SqlServer/Codebreaker.Data.SqlServer.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PackageId>CNinnovation.Codebreaker.SqlServer</PackageId>
55
<TargetFrameworks>net8.0</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
7-
<LangVersion>preview</LangVersion>
7+
<LangVersion>latest</LangVersion>
88
<Nullable>enable</Nullable>
99
<PackageTags>
1010
Codebreaker;CNinnovation;SqlServer
@@ -18,8 +18,8 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="CNinnovation.Codebreaker.BackendModels" Version="3.5.0-beta.16" />
22-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0-rc.2.23480.1" />
21+
<PackageReference Include="CNinnovation.Codebreaker.BackendModels" Version="3.6.0-beta.20" />
22+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/GameConfiguration.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ internal class GameConfiguration : IEntityTypeConfiguration<Game>
66
{
77
public void Configure(EntityTypeBuilder<Game> builder)
88
{
9-
builder.HasKey(g => g.GameId);
10-
9+
builder.HasKey(g => g.Id);
10+
11+
builder.HasMany(g => g.Moves)
12+
.WithOne()
13+
.HasForeignKey("GameId");
14+
1115
builder.Property(g => g.GameType).HasMaxLength(20);
1216
builder.Property(g => g.PlayerName).HasMaxLength(60);
1317

src/services/gameapi/Codebreaker.Data.SqlServer/Configurations/MoveConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ internal class MoveConfiguration : IEntityTypeConfiguration<Move>
55
public void Configure(EntityTypeBuilder<Move> builder)
66
{
77
// shadow property for the foreign key
8-
builder.Property<Guid>(GamesSqlServerContext.GameId);
8+
builder.Property<Guid>("GameId");
99

1010
builder.Property(g => g.GuessPegs).HasMaxLength(120);
1111
builder.Property(g => g.KeyPegs).HasMaxLength(60);

src/services/gameapi/Codebreaker.Data.SqlServer/GamesSqlServerContext.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ namespace Codebreaker.Data.SqlServer;
44

55
public class GamesSqlServerContext(DbContextOptions<GamesSqlServerContext> options) : DbContext(options), IGamesRepository
66
{
7-
internal const string GameId = nameof(GameId);
8-
97
protected override void OnModelCreating(ModelBuilder modelBuilder)
108
{
119
modelBuilder.HasDefaultSchema("codebreaker");
1210
modelBuilder.ApplyConfiguration(new GameConfiguration());
1311
modelBuilder.ApplyConfiguration(new MoveConfiguration());
14-
15-
modelBuilder.Entity<Game>()
16-
.HasMany(g => g.Moves)
17-
.WithOne()
18-
.HasForeignKey(GameId);
1912
}
2013

2114
public DbSet<Game> Games => Set<Game>();
@@ -35,22 +28,20 @@ public async Task AddMoveAsync(Game game, Move move, CancellationToken cancellat
3528
await SaveChangesAsync(cancellationToken);
3629
}
3730

38-
public async Task<bool> DeleteGameAsync(Guid gameId, CancellationToken cancellationToken = default)
31+
public async Task<bool> DeleteGameAsync(Guid id, CancellationToken cancellationToken = default)
3932
{
40-
var game = await Games.FindAsync(new object[] { gameId }, cancellationToken);
41-
if (game is null)
42-
return false;
43-
Games.Remove(game);
44-
await SaveChangesAsync(cancellationToken);
45-
return true;
33+
var affected = await Games
34+
.Where(g => g.Id == id)
35+
.ExecuteDeleteAsync(cancellationToken);
36+
return affected == 1;
4637
}
4738

48-
public async Task<Game?> GetGameAsync(Guid gameId, CancellationToken cancellationToken = default)
39+
public async Task<Game?> GetGameAsync(Guid id, CancellationToken cancellationToken = default)
4940
{
5041
var game = await Games
5142
.Include("Moves")
5243
.TagWith(nameof(GetGameAsync))
53-
.SingleOrDefaultAsync(g => g.GameId == gameId, cancellationToken);
44+
.SingleOrDefaultAsync(g => g.Id == id, cancellationToken);
5445
return game;
5546
}
5647

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20230709114237_InitGames.cs renamed to src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/20231225095734_InitGames.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
1919
schema: "codebreaker",
2020
columns: table => new
2121
{
22-
GameId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
22+
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
2323
GameType = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
2424
PlayerName = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
2525
StartTime = table.Column<DateTime>(type: "datetime2", nullable: false),
@@ -34,29 +34,29 @@ protected override void Up(MigrationBuilder migrationBuilder)
3434
},
3535
constraints: table =>
3636
{
37-
table.PrimaryKey("PK_Games", x => x.GameId);
37+
table.PrimaryKey("PK_Games", x => x.Id);
3838
});
3939

4040
migrationBuilder.CreateTable(
4141
name: "Moves",
4242
schema: "codebreaker",
4343
columns: table => new
4444
{
45-
MoveId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
45+
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
4646
MoveNumber = table.Column<int>(type: "int", nullable: false),
4747
GuessPegs = table.Column<string>(type: "nvarchar(120)", maxLength: 120, nullable: false),
4848
KeyPegs = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
4949
GameId = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
5050
},
5151
constraints: table =>
5252
{
53-
table.PrimaryKey("PK_Moves", x => x.MoveId);
53+
table.PrimaryKey("PK_Moves", x => x.Id);
5454
table.ForeignKey(
5555
name: "FK_Moves_Games_GameId",
5656
column: x => x.GameId,
5757
principalSchema: "codebreaker",
5858
principalTable: "Games",
59-
principalColumn: "GameId",
59+
principalColumn: "Id",
6060
onDelete: ReferentialAction.Cascade);
6161
});
6262

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/gameapi/Codebreaker.Data.SqlServer/Migrations/GamesSqlServerContextModelSnapshot.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1818
#pragma warning disable 612, 618
1919
modelBuilder
2020
.HasDefaultSchema("codebreaker")
21-
.HasAnnotation("ProductVersion", "8.0.0-preview.5.23280.1")
21+
.HasAnnotation("ProductVersion", "8.0.0")
2222
.HasAnnotation("Relational:MaxIdentifierLength", 128);
2323

2424
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
2525

2626
modelBuilder.Entity("Codebreaker.GameAPIs.Models.Game", b =>
2727
{
28-
b.Property<Guid>("GameId")
28+
b.Property<Guid>("Id")
2929
.ValueGeneratedOnAdd()
3030
.HasColumnType("uniqueidentifier");
3131

@@ -74,14 +74,14 @@ protected override void BuildModel(ModelBuilder modelBuilder)
7474
b.Property<DateTime>("StartTime")
7575
.HasColumnType("datetime2");
7676

77-
b.HasKey("GameId");
77+
b.HasKey("Id");
7878

7979
b.ToTable("Games", "codebreaker");
8080
});
8181

8282
modelBuilder.Entity("Codebreaker.GameAPIs.Models.Move", b =>
8383
{
84-
b.Property<Guid>("MoveId")
84+
b.Property<Guid>("Id")
8585
.ValueGeneratedOnAdd()
8686
.HasColumnType("uniqueidentifier");
8787

@@ -101,7 +101,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
101101
b.Property<int>("MoveNumber")
102102
.HasColumnType("int");
103103

104-
b.HasKey("MoveId");
104+
b.HasKey("Id");
105105

106106
b.HasIndex("GameId");
107107

src/services/gameapi/Codebreaker.Data.SqlServer/docs/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ This library contains the data backend for Codebreaker for SQL Server using EF C
55
See https://github.com/codebreakerapp for more information on the complete solution.
66

77
See [Codebreakerlight](https://github.com/codebreakerapp/codebreakerlight) for a simple version of the Codebreaker solution with a Wiki to create your own Codebreaker service.
8+
9+
## Types available in this package
10+
11+
12+
| Type | Description |
13+
| --- | --- |
14+
| `GamesSqlServerContext` | This class implements `IGamesRepository` |
15+
16+
Configure this class to be injected for `IGamesRepository` in your DI container when Codebreaker games data should be stored in SQL Server.

0 commit comments

Comments
 (0)