修改初始数据

This commit is contained in:
2024-10-13 22:09:08 +08:00
parent 8845fb6521
commit e94a8bbfd2
8 changed files with 405 additions and 9 deletions
+12
View File
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "8.0.10",
"commands": [
"dotnet-ef"
]
}
}
}
@@ -0,0 +1,100 @@
using LMS.DAO;
using LMS.Repository.Models.DB;
using LMS.Tools.Extensions;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using static LMS.Common.Enums.ResponseCodeEnum;
namespace LMS.service.Configuration.InitConfiguration;
/// <summary>
/// 检查数据库中的一些操作
/// </summary>
/// <param name="serviceProvider"></param>
public class DatabaseConfiguration(IServiceProvider serviceProvider) : IHostedService
{
private readonly IServiceProvider _serviceProvider = serviceProvider;
public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _serviceProvider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<User>>();
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<Role>>();
using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
try
{
// 判断数据库中是不是超级管理员,没有创建一个
// 要是没有用户,默认没有
User? user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserName == "admin", cancellationToken: cancellationToken);
if (user != null)
{
return;
}
User admin = new()
{
UserName = "admin",
NickName = "admin",
Email = "admin@admin.com",
AllDeviceCount = 1,
AgentPercent = 0.50,
FreeCount = 10,
CreatedDate = BeijingTimeExtension.GetBeijingTime(),
UpdatedDate = BeijingTimeExtension.GetBeijingTime(),
};
var result = await userManager.CreateAsync(admin, "Admin123.");
if (!result.Succeeded)
{
foreach (var s in result.Errors)
{
Console.WriteLine(s.Description);
return;
}
}
List<string> roleNames =
[
"Super Admin",
"Admin",
"Agent User",
"VIP User",
"Simple User"
];
for (int i = 0; i < roleNames.Count; i++)
{
// 开始添加一些默认数据。添加角色
if (await roleManager.FindByNameAsync(roleNames[i]) == null)
{
var indentiyResult = await roleManager.CreateAsync(new Role
{
Name = roleNames[i],
CreatedUserId = admin.Id,
UpdatedUserId = admin.Id,
CreatedTime = BeijingTimeExtension.GetBeijingTime(),
UpdatedTime = BeijingTimeExtension.GetBeijingTime(),
Remark = string.Empty,
});
Console.WriteLine(indentiyResult.Succeeded);
}
}
// 判断判断admin是不是超级管理员,不是的话,添加
if (!await userManager.IsInRoleAsync(admin, "Super Admin"))
{
await userManager.AddToRoleAsync(admin, "Super Admin");
}
await transaction.CommitAsync(cancellationToken);
}
catch (Exception ex)
{
await transaction.RollbackAsync(cancellationToken);
// 根据需要处理异常,例如终止应用程序
Console.WriteLine(ex.Message);
Environment.Exit(1);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
@@ -1,16 +1,14 @@

using LMS.Common.RSAKey;
using LMS.Common.RSAKey;
using LMS.DAO;
using LMS.Repository.DB;
using LMS.Tools.Extensions;
using Microsoft.EntityFrameworkCore;
namespace LMS.service.Configuration
namespace LMS.service.Configuration.InitConfiguration
{
public class RsaConfigurattions(IServiceProvider serviceProvider) : IHostedService
{
private readonly IServiceProvider _serviceProvider = serviceProvider;
public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _serviceProvider.CreateScope();
@@ -2,7 +2,7 @@
using LMS.DAO.PermissionDAO;
using LMS.DAO.RoleDAO;
using LMS.DAO.UserDAO;
using LMS.service.Configuration;
using LMS.service.Configuration.InitConfiguration;
using LMS.service.Service;
using LMS.service.Service.PermissionService;
using LMS.service.Service.RoleService;
@@ -16,6 +16,7 @@ namespace Lai_server.Configuration
{
// 注入Service
services.AddScoped<RsaConfigurattions>();
services.AddScoped<DatabaseConfiguration>();
// 注入DDL
services.AddScoped<LoginService>();
+2 -1
View File
@@ -199,7 +199,7 @@ namespace LMS.service.Controllers
}
#endregion
#region
#region
[HttpPost]
[Authorize]
@@ -208,6 +208,7 @@ namespace LMS.service.Controllers
long requestUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
return await _loginService.EnableAgent(requestUserId);
}
#endregion
#region
+2
View File
@@ -2,6 +2,7 @@ using Lai_server.Configuration;
using LMS.DAO;
using LMS.Repository.Models.DB;
using LMS.service.Configuration;
using LMS.service.Configuration.InitConfiguration;
using LMS.service.Extensions.Middleware;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
@@ -70,6 +71,7 @@ builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerService();
builder.Services.AddHostedService<RsaConfigurattions>();
builder.Services.AddHostedService<DatabaseConfiguration>();
var app = builder.Build();