refactor: 更新 AppService 服务使用 DBManagerService

This commit is contained in:
Argo-Lenovo 2022-06-02 10:17:15 +08:00
parent 09bf567698
commit b96aab631f
2 changed files with 15 additions and 10 deletions

View File

@ -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)

View File

@ -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));