forked from ddrilling/AsbCloudServer
#7205798 fix cache dto
This commit is contained in:
parent
950e7ad02a
commit
41d9e506bd
@ -5,6 +5,7 @@ using AsbCloudDb;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.EfCache;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -57,9 +58,10 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
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++)
|
||||
dtos[i].RoleNames = GetRolesNamesByIdUser(dtos[i].Id);
|
||||
return dtos;
|
||||
@ -67,20 +69,20 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
public UserExtendedDto? GetOrDefault(int id)
|
||||
{
|
||||
var entity = GetCacheUser().FirstOrDefault(u => u.Id == id);
|
||||
if (entity is null)
|
||||
var dto = GetCacheUser().FirstOrDefault(u => u.Id == id);
|
||||
if (dto is null)
|
||||
return null;
|
||||
var dto = Convert(entity);
|
||||
var entity = Convert(dto);
|
||||
dto.RoleNames = GetRolesNamesByIdUser(dto.Id);
|
||||
return dto;
|
||||
}
|
||||
|
||||
public async Task<UserExtendedDto?> GetOrDefaultAsync(int id, CancellationToken token = default)
|
||||
{
|
||||
var entity = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id);
|
||||
if (entity is null)
|
||||
var dto = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id);
|
||||
if (dto is null)
|
||||
return null;
|
||||
var dto = Convert(entity);
|
||||
|
||||
dto.RoleNames = GetRolesNamesByIdUser(dto.Id);
|
||||
return dto;
|
||||
}
|
||||
@ -112,10 +114,11 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
public async Task<int> DeleteAsync(int id, CancellationToken token = default)
|
||||
{
|
||||
var entity = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id);
|
||||
if (entity is null)
|
||||
var dto = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id);
|
||||
if (dto is null)
|
||||
return 0;
|
||||
|
||||
var entity = Convert(dto);
|
||||
var result = dbContext.Users.Remove(entity);
|
||||
await dbContext.SaveChangesAsync(token);
|
||||
DropCacheUsers();
|
||||
@ -124,11 +127,12 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
|
||||
{
|
||||
var entities = (await GetCacheUserAsync(token)).Where(r => ids.Contains(r.Id));
|
||||
if (entities is null)
|
||||
var dto = (await GetCacheUserAsync(token)).Where(r => ids.Contains(r.Id));
|
||||
if (dto is null)
|
||||
return 0;
|
||||
|
||||
var count = entities.Count();
|
||||
var count = dto.Count();
|
||||
var entities = dto.Select(Convert);
|
||||
dbContext.Users.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync(token);
|
||||
DropCacheUsers();
|
||||
@ -177,22 +181,23 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
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());
|
||||
var existingUser = Convert(existingUserDto);
|
||||
if (existingUser is not null)
|
||||
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
|
||||
.Include(r => r.Company)
|
||||
.Include(r => r.RelationUsersUserRoles)
|
||||
.FromCacheAsync(userCacheTag, cacheObsolence, token);
|
||||
private IEnumerable<User> GetCacheUser()
|
||||
.FromCacheAsync(userCacheTag, cacheObsolence, Convert, token);
|
||||
private IEnumerable<UserExtendedDto> GetCacheUser()
|
||||
=> dbContext.Users
|
||||
.Include(r => r.Company)
|
||||
.Include(r => r.RelationUsersUserRoles)
|
||||
.FromCache(userCacheTag, cacheObsolence);
|
||||
.FromCache(userCacheTag, cacheObsolence, Convert);
|
||||
private void DropCacheUsers()
|
||||
=> dbContext.Users.DropCache(userCacheTag);
|
||||
|
||||
|
@ -50,33 +50,30 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token)
|
||||
{
|
||||
var entities = await GetCacheUserRoleAsync(token)
|
||||
var dtos = await GetCacheUserRoleAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (entities is null)
|
||||
if (dtos is null)
|
||||
return Enumerable.Empty<UserRoleDto>();
|
||||
|
||||
var dtos = entities.Select(Convert);
|
||||
return dtos;
|
||||
|
||||
}
|
||||
|
||||
public UserRoleDto? GetOrDefault(int id)
|
||||
{
|
||||
var entity = GetCacheUserRole().FirstOrDefault(x => x.Id == id);
|
||||
if (entity is null)
|
||||
var dto = GetCacheUserRole().FirstOrDefault(x => x.Id == id);
|
||||
if (dto is null)
|
||||
return null;
|
||||
var dto = Convert(entity);
|
||||
return dto;
|
||||
}
|
||||
|
||||
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);
|
||||
if (entity is null)
|
||||
if (dto is null)
|
||||
return null;
|
||||
var dto = Convert(entity);
|
||||
return dto;
|
||||
}
|
||||
|
||||
@ -85,13 +82,12 @@ namespace AsbCloudInfrastructure.Repository
|
||||
if (names?.Any() != true)
|
||||
return Enumerable.Empty<UserRoleDto>();
|
||||
|
||||
var entities = (await GetCacheUserRoleAsync(token))
|
||||
var dtos = (await GetCacheUserRoleAsync(token))
|
||||
.Where(r => names.Contains(r.Caption));
|
||||
|
||||
if (entities?.Count() != names.Count())
|
||||
if (dtos?.Count() != names.Count())
|
||||
throw new ArgumentInvalidException("Invalid role names", nameof(names));
|
||||
|
||||
var dtos = entities.Select(Convert);
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@ -109,11 +105,11 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7)
|
||||
{
|
||||
var role = GetCacheUserRole().FirstOrDefault(r => r.Id == id);
|
||||
if (role is null)
|
||||
var dto = GetCacheUserRole().FirstOrDefault(r => r.Id == id);
|
||||
if (dto is null)
|
||||
return Enumerable.Empty<UserRoleDto>();
|
||||
var role = Convert(dto);
|
||||
|
||||
var dto = Convert(role);
|
||||
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto };
|
||||
|
||||
if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true)
|
||||
@ -132,10 +128,11 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
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);
|
||||
await dbContext.SaveChangesAsync(token);
|
||||
DropCacheUserRole();
|
||||
@ -146,10 +143,11 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
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();
|
||||
dbContext.UserRoles.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync(token);
|
||||
@ -172,8 +170,10 @@ namespace AsbCloudInfrastructure.Repository
|
||||
return true;
|
||||
|
||||
var idPermissionInfo = permissionInfo.Id;
|
||||
var roles = GetCacheUserRole()
|
||||
var dtos = GetCacheUserRole()
|
||||
.Where(r => rolesIds.Contains(r.Id));
|
||||
var roles = dtos.Select(Convert);
|
||||
|
||||
foreach (var role in roles)
|
||||
if (HasPermission(role, idPermissionInfo))
|
||||
return true;
|
||||
@ -190,8 +190,9 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
foreach (var relation in userRole.RelationUserRoleUserRoles)
|
||||
{
|
||||
var includedRole = GetCacheUserRole()
|
||||
var dto = GetCacheUserRole()
|
||||
.First(p => p.Id == relation.IdInclude);
|
||||
var includedRole = Convert(dto);
|
||||
if (HasPermission(includedRole, idPermission, --recursionLevel))
|
||||
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
|
||||
.Include(r => r.RelationUserRolePermissions)
|
||||
.Include(r => r.RelationUserRoleUserRoles)
|
||||
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, token);
|
||||
private IEnumerable<UserRole> GetCacheUserRole()
|
||||
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, Convert, token);
|
||||
private IEnumerable<UserRoleDto> GetCacheUserRole()
|
||||
=> dbContext.UserRoles
|
||||
.Include(r => r.RelationUserRolePermissions)
|
||||
.Include(r => r.RelationUserRoleUserRoles)
|
||||
.FromCache(userRoleCacheTag, relationCacheObsolence);
|
||||
.FromCache(userRoleCacheTag, relationCacheObsolence, Convert);
|
||||
private void DropCacheUserRole()
|
||||
=> dbContext.RelationUserUserRoles.DropCache(relationUserRoleUserRoleCacheTag);
|
||||
|
||||
@ -294,9 +295,10 @@ namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
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);
|
||||
}).ToList();
|
||||
}).ToArray();
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user