-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.完善多租户-多库方案 2.增加租户管理 (实际业务中 也是运维、系统管理员等角色来操作 甚至直接维护数据库而不会开放接口)
- Loading branch information
1 parent
2962019
commit d85087c
Showing
8 changed files
with
241 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
Blog.Core.Api/Controllers/Tenant/TenantManagerController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using Blog.Core.Controllers; | ||
using Blog.Core.IServices; | ||
using Blog.Core.Model; | ||
using Blog.Core.Model.Models; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Blog.Core.Api.Controllers.Tenant; | ||
|
||
/// <summary> | ||
/// 租户管理 | ||
/// </summary> | ||
[Produces("application/json")] | ||
[Route("api/TenantManager")] | ||
[Authorize] | ||
public class TenantManagerController : BaseApiController | ||
{ | ||
private readonly ITenantService _services; | ||
|
||
public TenantManagerController(ITenantService services) | ||
{ | ||
_services = services; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 获取全部租户 | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet] | ||
public async Task<MessageModel<List<SysTenant>>> GetAll() | ||
{ | ||
var data = await _services.Query(); | ||
return Success(data); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 获取租户信息 | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet("{id}")] | ||
public async Task<MessageModel<SysTenant>> GetInfo(long id) | ||
{ | ||
var data = await _services.QueryById(id); | ||
return Success(data); | ||
} | ||
|
||
/// <summary> | ||
/// 新增租户信息 <br/> | ||
/// 此处只做演示,具体要以实际业务为准 | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public async Task<MessageModel> Post(SysTenant tenant) | ||
{ | ||
await _services.SaveTenant(tenant); | ||
return Success(); | ||
} | ||
|
||
/// <summary> | ||
/// 修改租户信息 <br/> | ||
/// 此处只做演示,具体要以实际业务为准 | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpPut] | ||
public async Task<MessageModel> Put(SysTenant tenant) | ||
{ | ||
await _services.SaveTenant(tenant); | ||
return Success(); | ||
} | ||
|
||
/// <summary> | ||
/// 删除租户 <br/> | ||
/// 此处只做演示,具体要以实际业务为准 | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpDelete] | ||
public async Task<MessageModel> Delete(long id) | ||
{ | ||
//是否删除租户库? | ||
//要根据实际情况而定 | ||
//例如直接删除租户库、备份租户库到xx | ||
await _services.DeleteById(id); | ||
return Success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Threading.Tasks; | ||
using Blog.Core.IServices.BASE; | ||
using Blog.Core.Model.Models; | ||
|
||
namespace Blog.Core.IServices; | ||
|
||
public interface ITenantService : IBaseServices<SysTenant> | ||
{ | ||
public Task SaveTenant(SysTenant tenant); | ||
|
||
public Task InitTenantDb(SysTenant tenant); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Blog.Core.Common.DB; | ||
using Blog.Core.Common.Seed; | ||
using Blog.Core.IServices; | ||
using Blog.Core.Model.Models; | ||
using Blog.Core.Repository.UnitOfWorks; | ||
using Blog.Core.Services.BASE; | ||
using System.Threading.Tasks; | ||
|
||
namespace Blog.Core.Services; | ||
|
||
public class TenantService : BaseServices<SysTenant>, ITenantService | ||
{ | ||
private readonly IUnitOfWorkManage _uowManager; | ||
|
||
public TenantService(IUnitOfWorkManage uowManage) | ||
{ | ||
this._uowManager = uowManage; | ||
} | ||
|
||
|
||
public async Task SaveTenant(SysTenant tenant) | ||
{ | ||
bool initDb = tenant.Id == 0; | ||
using (var uow = _uowManager.CreateUnitOfWork()) | ||
{ | ||
|
||
tenant.DefaultTenantConfig(); | ||
|
||
if (tenant.Id == 0) | ||
{ | ||
await Db.Insertable(tenant).ExecuteReturnSnowflakeIdAsync(); | ||
} | ||
else | ||
{ | ||
var oldTenant = await QueryById(tenant.Id); | ||
if (oldTenant.Connection != tenant.Connection) | ||
{ | ||
initDb = true; | ||
} | ||
|
||
await Db.Updateable(tenant).ExecuteCommandAsync(); | ||
} | ||
|
||
uow.Commit(); | ||
} | ||
|
||
if (initDb) | ||
{ | ||
await InitTenantDb(tenant); | ||
} | ||
} | ||
|
||
public async Task InitTenantDb(SysTenant tenant) | ||
{ | ||
await DBSeed.InitTenantSeedAsync(Db.AsTenant(), tenant.GetConnectionConfig()); | ||
} | ||
} |