DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/UserRoleService.cs

137 lines
5.3 KiB
C#
Raw Normal View History

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;
2021-12-03 09:44:10 +05:00
using AsbCloudApp.Services;
namespace AsbCloudInfrastructure.Services
{
public class UserRoleService : IUserRoleService
{
private readonly CacheTable<UserRole> cacheUserRoles;
2021-12-03 09:44:10 +05:00
private readonly IPermissionService permissionService;
public List<string> Includes { get; } = new();
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb, IPermissionService permissionService)
{
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context, new [] { nameof(UserRole.Permissions) });
2021-12-03 09:44:10 +05:00
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)
return updatedEntity?.Id ?? 0;
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;
}
2021-12-03 09:44:10 +05:00
private IEnumerable<Permission> GetNestedPermissions(UserRole role, int counter = 10)
{
var permissions = role.Permissions.ToList();
if (role.IdParent is null)
return permissions;
if (counter == 0)
2021-12-03 09:44:10 +05:00
{
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;
2021-12-03 09:44:10 +05:00
var parentPermissions = GetNestedPermissions(parentRole, --counter);
Merge(ref permissions, parentPermissions);
2021-12-03 09:44:10 +05:00
return permissions;
}
2021-12-03 09:44:10 +05:00
private static void Merge(ref List<Permission> permissions, IEnumerable<Permission> newPermissions)
{
2021-12-03 09:44:10 +05:00
foreach (var newPermission in newPermissions)
{
2021-12-03 09:44:10 +05:00
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);
}
}