refactor: 重构服务
This commit is contained in:
parent
7eb06c1df6
commit
889f03189d
|
@ -44,7 +44,7 @@ public class GroupService : IGroup, IDisposable
|
|||
{
|
||||
using var dbcontext = DbFactory.CreateDbContext();
|
||||
|
||||
return CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => dbcontext.Groups.ToList());
|
||||
return CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => dbcontext.Groups.AsNoTracking().ToList());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services
|
|||
return CacheManager.GetOrAdd($"{nameof(NavigationService)}-{nameof(GetAllMenus)}-{userName}", entry =>
|
||||
{
|
||||
using var context = DbFactory.CreateDbContext();
|
||||
return context.Set<Navigation>().FromSqlRaw("select n.ID, n.ParentId, n.Name, n.[order], n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application from Navigations n inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = {0} union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = {0} union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = {0} and r.RoleName = {1})) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.[order]", new[] { userName, "Administrators" }).ToList();
|
||||
return context.Set<Navigation>().FromSqlRaw("select n.ID, n.ParentId, n.Name, n.[order], n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application from Navigations n inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = {0} union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = {0} union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = {0} and r.RoleName = {1})) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.[order]", new[] { userName, "Administrators" }).AsNoTracking().ToList();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services
|
|||
{
|
||||
using var context = DbFactory.CreateDbContext();
|
||||
|
||||
return context.NavigationRole.Where(s => s.RoleId == roleId).Select(s => s.NavigationId!).ToList();
|
||||
return context.NavigationRole.Where(s => s.RoleId == roleId).Select(s => s.NavigationId!).AsNoTracking().ToList();
|
||||
}
|
||||
|
||||
public bool SaveMenusByRoleId(string? roleId, List<string> menuIds)
|
||||
|
|
|
@ -61,7 +61,7 @@ public class RoleService : IRole
|
|||
{
|
||||
using var dbcontext = DbFactory.CreateDbContext();
|
||||
|
||||
return dbcontext.RoleGroup.Where(s => s.GroupId == groupId).Select(s => s.RoleId!).ToList();
|
||||
return dbcontext.RoleGroup.Where(s => s.GroupId == groupId).Select(s => s.RoleId!).AsNoTracking().ToList();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ public class RoleService : IRole
|
|||
{
|
||||
using var dbcontext = DbFactory.CreateDbContext();
|
||||
|
||||
return dbcontext.NavigationRole.Where(s => s.NavigationId == menuId).Select(s => s.RoleId!).ToList();
|
||||
return dbcontext.NavigationRole.Where(s => s.NavigationId == menuId).Select(s => s.RoleId!).AsNoTracking().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -88,7 +88,7 @@ public class RoleService : IRole
|
|||
{
|
||||
using var dbcontext = DbFactory.CreateDbContext();
|
||||
|
||||
return dbcontext.UserRole.Where(s => s.UserId == userId).Select(s => s.RoleId!).ToList();
|
||||
return dbcontext.UserRole.Where(s => s.UserId == userId).Select(s => s.RoleId!).AsNoTracking().ToList();
|
||||
});
|
||||
|
||||
|
||||
|
@ -154,7 +154,7 @@ public class RoleService : IRole
|
|||
var ret = false;
|
||||
try
|
||||
{
|
||||
dbcontext.Database.ExecuteSqlRaw("delete from RoleGroup where GroupID = {0}", userId!);
|
||||
dbcontext.Database.ExecuteSqlRaw("delete from UserRole where UserID = {0}", userId!);
|
||||
dbcontext.AddRange(roleIds.Select(g => new UserRole { RoleId = g, UserId = userId }));
|
||||
dbcontext.SaveChanges();
|
||||
ret = true;
|
||||
|
|
|
@ -31,7 +31,7 @@ public class UserService : IUser
|
|||
public List<User> GetAll()
|
||||
{
|
||||
using var context = DbFactory.CreateDbContext();
|
||||
return context.Users.ToList();
|
||||
return context.Users.AsNoTracking().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -62,7 +62,7 @@ public class UserService : IUser
|
|||
public List<string> GetApps(string userName)
|
||||
{
|
||||
using var context = DbFactory.CreateDbContext();
|
||||
return context.Dicts.FromSqlRaw("select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = {0} union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join [Groups] g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = {0}) r on ra.RoleId = r.ID union select Code from Dicts where Category = {1} and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = {0} and r.RoleName = {2} union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join [Groups] g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = {0} and r.RoleName = {2})", new[] { userName, "应用程序", "Administrators" }).Select(s => s.Code).ToList();
|
||||
return context.Dicts.FromSqlRaw("select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = {0} union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join [Groups] g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = {0}) r on ra.RoleId = r.ID union select Code from Dicts where Category = {1} and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = {0} and r.RoleName = {2} union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join [Groups] g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = {0} and r.RoleName = {2})", new[] { userName, "应用程序", "Administrators" }).Select(s => s.Code).AsNoTracking().ToList();
|
||||
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ public class UserService : IUser
|
|||
{
|
||||
using var context = DbFactory.CreateDbContext();
|
||||
|
||||
return context.UserRole.FromSqlRaw("select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = {0} union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join [Groups] g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName = {0}", userName).Select(s => s.RoleId!).ToList();
|
||||
return context.Roles.FromSqlRaw("select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = {0} union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join [Groups] g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName = {0}", userName).Select(s => s.RoleName).AsNoTracking().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -98,7 +98,7 @@ public class UserService : IUser
|
|||
{
|
||||
using var context = DbFactory.CreateDbContext();
|
||||
|
||||
return context.UserGroup.Where(s => s.GroupId == groupId).Select(s => s.UserId!).ToList();
|
||||
return context.UserGroup.Where(s => s.GroupId == groupId).Select(s => s.UserId!).AsNoTracking().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -110,7 +110,7 @@ public class UserService : IUser
|
|||
{
|
||||
using var context = DbFactory.CreateDbContext();
|
||||
|
||||
return context.UserRole.Where(s => s.RoleId == roleId).Select(s => s.UserId!).ToList();
|
||||
return context.UserRole.Where(s => s.RoleId == roleId).Select(s => s.UserId!).AsNoTracking().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Reference in New Issue