feat: 角色模板改为数据库管理支持前端自定义
This commit is contained in:
@@ -1,26 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Abstractions;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Application.Identity.Queries;
|
||||
using TakeoutSaaS.Domain.Identity.Repositories;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 角色模板列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class ListRoleTemplatesQueryHandler(IRoleTemplateProvider roleTemplateProvider)
|
||||
public sealed class ListRoleTemplatesQueryHandler(IRoleTemplateRepository roleTemplateRepository)
|
||||
: IRequestHandler<ListRoleTemplatesQuery, IReadOnlyList<RoleTemplateDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Task<IReadOnlyList<RoleTemplateDto>> Handle(ListRoleTemplatesQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IReadOnlyList<RoleTemplateDto>> Handle(ListRoleTemplatesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var templates = roleTemplateProvider.GetTemplates()
|
||||
var templates = await roleTemplateRepository.GetAllAsync(request.IsActive, cancellationToken);
|
||||
var permissionsMap = await roleTemplateRepository.GetPermissionsAsync(templates.Select(t => t.Id), cancellationToken);
|
||||
|
||||
var dtos = templates
|
||||
.OrderBy(template => template.TemplateCode, StringComparer.OrdinalIgnoreCase)
|
||||
.Select(TemplateMapper.ToDto)
|
||||
.Select(template =>
|
||||
{
|
||||
var codes = permissionsMap.TryGetValue(template.Id, out var perms)
|
||||
? (IReadOnlyCollection<string>)perms.Select(p => p.PermissionCode).ToArray()
|
||||
: Array.Empty<string>();
|
||||
return TemplateMapper.ToDto(template, codes);
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
return Task.FromResult<IReadOnlyList<RoleTemplateDto>>(templates);
|
||||
return dtos;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user