рефакт

This commit is contained in:
eugeniy_ivanov 2023-02-14 12:20:29 +05:00
parent 6bf992f1f6
commit 2cc8abd767
2 changed files with 17 additions and 26 deletions

View File

@ -1,10 +1,8 @@
using AsbCloudDb.Model.Subsystems; using AsbCloudDb.Model.Subsystems;
using AsbCloudDb.Model.WITS; using AsbCloudDb.Model.WITS;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using System; using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -69,8 +67,7 @@ namespace AsbCloudDb.Model
Task<int> RefreshMaterializedViewAsync<TEntity>(CancellationToken token) where TEntity : class; Task<int> RefreshMaterializedViewAsync<TEntity>(CancellationToken token) where TEntity : class;
int SaveChanges(); int SaveChanges();
int SaveChanges(bool acceptAllChangesOnSuccess); int SaveChanges(bool acceptAllChangesOnSuccess);
Task<int> SaveChangesAsync(CancellationToken cancellationToken); Task<int> SaveChangesAsync(CancellationToken cancellationToken);
DbSet<TEntity> Set<TEntity>(string name) where TEntity : class;
DbSet<TEntity> Set<TEntity>() where TEntity : class; DbSet<TEntity> Set<TEntity>() where TEntity : class;
} }
} }

View File

@ -67,8 +67,7 @@ namespace AsbCloudInfrastructure.Repository
foreach(var dto in dtos) foreach(var dto in dtos)
{ {
dto.RoleNames = GetRolesNamesByIdUser(dto.Id); dto.RoleNames = GetRolesNamesByIdUser(dto.Id);
}; };
return dtos; return dtos;
} }
@ -110,18 +109,16 @@ namespace AsbCloudInfrastructure.Repository
var entity = dbContext.Users.FirstOrDefault(u => u.Id == dto.Id); var entity = dbContext.Users.FirstOrDefault(u => u.Id == dto.Id);
if (entity is not null) if (entity is not null)
{ {
var user = Convert(dto); entity.Id = dto.Id;
entity.Id = user.Id; entity.Name = dto.Name;
entity.Company = user.Company; entity.Email = dto.Email;
entity.Name = user.Name; entity.Phone = dto.Phone;
entity.Email = user.Email; entity.Surname = dto.Surname;
entity.Phone = user.Phone; entity.Patronymic = dto.Patronymic;
entity.Surname = user.Surname; entity.Position = dto.Position;
entity.Patronymic = user.Patronymic; entity.IdCompany = dto.IdCompany;
entity.Position = user.Position; entity.IdState = dto.IdState;
entity.IdCompany = user.IdCompany;
entity.IdState = user.IdState;
await dbContext.SaveChangesAsync(token); await dbContext.SaveChangesAsync(token);
} }
DropCacheUsers(); DropCacheUsers();
@ -209,7 +206,11 @@ namespace AsbCloudInfrastructure.Repository
{ {
var relations = dbContext.RelationUserUserRoles.Where(r => r.IdUser == idUser); var relations = dbContext.RelationUserUserRoles.Where(r => r.IdUser == idUser);
dbContext.RelationUserUserRoles.RemoveRange(relations); dbContext.RelationUserUserRoles.RemoveRange(relations);
var entityRoles = roleDtos.Select(role => Convert(role)); var entityRoles = roleDtos.Select(role => new RelationUserUserRole
{
IdUser = idUser,
IdUserRole = role.Id
});
dbContext.RelationUserUserRoles.AddRange(entityRoles); dbContext.RelationUserUserRoles.AddRange(entityRoles);
await dbContext.SaveChangesAsync(token); await dbContext.SaveChangesAsync(token);
DropCacheRelationUserUserRoleCacheTag(); DropCacheRelationUserUserRoleCacheTag();
@ -255,13 +256,6 @@ namespace AsbCloudInfrastructure.Repository
entity.PasswordHash = dbContext.Users.FirstOrDefault(u => u.Id == dto.Id)?.PasswordHash; entity.PasswordHash = dbContext.Users.FirstOrDefault(u => u.Id == dto.Id)?.PasswordHash;
return entity; return entity;
} }
protected RelationUserUserRole Convert(UserRoleDto dto)
{
var entity = dto.Adapt<RelationUserUserRole>();
return entity;
}
protected virtual UserExtendedDto Convert(User entity) protected virtual UserExtendedDto Convert(User entity)
{ {
var dto = entity.Adapt<UserExtendedDto>(); var dto = entity.Adapt<UserExtendedDto>();