first commit
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Roles;
|
||||
|
||||
internal class RolePermissionWriteRepository : F9RepositoryBase, IRolePermissionWriteRepository
|
||||
{
|
||||
public RolePermissionWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<RolePermission> GetByIdAsync(long id)
|
||||
=> await Orm.Select<RolePermission>().Where(x => x.Id == id).FirstAsync();
|
||||
|
||||
public async Task<bool> AssignAsync(RolePermission rolePermission)
|
||||
{
|
||||
rolePermission.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(rolePermission).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveAsync(long id)
|
||||
=> await Orm.Delete<RolePermission>().Where(x => x.Id == id).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Roles;
|
||||
|
||||
internal class RoleReadRepository : F9RepositoryBase, IRoleReadRepository
|
||||
{
|
||||
public RoleReadRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<RoleReadDto_Edit> GetByIdAsync(long id)
|
||||
=> await Orm.Select<Role>()
|
||||
.Where(x => x.Id == id && x.DeletedAt == null)
|
||||
.FirstAsync(x => new RoleReadDto_Edit(x.Id, x.Name, x.Code, x.SortOrder, x.Description));
|
||||
|
||||
public async Task<List<RoleReadDto_View>> GetAllAsync()
|
||||
{
|
||||
var roles = await Orm.Select<Role>()
|
||||
.Where(x => x.DeletedAt == null)
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ToListAsync(x => new { x.Id, x.Name, x.Code, x.SortOrder, x.IsActive });
|
||||
|
||||
var roleIds = roles.Select(x => x.Id).ToList();
|
||||
var permCounts = await GetPermissionCountsAsync(roleIds);
|
||||
|
||||
return roles.Select(x =>
|
||||
{
|
||||
var c = permCounts.GetValueOrDefault(x.Id);
|
||||
return new RoleReadDto_View(x.Id, x.Name, x.Code, x.SortOrder, c.Menu, c.Report, c.Dep, x.IsActive);
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<RoleReadDto_Cache>> GetAllForCacheAsync()
|
||||
=> await Orm.Select<Role>()
|
||||
.Where(x => x.DeletedAt == null)
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ToListAsync(x => new RoleReadDto_Cache(x.Id, x.Name, x.Code, x.SortOrder));
|
||||
|
||||
public async Task<List<RolePermissionReadDto_View>> GetRolePermissionsByResourceType(long roleId, Resource_Type_Enum resourceType)
|
||||
=> await Orm.Select<RolePermission, Permission>()
|
||||
.InnerJoin((rp, p) => rp.PermissionId == p.Id && p.DeletedAt == null)
|
||||
.Where((rp, p) => rp.RoleId == roleId && p.ResourceType == resourceType)
|
||||
.ToListAsync((rp, p) => new RolePermissionReadDto_View(rp.Id, rp.RoleId, rp.PermissionId, p.ResourceId, p.PermissionType));
|
||||
|
||||
private async Task<Dictionary<long, (int Menu, int Report, int Dep)>> GetPermissionCountsAsync(List<long> roleIds)
|
||||
{
|
||||
var result = new Dictionary<long, (int, int, int)>();
|
||||
if (roleIds.Count == 0) return result;
|
||||
|
||||
var perms = await Orm.Select<RolePermission, Permission>()
|
||||
.InnerJoin((rp, p) => rp.PermissionId == p.Id && p.DeletedAt == null)
|
||||
.Where((rp, p) => roleIds.Contains(rp.RoleId))
|
||||
.ToListAsync((rp, p) => new { rp.RoleId, p.ResourceType });
|
||||
|
||||
foreach (var g in perms.GroupBy(x => x.RoleId))
|
||||
{
|
||||
result[g.Key] = (
|
||||
g.Count(x => x.ResourceType == Resource_Type_Enum.菜单),
|
||||
g.Count(x => x.ResourceType == Resource_Type_Enum.报表),
|
||||
g.Count(x => x.ResourceType == Resource_Type_Enum.部门));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Roles;
|
||||
|
||||
internal class RoleWriteRepository : F9RepositoryBase, IRoleWriteRepository
|
||||
{
|
||||
public RoleWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<Role> GetByIdAsync(long id)
|
||||
=> await Orm.Select<Role>().Where(x => x.Id == id ).FirstAsync();
|
||||
|
||||
public async Task<bool> AddAsync(Role role)
|
||||
{
|
||||
role.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(role).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(Role role)
|
||||
{
|
||||
role.UpdatedAt = DateTime.Now;
|
||||
return await Orm.Update<Role>().SetSource(role).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SoftDeleteAsync(long id)
|
||||
=> await Orm.Update<Role>()
|
||||
.Set(x => x.IsDeleted, true)
|
||||
.Where(x => x.Id == id )
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> ActiveOrDeactive(long id, bool active)
|
||||
=> await Orm.Update<Role>()
|
||||
.Set(x => x.IsActive, active)
|
||||
.Set(x => x.UpdatedAt, DateTime.Now)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user