forked from ddrilling/AsbCloudServer
107 lines
3.9 KiB
C#
107 lines
3.9 KiB
C#
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
|
using AsbCloudApp.Data;
|
||
|
using AsbCloudApp.Services;
|
||
|
using AsbCloudDb.Model;
|
||
|
using AsbCloudInfrastructure.Services.Cache;
|
||
|
using Mapster;
|
||
|
|
||
|
namespace AsbCloudInfrastructure.Services
|
||
|
{
|
||
|
public class UserRoleService : IUserRoleService
|
||
|
{
|
||
|
private readonly IAsbCloudDbContext db;
|
||
|
private readonly CacheTable<UserRole> cacheUserRoles;
|
||
|
private readonly CacheTable<Permission> cachePermissions;
|
||
|
private readonly CacheTable<RelationUserRolePermission> cacheUserRolesPermissions;
|
||
|
|
||
|
public UserRoleService(IAsbCloudDbContext db, CacheDb cacheDb)
|
||
|
{
|
||
|
this.db = db;
|
||
|
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)db);
|
||
|
cachePermissions = cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)db);
|
||
|
cacheUserRolesPermissions =
|
||
|
cacheDb.GetCachedTable<RelationUserRolePermission>((AsbCloudDbContext)db);
|
||
|
}
|
||
|
|
||
|
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token)
|
||
|
{
|
||
|
var rolesDtos = (await cacheUserRoles.WhereAsync(token).ConfigureAwait(false))
|
||
|
.Adapt<UserRoleDto>();
|
||
|
|
||
|
return rolesDtos.Select(FillUserRoleWithPermissions);
|
||
|
}
|
||
|
|
||
|
public async Task<UserRoleDto> GetAsync(int id, CancellationToken token)
|
||
|
{
|
||
|
var roleDto = (await cacheUserRoles.FirstOrDefaultAsync(r => r.Id == id, token)
|
||
|
.ConfigureAwait(false)).Adapt<UserRoleDto>();
|
||
|
|
||
|
return roleDto is null ? null : FillUserRoleWithPermissions(roleDto);
|
||
|
}
|
||
|
|
||
|
public async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
||
|
{
|
||
|
var newRole = dto.Adapt<UserRole>();
|
||
|
db.UserRoles.Add(newRole);
|
||
|
var result = await db.SaveChangesAsync(token);
|
||
|
|
||
|
if (dto.PermissionIds == default)
|
||
|
return result;
|
||
|
|
||
|
foreach (var pId in dto.PermissionIds)
|
||
|
{
|
||
|
var relation = new RelationUserRolePermission()
|
||
|
{
|
||
|
IdUserRole = newRole.Id,
|
||
|
IdPermission = pId
|
||
|
};
|
||
|
|
||
|
db.RelationUserRolesPermissions.Add(relation);
|
||
|
}
|
||
|
|
||
|
return await db.SaveChangesAsync(token);
|
||
|
}
|
||
|
|
||
|
public async Task UpdateAsync(UserRoleDto dto, CancellationToken token = default)
|
||
|
{
|
||
|
var entity = dto.Adapt<UserRole>();
|
||
|
db.UserRoles.Update(entity);
|
||
|
await db.SaveChangesAsync(token);
|
||
|
|
||
|
if (dto.PermissionIds != default)
|
||
|
{
|
||
|
await cacheUserRolesPermissions.RemoveAsync(r => r.IdUserRole == dto.Id, token)
|
||
|
.ConfigureAwait(false);
|
||
|
|
||
|
var newRelations = dto.PermissionIds.Select(p => new RelationUserRolePermission()
|
||
|
{
|
||
|
IdUserRole = dto.Id,
|
||
|
IdPermission = p
|
||
|
});
|
||
|
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
||
|
{
|
||
|
var entities = cacheUserRoles.Where(e => ids.Contains(e.Id));
|
||
|
if (entities == default)
|
||
|
return 0;
|
||
|
db.UserRoles.RemoveRange(entities);
|
||
|
return await db.SaveChangesAsync(token);
|
||
|
}
|
||
|
|
||
|
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
|
||
|
{
|
||
|
var rolePermissionIds = cacheUserRolesPermissions.Where(c =>
|
||
|
c.IdUserRole == roleDto.Id).Select(p => p.IdPermission);
|
||
|
roleDto.Permissions = cachePermissions.Where(permission => rolePermissionIds.Contains(permission.Id))
|
||
|
.Adapt<PermissionDto>();
|
||
|
|
||
|
return roleDto;
|
||
|
}
|
||
|
}
|
||
|
}
|