This commit is contained in:
ai.astrakhantsev 2022-10-27 15:44:04 +05:00
parent 950e7ad02a
commit 41d9e506bd
2 changed files with 52 additions and 45 deletions

View File

@ -5,6 +5,7 @@ using AsbCloudDb;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.EfCache; using AsbCloudInfrastructure.EfCache;
using Mapster; using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -57,9 +58,10 @@ namespace AsbCloudInfrastructure.Repository
public async Task<IEnumerable<UserExtendedDto>> GetAllAsync(CancellationToken token = default) public async Task<IEnumerable<UserExtendedDto>> GetAllAsync(CancellationToken token = default)
{ {
var entities = await GetCacheUserAsync(token); var dtos = (await GetCacheUserAsync(token)).ToList();
if (dtos is null)
return Enumerable.Empty<UserExtendedDto>();
var dtos = entities.Select(Convert).ToList();
for (var i = 0; i < dtos.Count; i++) for (var i = 0; i < dtos.Count; i++)
dtos[i].RoleNames = GetRolesNamesByIdUser(dtos[i].Id); dtos[i].RoleNames = GetRolesNamesByIdUser(dtos[i].Id);
return dtos; return dtos;
@ -67,20 +69,20 @@ namespace AsbCloudInfrastructure.Repository
public UserExtendedDto? GetOrDefault(int id) public UserExtendedDto? GetOrDefault(int id)
{ {
var entity = GetCacheUser().FirstOrDefault(u => u.Id == id); var dto = GetCacheUser().FirstOrDefault(u => u.Id == id);
if (entity is null) if (dto is null)
return null; return null;
var dto = Convert(entity); var entity = Convert(dto);
dto.RoleNames = GetRolesNamesByIdUser(dto.Id); dto.RoleNames = GetRolesNamesByIdUser(dto.Id);
return dto; return dto;
} }
public async Task<UserExtendedDto?> GetOrDefaultAsync(int id, CancellationToken token = default) public async Task<UserExtendedDto?> GetOrDefaultAsync(int id, CancellationToken token = default)
{ {
var entity = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id); var dto = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id);
if (entity is null) if (dto is null)
return null; return null;
var dto = Convert(entity);
dto.RoleNames = GetRolesNamesByIdUser(dto.Id); dto.RoleNames = GetRolesNamesByIdUser(dto.Id);
return dto; return dto;
} }
@ -112,10 +114,11 @@ namespace AsbCloudInfrastructure.Repository
public async Task<int> DeleteAsync(int id, CancellationToken token = default) public async Task<int> DeleteAsync(int id, CancellationToken token = default)
{ {
var entity = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id); var dto = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id);
if (entity is null) if (dto is null)
return 0; return 0;
var entity = Convert(dto);
var result = dbContext.Users.Remove(entity); var result = dbContext.Users.Remove(entity);
await dbContext.SaveChangesAsync(token); await dbContext.SaveChangesAsync(token);
DropCacheUsers(); DropCacheUsers();
@ -124,11 +127,12 @@ namespace AsbCloudInfrastructure.Repository
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default) public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
{ {
var entities = (await GetCacheUserAsync(token)).Where(r => ids.Contains(r.Id)); var dto = (await GetCacheUserAsync(token)).Where(r => ids.Contains(r.Id));
if (entities is null) if (dto is null)
return 0; return 0;
var count = entities.Count(); var count = dto.Count();
var entities = dto.Select(Convert);
dbContext.Users.RemoveRange(entities); dbContext.Users.RemoveRange(entities);
await dbContext.SaveChangesAsync(token); await dbContext.SaveChangesAsync(token);
DropCacheUsers(); DropCacheUsers();
@ -177,22 +181,23 @@ namespace AsbCloudInfrastructure.Repository
private async Task AssertLoginIsBusyAsync(string login, CancellationToken token = default) private async Task AssertLoginIsBusyAsync(string login, CancellationToken token = default)
{ {
var existingUser = (await GetCacheUserAsync(token)) var existingUserDto = (await GetCacheUserAsync(token))
.FirstOrDefault(u => u.Login.ToLower() == login.ToLower()); .FirstOrDefault(u => u.Login.ToLower() == login.ToLower());
var existingUser = Convert(existingUserDto);
if (existingUser is not null) if (existingUser is not null)
throw new ArgumentInvalidException($"Login {login} is busy by {existingUser.MakeDisplayName()}, id{existingUser.Id}", nameof(login)); throw new ArgumentInvalidException($"Login {login} is busy by {existingUser.MakeDisplayName()}, id{existingUser.Id}", nameof(login));
} }
private Task<IEnumerable<User>> GetCacheUserAsync(CancellationToken token) private Task<IEnumerable<UserExtendedDto>> GetCacheUserAsync(CancellationToken token)
=> dbContext.Users => dbContext.Users
.Include(r => r.Company) .Include(r => r.Company)
.Include(r => r.RelationUsersUserRoles) .Include(r => r.RelationUsersUserRoles)
.FromCacheAsync(userCacheTag, cacheObsolence, token); .FromCacheAsync(userCacheTag, cacheObsolence, Convert, token);
private IEnumerable<User> GetCacheUser() private IEnumerable<UserExtendedDto> GetCacheUser()
=> dbContext.Users => dbContext.Users
.Include(r => r.Company) .Include(r => r.Company)
.Include(r => r.RelationUsersUserRoles) .Include(r => r.RelationUsersUserRoles)
.FromCache(userCacheTag, cacheObsolence); .FromCache(userCacheTag, cacheObsolence, Convert);
private void DropCacheUsers() private void DropCacheUsers()
=> dbContext.Users.DropCache(userCacheTag); => dbContext.Users.DropCache(userCacheTag);

View File

@ -50,33 +50,30 @@ namespace AsbCloudInfrastructure.Repository
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token) public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token)
{ {
var entities = await GetCacheUserRoleAsync(token) var dtos = await GetCacheUserRoleAsync(token)
.ConfigureAwait(false); .ConfigureAwait(false);
if (entities is null) if (dtos is null)
return Enumerable.Empty<UserRoleDto>(); return Enumerable.Empty<UserRoleDto>();
var dtos = entities.Select(Convert);
return dtos; return dtos;
} }
public UserRoleDto? GetOrDefault(int id) public UserRoleDto? GetOrDefault(int id)
{ {
var entity = GetCacheUserRole().FirstOrDefault(x => x.Id == id); var dto = GetCacheUserRole().FirstOrDefault(x => x.Id == id);
if (entity is null) if (dto is null)
return null; return null;
var dto = Convert(entity);
return dto; return dto;
} }
public async Task<UserRoleDto?> GetOrDefaultAsync(int id, CancellationToken token) public async Task<UserRoleDto?> GetOrDefaultAsync(int id, CancellationToken token)
{ {
var entity = (await GetCacheUserRoleAsync(token) var dto = (await GetCacheUserRoleAsync(token)
.ConfigureAwait(false)).FirstOrDefault(r => r.Id == id); .ConfigureAwait(false)).FirstOrDefault(r => r.Id == id);
if (entity is null) if (dto is null)
return null; return null;
var dto = Convert(entity);
return dto; return dto;
} }
@ -85,13 +82,12 @@ namespace AsbCloudInfrastructure.Repository
if (names?.Any() != true) if (names?.Any() != true)
return Enumerable.Empty<UserRoleDto>(); return Enumerable.Empty<UserRoleDto>();
var entities = (await GetCacheUserRoleAsync(token)) var dtos = (await GetCacheUserRoleAsync(token))
.Where(r => names.Contains(r.Caption)); .Where(r => names.Contains(r.Caption));
if (entities?.Count() != names.Count()) if (dtos?.Count() != names.Count())
throw new ArgumentInvalidException("Invalid role names", nameof(names)); throw new ArgumentInvalidException("Invalid role names", nameof(names));
var dtos = entities.Select(Convert);
return dtos; return dtos;
} }
@ -109,11 +105,11 @@ namespace AsbCloudInfrastructure.Repository
public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7) public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7)
{ {
var role = GetCacheUserRole().FirstOrDefault(r => r.Id == id); var dto = GetCacheUserRole().FirstOrDefault(r => r.Id == id);
if (role is null) if (dto is null)
return Enumerable.Empty<UserRoleDto>(); return Enumerable.Empty<UserRoleDto>();
var role = Convert(dto);
var dto = Convert(role);
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto }; var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto };
if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true) if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true)
@ -132,10 +128,11 @@ namespace AsbCloudInfrastructure.Repository
public async Task<int> DeleteAsync(int id, CancellationToken token) public async Task<int> DeleteAsync(int id, CancellationToken token)
{ {
var entity = (await GetCacheUserRoleAsync(token)).FirstOrDefault(r => r.Id == id); var dto = (await GetCacheUserRoleAsync(token)).FirstOrDefault(r => r.Id == id);
if (entity is not null) if (dto is not null)
{ {
var entity = Convert(dto);
var removeEntity = dbContext.UserRoles.Remove(entity); var removeEntity = dbContext.UserRoles.Remove(entity);
await dbContext.SaveChangesAsync(token); await dbContext.SaveChangesAsync(token);
DropCacheUserRole(); DropCacheUserRole();
@ -146,10 +143,11 @@ namespace AsbCloudInfrastructure.Repository
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token) public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
{ {
var entities = (await GetCacheUserRoleAsync(token)).Where(r => ids.Contains(r.Id)); var dtos = (await GetCacheUserRoleAsync(token)).Where(r => ids.Contains(r.Id));
if (entities is not null) if (dtos is not null)
{ {
var entities = dtos.Select(Convert);
var count = entities.Count(); var count = entities.Count();
dbContext.UserRoles.RemoveRange(entities); dbContext.UserRoles.RemoveRange(entities);
await dbContext.SaveChangesAsync(token); await dbContext.SaveChangesAsync(token);
@ -172,8 +170,10 @@ namespace AsbCloudInfrastructure.Repository
return true; return true;
var idPermissionInfo = permissionInfo.Id; var idPermissionInfo = permissionInfo.Id;
var roles = GetCacheUserRole() var dtos = GetCacheUserRole()
.Where(r => rolesIds.Contains(r.Id)); .Where(r => rolesIds.Contains(r.Id));
var roles = dtos.Select(Convert);
foreach (var role in roles) foreach (var role in roles)
if (HasPermission(role, idPermissionInfo)) if (HasPermission(role, idPermissionInfo))
return true; return true;
@ -190,8 +190,9 @@ namespace AsbCloudInfrastructure.Repository
foreach (var relation in userRole.RelationUserRoleUserRoles) foreach (var relation in userRole.RelationUserRoleUserRoles)
{ {
var includedRole = GetCacheUserRole() var dto = GetCacheUserRole()
.First(p => p.Id == relation.IdInclude); .First(p => p.Id == relation.IdInclude);
var includedRole = Convert(dto);
if (HasPermission(includedRole, idPermission, --recursionLevel)) if (HasPermission(includedRole, idPermission, --recursionLevel))
return true; return true;
} }
@ -241,16 +242,16 @@ namespace AsbCloudInfrastructure.Repository
} }
} }
private Task<IEnumerable<UserRole>> GetCacheUserRoleAsync(CancellationToken token) private Task<IEnumerable<UserRoleDto>> GetCacheUserRoleAsync(CancellationToken token)
=> dbContext.UserRoles => dbContext.UserRoles
.Include(r => r.RelationUserRolePermissions) .Include(r => r.RelationUserRolePermissions)
.Include(r => r.RelationUserRoleUserRoles) .Include(r => r.RelationUserRoleUserRoles)
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, token); .FromCacheAsync(userRoleCacheTag, relationCacheObsolence, Convert, token);
private IEnumerable<UserRole> GetCacheUserRole() private IEnumerable<UserRoleDto> GetCacheUserRole()
=> dbContext.UserRoles => dbContext.UserRoles
.Include(r => r.RelationUserRolePermissions) .Include(r => r.RelationUserRolePermissions)
.Include(r => r.RelationUserRoleUserRoles) .Include(r => r.RelationUserRoleUserRoles)
.FromCache(userRoleCacheTag, relationCacheObsolence); .FromCache(userRoleCacheTag, relationCacheObsolence, Convert);
private void DropCacheUserRole() private void DropCacheUserRole()
=> dbContext.RelationUserUserRoles.DropCache(relationUserRoleUserRoleCacheTag); => dbContext.RelationUserUserRoles.DropCache(relationUserRoleUserRoleCacheTag);
@ -294,9 +295,10 @@ namespace AsbCloudInfrastructure.Repository
{ {
dto.Roles = entity.RelationUserRoleUserRoles.Select(rel => dto.Roles = entity.RelationUserRoleUserRoles.Select(rel =>
{ {
var includedRole = GetCacheUserRole().First(r => r.Id == rel.IdInclude); var dto = GetCacheUserRole().First(r => r.Id == rel.IdInclude);
var includedRole = Convert(dto);
return Convert(includedRole); return Convert(includedRole);
}).ToList(); }).ToArray();
} }
return dto; return dto;
} }