DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/UserRoleService.cs
2021-12-03 15:03:33 +05:00

139 lines
5.3 KiB
C#

using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using AsbCloudApp.Services;
namespace AsbCloudInfrastructure.Services
{
public class UserRoleService : IUserRoleService
{
private readonly CacheTable<UserRole> cacheUserRoles;
private readonly IPermissionService permissionService;
public List<string> Incledes { get; } = new();
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb, IPermissionService permissionService)
{
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context, new string[] { nameof(UserRole.Permissions) });
this.permissionService = permissionService;
}
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token = default)
{
var entities = await cacheUserRoles.WhereAsync(token)
.ConfigureAwait(false);
var dtos = entities.Adapt<UserRoleDto>();
return dtos;
}
public async Task<UserRoleDto> GetAsync(int id, CancellationToken token = default)
{
var entity = await cacheUserRoles.FirstOrDefaultAsync(r=>r.Id == id, token)
.ConfigureAwait(false);
var dto = entity?.Adapt<UserRoleDto>();
return dto;
}
public async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
{
var entity = dto.Adapt<UserRole>();
var updatedEntity = await cacheUserRoles.InsertAsync(entity, token).ConfigureAwait(false);
if (dto.Permissions?.Any() == true)
{
foreach (var permission in dto.Permissions)
permission.IdUserRole = updatedEntity.Id;
await permissionService.InsertRangeAsync(dto.Permissions, token).ConfigureAwait(false);
await cacheUserRoles.RefreshAsync(true, token)
.ConfigureAwait(false);
}
return updatedEntity?.Id ?? 0;
}
public async Task<int> InsertRangeAsync(IEnumerable<UserRoleDto> dtos, CancellationToken token = default)
{
var entities = dtos.Adapt<UserRole>();
return await cacheUserRoles.InsertAsync(entities, token).ConfigureAwait(false);
}
public async Task<int> UpdateAsync(int id, UserRoleDto dto, CancellationToken token = default)
{
var entity = dto.Adapt<UserRole>();
entity.Id = id;
await cacheUserRoles.UpsertAsync(entity, token)
.ConfigureAwait(false);
if(dto.Permissions is not null)
{
await permissionService.DeleteAllByRoleAsync(id, token)
.ConfigureAwait(false);
if (dto.Permissions.Any())
{
foreach (var permission in dto.Permissions)
permission.IdUserRole = id;
await permissionService.InsertRangeAsync(dto.Permissions, token)
.ConfigureAwait(false);
}
await cacheUserRoles.RefreshAsync(true, token)
.ConfigureAwait(false);
}
return id;
}
private IEnumerable<Permission> GetNestedPermissions(UserRole role, int counter = 10)
{
List<Permission> permissions = role.Permissions.ToList();
if (role.IdParent is not null)
{
if (counter == 0)
{
Trace.WriteLine($"User role with id: {role.Id} has more than 10 nested childs");
return permissions;
}
var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == role.IdParent);
if (parentRole is null)
return permissions;
var parentPermissions = GetNestedPermissions(parentRole, counter--);
Merge(ref permissions, parentPermissions);
}
return permissions;
}
private static void Merge(ref List<Permission> permissions, IEnumerable<Permission> newPermissions)
{
foreach (var newPermission in newPermissions)
{
var permissionIndex = permissions.FindIndex(p => p.IdPermission == newPermission.IdPermission);
if (permissionIndex == -1)
permissions.Add(newPermission);
else
{
var permission = permissions[permissionIndex];
permissions[permissionIndex] = new Permission
{
IdPermission = permission.IdPermission,
IdUserRole = permission.IdUserRole,
PermissionValue = permission.PermissionValue | newPermission.PermissionValue,
};
}
}
}
public Task<int> DeleteAsync(int id, CancellationToken token = default)
=> cacheUserRoles.RemoveAsync(r => r.Id == id, token);
public Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
=> cacheUserRoles.RemoveAsync(r => ids.Contains(r.Id), token);
}
}