#7205798 Перенос сервиса пользователей и ролей в репозиторий

This commit is contained in:
ai.astrakhantsev 2022-11-07 13:53:10 +05:00
parent 4a2a94ddfb
commit 1348b1090b
2 changed files with 23 additions and 22 deletions

View File

@ -152,7 +152,7 @@ namespace AsbCloudInfrastructure.Repository
{
var roles = GetRolesByIdUser(idUser, 7);
if (roles is null)
return Enumerable.Empty<PermissionDto>(); ;
return Enumerable.Empty<PermissionDto>();
var permissions = roles
.Where(r => r.Permissions is not null)
.SelectMany(r => r.Permissions);
@ -175,7 +175,7 @@ namespace AsbCloudInfrastructure.Repository
}
private IEnumerable<string>? GetRolesNamesByIdUser(int idUser)
=> GetRolesByIdUser(idUser)
=> GetRolesByIdUser(idUser, 7)
?.Select(r => r.Caption)
.Distinct();

View File

@ -5,6 +5,7 @@ using AsbCloudApp.Repositories;
using AsbCloudDb;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.EfCache;
using DocumentFormat.OpenXml.Drawing;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
@ -56,7 +57,7 @@ namespace AsbCloudInfrastructure.Repository
if (dtos is null)
return Enumerable.Empty<UserRoleDto>();
return dtos;
return dtos.Select(Convert);
}
@ -65,7 +66,7 @@ namespace AsbCloudInfrastructure.Repository
var dto = GetCacheUserRole().FirstOrDefault(x => x.Id == id);
if (dto is null)
return null;
return dto;
return Convert(dto);
}
public async Task<UserRoleDto?> GetOrDefaultAsync(int id, CancellationToken token)
@ -74,7 +75,7 @@ namespace AsbCloudInfrastructure.Repository
.ConfigureAwait(false)).FirstOrDefault(r => r.Id == id);
if (dto is null)
return null;
return dto;
return Convert(dto);
}
public async Task<IEnumerable<UserRoleDto>> GetByNamesAsync(IEnumerable<string> names, CancellationToken token)
@ -88,7 +89,7 @@ namespace AsbCloudInfrastructure.Repository
if (dtos?.Count() != names.Count())
throw new ArgumentInvalidException("Invalid role names", nameof(names));
return dtos;
return dtos.Select(Convert);
}
public async Task<int> UpdateAsync(UserRoleDto dto, CancellationToken token)
@ -105,13 +106,12 @@ namespace AsbCloudInfrastructure.Repository
public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7)
{
var dto = GetCacheUserRole()
var role = GetCacheUserRole()
.FirstOrDefault(r => r.Id == id);
if (dto is null)
if (role is null)
return Enumerable.Empty<UserRoleDto>();
var role = Convert(dto);
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto };
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { Convert(role) };
if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true)
return roles;
@ -134,7 +134,7 @@ namespace AsbCloudInfrastructure.Repository
if (dto is not null)
{
var entity = Convert(dto);
var removeEntity = dbContext.UserRoles.Remove(entity);
var removeEntity = dbContext.UserRoles.Remove(Convert(entity));
await dbContext.SaveChangesAsync(token);
DropCacheUserRole();
return removeEntity?.Entity?.Id ?? 0;
@ -144,11 +144,10 @@ namespace AsbCloudInfrastructure.Repository
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
{
var dtos = (await GetCacheUserRoleAsync(token)).Where(r => ids.Contains(r.Id));
var entities = (await GetCacheUserRoleAsync(token)).Where(r => ids.Contains(r.Id));
if (dtos is not null)
if (entities is not null)
{
var entities = dtos.Select(Convert);
var count = entities.Count();
dbContext.UserRoles.RemoveRange(entities);
await dbContext.SaveChangesAsync(token);
@ -176,7 +175,7 @@ namespace AsbCloudInfrastructure.Repository
var roles = dtos.Select(Convert);
foreach (var role in roles)
if (HasPermission(role, idPermissionInfo))
if (HasPermission(Convert(role), idPermissionInfo))
return true;
return false;
}
@ -194,7 +193,7 @@ namespace AsbCloudInfrastructure.Repository
var dto = GetCacheUserRole()
.First(p => p.Id == relation.IdInclude);
var includedRole = Convert(dto);
if (HasPermission(includedRole, idPermission, --recursionLevel))
if (HasPermission(Convert(includedRole), idPermission, --recursionLevel))
return true;
}
return false;
@ -243,16 +242,18 @@ namespace AsbCloudInfrastructure.Repository
}
}
private Task<IEnumerable<UserRoleDto>> GetCacheUserRoleAsync(CancellationToken token)
private Task<IEnumerable<UserRole>> GetCacheUserRoleAsync(CancellationToken token)
=> dbContext.UserRoles
.Include(r => r.RelationUserRolePermissions)
.Include(r => r.RelationUserRoleUserRoles)
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, Convert, token);
private IEnumerable<UserRoleDto> GetCacheUserRole()
.Include(r => r.RelationUsersUserRoles)
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, token);
private IEnumerable<UserRole> GetCacheUserRole()
=> dbContext.UserRoles
.Include(r => r.RelationUserRolePermissions)
.Include(r => r.RelationUserRoleUserRoles)
.FromCache(userRoleCacheTag, relationCacheObsolence, Convert);
.Include(r => r.RelationUsersUserRoles)
.FromCache(userRoleCacheTag, relationCacheObsolence);
private void DropCacheUserRole()
=> dbContext.RelationUserUserRoles.DropCache(userRoleCacheTag);
@ -296,8 +297,8 @@ namespace AsbCloudInfrastructure.Repository
{
var rolesCache = GetCacheUserRole();
dto.Roles = entity.RelationUserRoleUserRoles
.Select(rel => rolesCache
.First(r => r.Id == rel.IdInclude))
.Select(rel => Convert(rolesCache
.First(r => r.Id == rel.IdInclude)))
.ToArray();
}
return dto;