This commit is contained in:
Tomasi - Developing 2024-10-10 07:42:45 +02:00
parent ed79851e8b
commit 98f772e5d5
13 changed files with 133 additions and 49 deletions

View File

@ -7,11 +7,29 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Serilog" Version="4.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\v1\" />
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Database\Database.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ui\Ui.esproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
</Project>

View File

@ -1,11 +1,39 @@
// Configure Serilog logger
using Api.Configurations;
using Application;
using Database;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Serilog;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
Log.Information("Starting API . . . ");
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddSerilog(options =>
{
options.ReadFrom.Configuration(builder.Configuration);
});
builder.Services.AddDatabase(builder.Configuration);
builder.Services.AddApplication();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.ConfigureAndApiVersioning();
builder.Services.ConfigureAndAddSwagger();
builder.Services.ConfigureAndAddCors();
builder.Services.ConfigureAndAddHealthChecks(builder.Configuration);
var jwtSection = builder.Configuration.GetRequiredSection("Jwt");
builder.Services.ConfigureAndAddAuthentication(jwtSection);
var app = builder.Build();
@ -13,10 +41,30 @@ var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors("AllowAll");
app.UseSerilogRequestLogging();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapHealthChecks("/health", new HealthCheckOptions()
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.Run();
}
catch (Exception e)
{
Log.Fatal(e, e.Message);
}
finally
{
Log.CloseAndFlush();
}

View File

@ -14,8 +14,8 @@ public class PlayerConfiguration : IEntityTypeConfiguration<Player>
builder.Property(player => player.Level).IsRequired().HasMaxLength(3);
builder.HasOne(player => player.Rank)
.WithOne(rank => rank.Player)
.HasForeignKey<Rank>(rank => rank.PlayerId)
.WithMany(rank => rank.Players)
.HasForeignKey(player => player.RankId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(player => player.VsDuels)

View File

@ -16,33 +16,28 @@ public class RankConfiguration : IEntityTypeConfiguration<Rank>
{
new()
{
Id = new Guid(""),
Name = "R5",
Player = null!
Id = new Guid("b1c10a1c-5cf3-4e22-9fc1-d9b165b85dd3"),
Name = "R5"
},
new()
{
Id = new Guid(""),
Name = "R4",
Player = null!
Id = new Guid("0fc2f68a-0a4d-4922-981e-c624e4c39024"),
Name = "R4"
},
new()
{
Id = new Guid(""),
Name = "R3",
Player = null!
Id = new Guid("4970e1f5-f7f5-43e8-88cc-7f8fc4075418"),
Name = "R3"
},
new()
{
Id = new Guid(""),
Name = "R2",
Player = null!
Id = new Guid("d8d0c587-f269-45ff-b13e-4631298bf0af"),
Name = "R2"
},
new()
{
Id = new Guid(""),
Name = "R1",
Player = null!
Id = new Guid("326edef0-5074-43a5-9db9-edc71221a0f7"),
Name = "R1"
}
};

View File

@ -13,25 +13,25 @@ public class RoleConfiguration : IEntityTypeConfiguration<IdentityRole<Guid>>
{
new()
{
Id = new Guid(""),
Id = new Guid("d8b9f882-95f0-4ba0-80ed-9c22c27ac88a"),
NormalizedName = ApplicationRoles.SystemAdministrator.ToUpper(),
Name = ApplicationRoles.SystemAdministrator
},
new()
{
Id = new Guid(""),
Id = new Guid("47de05ba-ff1e-46b6-9995-269084006c24"),
NormalizedName = ApplicationRoles.Administrator.ToUpper(),
Name = ApplicationRoles.Administrator
},
new()
{
Id = new Guid(""),
Id = new Guid("5cc27946-5601-4a25-b9a9-75b8a11c0cf4"),
NormalizedName = ApplicationRoles.User.ToUpper(),
Name = ApplicationRoles.User
},
new()
{
Id = new Guid(""),
Id = new Guid("207bb0a3-ad50-49bb-bc41-b266fce66529"),
NormalizedName = ApplicationRoles.ReadOnly.ToUpper(),
Name = ApplicationRoles.ReadOnly
}

View File

@ -11,8 +11,8 @@ public class UserConfiguration : IEntityTypeConfiguration<User>
builder.Property(user => user.PlayerName).IsRequired().HasMaxLength(200);
builder.HasOne(user => user.Alliance)
.WithOne(alliance => alliance.User)
.HasForeignKey<Alliance>(alliance => alliance.UserId)
.WithMany(alliance => alliance.Users)
.HasForeignKey(user => user.AllianceId)
.OnDelete(DeleteBehavior.NoAction);
}
}

View File

@ -7,10 +7,15 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>

View File

@ -1,4 +1,5 @@
using Database.Entities;
using System.Reflection;
using Database.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@ -10,6 +11,8 @@ public static class DatabaseDependencyInjection
{
public static IServiceCollection AddDatabase(this IServiceCollection services, IConfiguration configuration)
{
services.AddDataProtection();
services.AddDbContext<ApplicationContext>(options =>
{
options.UseSqlServer(configuration.GetConnectionString("ApplicationDbConnection"));
@ -35,7 +38,6 @@ public static class DatabaseDependencyInjection
options.TokenLifespan = TimeSpan.FromHours(2);
});
return services;
}
}

View File

@ -8,9 +8,7 @@ public class Alliance : BaseEntity
public required string Abbreviation { get; set; }
public User User { get; set; }
public ICollection<Player> Players { get; set; } = [];
public Guid UserId { get; set; }
public ICollection<Player> Players { get; set; }
public ICollection<User> Users { get; set; } = [];
}

View File

@ -6,8 +6,12 @@ public class Player : BaseEntity
public required Rank Rank { get; set; }
public Guid RankId { get; set; }
public Alliance Alliance { get; set; }
public Guid AllianceId { get; set; }
public required string Level { get; set; }
public ICollection<DesertStorm> DesertStorms { get; set; } = [];

View File

@ -4,7 +4,5 @@ public class Rank : BaseEntity
{
public required string Name { get; set; }
public Guid PlayerId { get; set; }
public required Player Player { get; set; }
public ICollection<Player> Players { get; set; } = [];
}

View File

@ -6,5 +6,7 @@ public class User : IdentityUser<Guid>
{
public required Alliance Alliance { get; set; }
public Guid AllianceId { get; set; }
public required string PlayerName { get; set; }
}

View File

@ -3,11 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35122.118
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Database", "Database\Database.csproj", "{93C305BF-7225-492E-9FBA-D2DD9B0698DF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Database", "Database\Database.csproj", "{93C305BF-7225-492E-9FBA-D2DD9B0698DF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utilities", "Utilities\Utilities.csproj", "{FA8FEE7C-58A5-458F-A7D5-509D9364159B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utilities", "Utilities\Utilities.csproj", "{FA8FEE7C-58A5-458F-A7D5-509D9364159B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "Api\Api.csproj", "{B3CFE8A6-2BA3-4CB2-893F-3F2B0FABB08F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api", "Api\Api.csproj", "{B3CFE8A6-2BA3-4CB2-893F-3F2B0FABB08F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Application", "Application\Application.csproj", "{BFE66A98-A2BC-4B8C-803E-610705855E7A}"
EndProject
Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "Ui", "Ui\Ui.esproj", "{77168004-AF1E-4F51-A096-E8E8BF18A37A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -27,6 +31,16 @@ Global
{B3CFE8A6-2BA3-4CB2-893F-3F2B0FABB08F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3CFE8A6-2BA3-4CB2-893F-3F2B0FABB08F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3CFE8A6-2BA3-4CB2-893F-3F2B0FABB08F}.Release|Any CPU.Build.0 = Release|Any CPU
{BFE66A98-A2BC-4B8C-803E-610705855E7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFE66A98-A2BC-4B8C-803E-610705855E7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFE66A98-A2BC-4B8C-803E-610705855E7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFE66A98-A2BC-4B8C-803E-610705855E7A}.Release|Any CPU.Build.0 = Release|Any CPU
{77168004-AF1E-4F51-A096-E8E8BF18A37A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77168004-AF1E-4F51-A096-E8E8BF18A37A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77168004-AF1E-4F51-A096-E8E8BF18A37A}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{77168004-AF1E-4F51-A096-E8E8BF18A37A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77168004-AF1E-4F51-A096-E8E8BF18A37A}.Release|Any CPU.Build.0 = Release|Any CPU
{77168004-AF1E-4F51-A096-E8E8BF18A37A}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE