Skip to content

Commit

Permalink
Merge pull request #123 from CodebreakerApp/122-sqlserverlibupdates
Browse files Browse the repository at this point in the history
122 updates SQL Server library
  • Loading branch information
christiannagel authored Dec 26, 2023
2 parents 4392afc + efdc718 commit 66ae4d7
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>CNinnovation.Codebreaker.SqlServer</PackageId>
<TargetFrameworks>net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<PackageTags>
Codebreaker;CNinnovation;SqlServer
Expand All @@ -18,8 +18,8 @@
</PropertyGroup>

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

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ internal class GameConfiguration : IEntityTypeConfiguration<Game>
{
public void Configure(EntityTypeBuilder<Game> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal class MoveConfiguration : IEntityTypeConfiguration<Move>
public void Configure(EntityTypeBuilder<Move> builder)
{
// shadow property for the foreign key
builder.Property<Guid>(GamesSqlServerContext.GameId);
builder.Property<Guid>("GameId");

builder.Property(g => g.GuessPegs).HasMaxLength(120);
builder.Property(g => g.KeyPegs).HasMaxLength(60);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ namespace Codebreaker.Data.SqlServer;

public class GamesSqlServerContext(DbContextOptions<GamesSqlServerContext> 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<Game>()
.HasMany(g => g.Moves)
.WithOne()
.HasForeignKey(GameId);
}

public DbSet<Game> Games => Set<Game>();
Expand All @@ -35,22 +28,20 @@ public async Task AddMoveAsync(Game game, Move move, CancellationToken cancellat
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 }, 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<Game?> GetGameAsync(Guid gameId, CancellationToken cancellationToken = default)
public async Task<Game?> 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;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
schema: "codebreaker",
columns: table => new
{
GameId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
GameType = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
PlayerName = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
StartTime = table.Column<DateTime>(type: "datetime2", nullable: false),
Expand All @@ -34,29 +34,29 @@ protected override void Up(MigrationBuilder migrationBuilder)
},
constraints: table =>
{
table.PrimaryKey("PK_Games", x => x.GameId);
table.PrimaryKey("PK_Games", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Moves",
schema: "codebreaker",
columns: table => new
{
MoveId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
MoveNumber = table.Column<int>(type: "int", nullable: false),
GuessPegs = table.Column<string>(type: "nvarchar(120)", maxLength: 120, nullable: false),
KeyPegs = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
GameId = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
},
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);
});

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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<Guid>("GameId")
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

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

b.HasKey("GameId");
b.HasKey("Id");

b.ToTable("Games", "codebreaker");
});

modelBuilder.Entity("Codebreaker.GameAPIs.Models.Move", b =>
{
b.Property<Guid>("MoveId")
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

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

b.HasKey("MoveId");
b.HasKey("Id");

b.HasIndex("GameId");

Expand Down
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 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.

0 comments on commit 66ae4d7

Please sign in to comment.