refactor: 更新 AppService 服务使用 DBManagerService
This commit is contained in:
parent
09bf567698
commit
b96aab631f
|
@ -12,29 +12,34 @@ class AppService : IApp
|
|||
{
|
||||
private const string AppServiceGetAppsByRoleIdCacheKey = "AppService-GetAppsByRoleId";
|
||||
|
||||
private IDatabase Database { get; }
|
||||
private IDBManager DBManager { get; }
|
||||
|
||||
public AppService(IDatabase db)
|
||||
public AppService(IDBManager db)
|
||||
{
|
||||
Database = db;
|
||||
DBManager = db;
|
||||
}
|
||||
|
||||
public List<string> GetAppsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{AppServiceGetAppsByRoleIdCacheKey}-{roleId}", entry => Database.Fetch<string>("select AppID from RoleApp where RoleID = @0", roleId));
|
||||
public List<string> GetAppsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{AppServiceGetAppsByRoleIdCacheKey}-{roleId}", entry =>
|
||||
{
|
||||
using var db = DBManager.Create();
|
||||
return db.Fetch<string>("select AppID from RoleApp where RoleID = @0", roleId);
|
||||
});
|
||||
|
||||
public bool SaveAppsByRoleId(string? roleId, IEnumerable<string> appIds)
|
||||
{
|
||||
var ret = false;
|
||||
using var db = DBManager.Create();
|
||||
try
|
||||
{
|
||||
Database.BeginTransaction();
|
||||
Database.Execute("delete from RoleApp where RoleID = @0", roleId);
|
||||
Database.InsertBatch("RoleApp", appIds.Select(g => new { AppID = g, RoleID = roleId }));
|
||||
Database.CompleteTransaction();
|
||||
db.BeginTransaction();
|
||||
db.Execute("delete from RoleApp where RoleID = @0", roleId);
|
||||
db.InsertBatch("RoleApp", appIds.Select(g => new { AppID = g, RoleID = roleId }));
|
||||
db.CompleteTransaction();
|
||||
ret = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Database.AbortTransaction();
|
||||
db.AbortTransaction();
|
||||
throw;
|
||||
}
|
||||
if (ret)
|
||||
|
|
|
@ -36,7 +36,7 @@ internal class DBManagerService : IDBManager
|
|||
/// <param name="connectionName">连接字符串键值</param>
|
||||
/// <param name="keepAlive"></param>
|
||||
/// <returns></returns>
|
||||
public IDatabase Create(string? connectionName = "client", bool keepAlive = false)
|
||||
public IDatabase Create(string? connectionName = "ba", bool keepAlive = false)
|
||||
{
|
||||
var conn = Configuration.GetConnectionString(connectionName) ?? throw new ArgumentNullException(nameof(connectionName));
|
||||
|
||||
|
|
Loading…
Reference in New Issue