db + migrations

This commit is contained in:
Aleksandr Popov
2023-06-25 17:56:30 +02:00
parent 944712df97
commit 751982beff
11 changed files with 110 additions and 172 deletions
@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using ChessDotNet;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TURN.Services.Chess.DBModel;
namespace TURN.Services.Chess.Controllers
{
@@ -11,6 +13,13 @@ namespace TURN.Services.Chess.Controllers
{
public const string AuthSchemes = JwtBearerDefaults.AuthenticationScheme;
private readonly ModelBaseContext _db;
public TurnController(ModelBaseContext db)
{
_db = db;
}
/// <summary>
/// Тестовый гет.
/// </summary>
@@ -19,6 +28,8 @@ namespace TURN.Services.Chess.Controllers
{
try
{
ChessGame game = new();
return Ok(param);
}
catch (Exception ex)
@@ -26,5 +37,33 @@ namespace TURN.Services.Chess.Controllers
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 = DateTime.Now.ToString()
};
_ = _db.Add(obj);
_ = _db.SaveChanges();
return Ok($"Game {obj.GameId} started.");
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
}
}
+10
View File
@@ -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);
}
}
@@ -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
}
}
}
+4 -2
View File
@@ -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>