db + migrations
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using ChessDotNet;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NLog;
|
||||
using TURN.Services.Chess.DBModel;
|
||||
|
||||
namespace TURN.Services.Chess.Controllers
|
||||
{
|
||||
@@ -11,6 +14,15 @@ namespace TURN.Services.Chess.Controllers
|
||||
{
|
||||
public const string AuthSchemes = JwtBearerDefaults.AuthenticationScheme;
|
||||
|
||||
private readonly ModelBaseContext _db;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public TurnController(ModelBaseContext db, Logger logger)
|
||||
{
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Тестовый гет.
|
||||
/// </summary>
|
||||
@@ -19,10 +31,42 @@ namespace TURN.Services.Chess.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
ChessGame game = new();
|
||||
|
||||
return Ok(param);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex);
|
||||
return StatusCode(500, $"Internal server error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создать игру.
|
||||
/// </summary>
|
||||
[HttpGet("startgame/{blackUser}/{whiteUser}")]
|
||||
public ActionResult<string> StartGame(string blackUser, string whiteUser)
|
||||
{
|
||||
try
|
||||
{
|
||||
ChessGame game = new();
|
||||
|
||||
Game obj = new()
|
||||
{
|
||||
BlackUserName = blackUser,
|
||||
WhiteUserName = whiteUser,
|
||||
StartDate = DateTimeOffset.Now.ToUnixTimeSeconds().ToString()
|
||||
};
|
||||
|
||||
_ = _db.Add(obj);
|
||||
_ = _db.SaveChanges();
|
||||
|
||||
return Ok($"Game {obj.GameId} started.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex);
|
||||
return StatusCode(500, $"Internal server error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace TURN.Services.Chess.DBModel
|
||||
{
|
||||
public partial class Game
|
||||
{
|
||||
public long? GameId { get; set; }
|
||||
public string? BlackUserName { get; set; }
|
||||
public string? WhiteUserName { get; set; }
|
||||
public string? StartDate { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TURN.Services.Chess.DBModel
|
||||
{
|
||||
public partial class ModelBaseContext : DbContext
|
||||
{
|
||||
public ModelBaseContext()
|
||||
{
|
||||
}
|
||||
|
||||
public ModelBaseContext(DbContextOptions<ModelBaseContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual DbSet<Game> Games { get; set; }
|
||||
|
||||
//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
//{
|
||||
// _ = optionsBuilder.UseNpgsql("Server=localhost;Port=5432;Database=TURNChess;UserId=postgres;Password=example;");
|
||||
//}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
_ = modelBuilder.Entity<Game>(entity =>
|
||||
{
|
||||
_ = entity.HasKey(e => e.GameId);
|
||||
_ = entity.Property(e => e.GameId)
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnName("GameId");
|
||||
});
|
||||
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
|
||||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using TURN.Services.Chess.DBModel;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace TURN.Services.Chess.Migrations
|
||||
{
|
||||
[DbContext(typeof(ModelBaseContext))]
|
||||
[Migration("20230625160918_Init")]
|
||||
partial class Init
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("TURN.Services.Chess.DBModel.Game", b =>
|
||||
{
|
||||
b.Property<long?>("GameId")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bigint")
|
||||
.HasColumnName("GameId");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long?>("GameId"));
|
||||
|
||||
b.Property<string>("BlackUserName")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("StartDate")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("WhiteUserName")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("GameId");
|
||||
|
||||
b.ToTable("Games");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace TURN.Services.Chess.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Init : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Games",
|
||||
columns: table => new
|
||||
{
|
||||
GameId = table.Column<long>(type: "bigint", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
BlackUserName = table.Column<string>(type: "varchar", nullable: true),
|
||||
WhiteUserName = table.Column<string>(type: "varchar", nullable: true),
|
||||
StartDate = table.Column<string>(type: "varchar", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Games", x => x.GameId);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Games");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using TURN.Services.Chess.DBModel;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace TURN.Services.Chess.Migrations
|
||||
{
|
||||
[DbContext(typeof(ModelBaseContext))]
|
||||
partial class ModelBaseContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("TURN.Services.Chess.DBModel.Game", b =>
|
||||
{
|
||||
b.Property<long?>("GameId")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bigint")
|
||||
.HasColumnName("GameId");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long?>("GameId"));
|
||||
|
||||
b.Property<string>("BlackUserName")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("StartDate")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("WhiteUserName")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("GameId");
|
||||
|
||||
b.ToTable("Games");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5110"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker",
|
||||
"publishAllPorts": true
|
||||
}
|
||||
},
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:12777",
|
||||
"sslPort": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using TURN.Services.Chess.DBModel;
|
||||
|
||||
namespace TURN.Services.Chess
|
||||
{
|
||||
@@ -16,8 +18,8 @@ namespace TURN.Services.Chess
|
||||
{
|
||||
jsonOptions.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
});
|
||||
//_ = services.AddDbContext<ModelBaseContext>(
|
||||
// options => options.UseSqlServer(Environment.GetEnvironmentVariable("DB")));
|
||||
_ = services.AddDbContext<ModelBaseContext>(
|
||||
options => options.UseNpgsql(Environment.GetEnvironmentVariable("DB")));
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
|
||||
@@ -11,12 +11,23 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ChessDotNet" Version="0.12.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.31.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="NLog" Version="5.2.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
|
||||
@@ -24,4 +35,8 @@
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Properties\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user