Skip to content

Commit c7893bc

Browse files
committed
add role service
1 parent 8c8654b commit c7893bc

5 files changed

Lines changed: 79 additions & 5 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using CleanArchitecture.Blazor.Application.Features.Identity.DTOs;
2+
3+
namespace CleanArchitecture.Blazor.Application.Common.Interfaces.Identity;
4+
5+
public interface IRoleService
6+
{
7+
List<ApplicationRoleDto> DataSource { get; }
8+
event Func<Task>? OnChange;
9+
void Initialize();
10+
void Refresh();
11+
}

src/Infrastructure/DependencyInjection.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,13 @@ private static IServiceCollection AddServices(this IServiceCollection services)
184184
return service;
185185
});
186186

187-
187+
services.AddSingleton<RoleService>()
188+
.AddSingleton<IRoleService>(sp =>
189+
{
190+
var service = sp.GetRequiredService<RoleService>();
191+
service.Initialize();
192+
return service;
193+
});
188194
return services
189195
.AddScoped<IValidationService, ValidationService>()
190196
.AddScoped<IDateTime, DateTimeService>()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using AutoMapper;
2+
using AutoMapper.QueryableExtensions;
3+
using CleanArchitecture.Blazor.Application.Features.Identity.DTOs;
4+
using CleanArchitecture.Blazor.Domain.Identity;
5+
using ZiggyCreatures.Caching.Fusion;
6+
7+
namespace CleanArchitecture.Blazor.Infrastructure.Services.Identity;
8+
9+
public class RoleService : IRoleService
10+
{
11+
private const string CACHEKEY = "ALL-ApplicationRoleDto";
12+
private readonly IMapper _mapper;
13+
private readonly IFusionCache _fusionCache;
14+
private readonly RoleManager<ApplicationRole> _roleManager;
15+
16+
public RoleService(
17+
IMapper mapper,
18+
IFusionCache fusionCache,
19+
IServiceScopeFactory scopeFactory)
20+
{
21+
_mapper = mapper;
22+
_fusionCache = fusionCache;
23+
var scope = scopeFactory.CreateScope();
24+
_roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<ApplicationRole>>();
25+
DataSource = new List<ApplicationRoleDto>();
26+
}
27+
28+
public List<ApplicationRoleDto> DataSource { get; private set; }
29+
30+
public event Func<Task>? OnChange;
31+
32+
public void Initialize()
33+
{
34+
DataSource = _fusionCache.GetOrSet(CACHEKEY,
35+
_ => _roleManager.Roles
36+
.ProjectTo<ApplicationRoleDto>(_mapper.ConfigurationProvider).OrderBy(x => x.TenantId).ThenBy(x => x.Name)
37+
.ToList())
38+
?? new List<ApplicationRoleDto>();
39+
OnChange?.Invoke();
40+
}
41+
42+
43+
public void Refresh()
44+
{
45+
_fusionCache.Remove(CACHEKEY);
46+
DataSource = _fusionCache.GetOrSet(CACHEKEY,
47+
_ => _roleManager.Roles
48+
.ProjectTo<ApplicationRoleDto>(_mapper.ConfigurationProvider).OrderBy(x => x.TenantId).ThenBy(x => x.Name)
49+
.ToList())
50+
?? new List<ApplicationRoleDto>();
51+
OnChange?.Invoke();
52+
}
53+
}

src/Server.UI/Pages/Identity/Roles/Roles.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@page "/identity/roles"
2+
@using CleanArchitecture.Blazor.Application.Common.Interfaces.Identity
23
@using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant
34
@using CleanArchitecture.Blazor.Domain.Identity
45
@using CleanArchitecture.Blazor.Application.Features.Identity.DTOs
@@ -12,6 +13,7 @@
1213
@attribute [Authorize(Policy = Permissions.Roles.View)]
1314
@inherits OwningComponentBase<ApplicationDbContext>
1415
@inject ITenantService TenantsService
16+
@inject IRoleService RoleService
1517
@inject IFusionCache FusionCache
1618
@inject IStringLocalizer<Roles> L
1719

@@ -273,6 +275,7 @@ OnAssignChanged="OnAssignChangedHandler">
273275
await InvokeAsync(async () =>
274276
{
275277
_selectedItems = new HashSet<ApplicationRoleDto>();
278+
RoleService.Refresh();
276279
await _table.ReloadServerData();
277280
});
278281
}

src/Server.UI/Pages/Identity/Users/Users.razor

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
@inject IFusionCache FusionCache
3333
@inject IExcelService ExcelService
3434
@inject IMailService MailService
35+
@inject IRoleService RoleService
3536
@inject IStringLocalizer<Users> L
3637
@inject ILogger<Users> Logger
3738

@@ -112,7 +113,7 @@
112113
@if (_accessRights.Search)
113114
{
114115
<MudSelect T="string" Placeholder="@L["Search by role name"]" Value="@_searchRole" Clearable="true" ValueChanged="@(OnSearchRole)" Style="width:150px">
115-
@foreach (var str in _roles)
116+
@foreach (var str in _roles.Select(x=>x.Name).Distinct())
116117
{
117118
<MudSelectItem Value="@str">@str</MudSelectItem>
118119
}
@@ -266,15 +267,15 @@
266267
private bool _exporting;
267268
private bool _uploading;
268269
private bool _canViewOnlineStatus;
269-
private List<string?> _roles = new();
270+
private List<ApplicationRoleDto> _roles = new();
270271
private string? _searchRole;
271272

272273
protected override async Task OnInitializedAsync()
273274
{
274275
Title = L[_currentDto.GetClassDescription()];
275276
InitializeServices();
276277
_accessRights = await PermissionService.GetAccessRightsAsync<UsersAccessRights>();
277-
_roles = await RoleManager.Roles.Select(x => x.Name).ToListAsync();
278+
_roles = RoleService.DataSource;
278279
}
279280
private void InitializeServices()
280281
{
@@ -334,7 +335,7 @@
334335
_selectedTenantId = tenantId;
335336
if (_selectedTenantId != string.Empty)
336337
{
337-
_roles = await RoleManager.Roles.Where(x => x.TenantId == _selectedTenantId).Select(x => x.Name).ToListAsync();
338+
_roles = RoleService.DataSource.Where(x => x.TenantId == _selectedTenantId).ToList();
338339
}
339340
await _table.ReloadServerData();
340341
}

0 commit comments

Comments
 (0)