DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/UserRepository.cs

103 lines
4.1 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.EfCache;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class UserRepository : IUserRepository
{
private readonly IAsbCloudDbContext db;
private static readonly System.TimeSpan cacheObsolescence = System.TimeSpan.FromMinutes(15);
private static readonly TypeAdapterConfig userTypeAdapterConfig = TypeAdapterConfig<UserExtendedDto, User>
.NewConfig()
.Ignore(dst => dst.Company,
dst => dst.FileMarks,
dst => dst.Files,
dst => dst.RelationUsersUserRoles)
.Config;
public UserRepository(IAsbCloudDbContext db) {
this.db = db;
}
public async Task<UserExtendedDto> InsertAsync(UserExtendedDto dto, CancellationToken token)
{
dto.Id = default;
var updatedEntity = await db.Users.AddAsync(Convert(dto), token).ConfigureAwait(false);
return Convert(updatedEntity.Entity);
}
public async Task UpdateRolesCacheForUserAsync(int idUser, IEnumerable<UserRoleDto> newRoles, CancellationToken token)
{
var entity = await db.RelationUserUserRoles.FirstOrDefaultAsync(x => x.IdUser == idUser, token).ConfigureAwait(false);
if (entity is not null)
{
db.RelationUserUserRoles.Remove(entity);
if (newRoles?.Any() == true)
await db.RelationUserUserRoles.AddRangeAsync(newRoles.Select(role => new RelationUserUserRole
{
IdUser = idUser,
IdUserRole = role.Id
}), token).ConfigureAwait(false);
}
}
public async Task<UserExtendedDto?> GetUserByLoginAsync(string login, CancellationToken token)
{
var entities = await db.Users.FromCacheAsync("Users", cacheObsolescence, token).ConfigureAwait(false);
var entity = entities.FirstOrDefault(x => x.Login.ToLower() == login.ToLower());
if (entity is not null)
return Convert(entity);
return null;
}
public async Task<IEnumerable<UserExtendedDto>?> GetAllAsync(CancellationToken token = default)
{
var entities = await db.Users.FromCacheAsync("Users", cacheObsolescence, token).ConfigureAwait(false);
if (entities is null)
return null;
var dtos = entities.Select(Convert).ToList();
//for (var i = 0; i < dtos.Count; i++)
// dtos[i].RoleNames = GetRolesNamesByIdUser(dtos[i].Id, token);
return dtos;
}
//private async Task<IEnumerable<string>> GetRolesNamesByIdUser(int idUser, CancellationToken token)
//=> await GetRolesByIdUser(idUser, token)
// ?.Select(r => r.Caption)
// .Distinct();
//public async Task<IEnumerable<UserRoleDto>> GetRolesByIdUser(int idUser, CancellationToken token, int nestedLevel = 0)
//{
// var allRoles = await db.RelationUserUserRoles.FromCacheAsync("RelationUserUserRoles", cacheObsolescence, token).ConfigureAwait(false);
// var roles = allRoles.Where(r => r.IdUser == idUser);
// if (roles?.Any() != true)
// return null;
// return roles.SelectMany(r => RoleService.GetNestedById(r.IdUserRole, nestedLevel));
//}
private User Convert(UserExtendedDto dto)
{
var entity = dto.Adapt<User>(userTypeAdapterConfig);
if (string.IsNullOrEmpty(entity.PasswordHash))
entity.PasswordHash = db.Users.FirstOrDefault(u => u.Id == dto.Id)?.PasswordHash;
return entity;
}
private UserExtendedDto Convert(User entity)
{
var dto = entity.Adapt<UserExtendedDto>();
return dto;
}
}
#nullable disable
}