editorconfig added,

modelbuilder fix for local connString
This commit is contained in:
Aleksandr Popov
2023-06-26 08:58:42 +02:00
parent cd2605efe3
commit 171b640230
8 changed files with 506 additions and 19 deletions
@@ -16,7 +16,7 @@ namespace TURN.Services.Chess.Controllers
{
try
{
return HttpContext.Request.Query.TryGetValue("apikey", out Microsoft.Extensions.Primitives.StringValues value)
return HttpContext.Request.Query.TryGetValue("apikey", out var value)
? value.ToString() == Environment.GetEnvironmentVariable("AUTH_API_KEY")
? new JwtSecurityTokenHandler().WriteToken(CreateToken(user))
: StatusCode(403, "Wrong api key.")
@@ -30,7 +30,7 @@ namespace TURN.Services.Chess.Controllers
private SecurityToken CreateToken(string user)
{
List<Claim> claims = new()
var claims = new List<Claim>()
{
new Claim(ClaimTypes.Name, user),
new Claim(ClaimTypes.Role, "admin")
@@ -31,7 +31,7 @@ namespace TURN.Services.Chess.Controllers
{
try
{
ChessGame game = new();
var game = new ChessGame();
return Ok(param);
}
@@ -50,7 +50,7 @@ namespace TURN.Services.Chess.Controllers
{
try
{
ChessGame game = new();
var game = new ChessGame();
Game obj = new()
{
@@ -62,7 +62,7 @@ namespace TURN.Services.Chess.Controllers
_ = _db.Add(obj);
_ = _db.SaveChanges();
string mes = $"Game {obj.GameId} started.";
var mes = $"Game {obj.GameId} started.";
_logger.Info(mes);
return Ok(mes);
@@ -15,10 +15,20 @@ namespace TURN.Services.Chess.DBModel
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 OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
try
{
_ = Environment.GetEnvironmentVariable("DB") != null
? optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("DB"))
: optionsBuilder.UseNpgsql("Server=localhost;Port=5432;Database=TURNChess;UserId=postgres;" +
"Password=example;");
}
catch (Exception)
{
_ = optionsBuilder.UseNpgsql("Server=localhost;Port=5432;Database=TURNChess;UserId=postgres;Password=example;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@@ -11,7 +11,7 @@ namespace TURN.Services.Chess.Migrations
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
_ = migrationBuilder.CreateTable(
name: "Games",
columns: table => new
{
@@ -23,14 +23,14 @@ namespace TURN.Services.Chess.Migrations
},
constraints: table =>
{
table.PrimaryKey("PK_Games", x => x.GameId);
_ = table.PrimaryKey("PK_Games", x => x.GameId);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
_ = migrationBuilder.DropTable(
name: "Games");
}
}
+6 -3
View File
@@ -2,12 +2,12 @@ using Microsoft.AspNetCore;
using TURN.Services.Chess;
using LoggerFactory = TURN.Services.Chess.LoggerFactory;
NLog.Logger logger = LoggerFactory.GetLogger();
var logger = LoggerFactory.GetLogger();
logger.Info("Chess app starting.");
try
{
IWebHost host = BuildWebHost(args);
var host = BuildWebHost(args);
host.Run();
}
catch (Exception ex)
@@ -15,7 +15,9 @@ catch (Exception ex)
logger.Error(ex);
}
IWebHost BuildWebHost(string[] args) => WebHost
IWebHost BuildWebHost(string[] args)
{
return WebHost
.CreateDefaultBuilder(args)
.CaptureStartupErrors(false)
.ConfigureServices(services =>
@@ -24,3 +26,4 @@ IWebHost BuildWebHost(string[] args) => WebHost
})
.UseStartup<Startup>()
.Build();
}
@@ -0,0 +1,19 @@
{
"profiles": {
"TURN.Services.Chess": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:14721;http://localhost:14722"
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"publishAllPorts": true,
"useSSL": true
}
}
}
+2 -4
View File
@@ -1,5 +1,4 @@
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
@@ -18,8 +17,7 @@ namespace TURN.Services.Chess
{
jsonOptions.SerializerSettings.Converters.Add(new StringEnumConverter());
});
_ = services.AddDbContext<ModelBaseContext>(
options => options.UseNpgsql(Environment.GetEnvironmentVariable("DB")));
_ = services.AddDbContext<ModelBaseContext>();
}
public void Configure(IApplicationBuilder app)
@@ -48,7 +46,7 @@ namespace TURN.Services.Chess
Title = "TURN.Api.Swagger",
Version = "v1"
});
string xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
_ = services.AddSwaggerGenNewtonsoftSupport();