Browse Source

Move role management to a code behind file

pull/5399/head
Halil İbrahim Kalkan 6 years ago
parent
commit
6facd14ea4
  1. 57
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/RoleManagement.razor
  2. 62
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/RoleManagement.razor.cs

57
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/RoleManagement.razor

@ -3,7 +3,6 @@
@using Volo.Abp.Identity
@using Microsoft.AspNetCore.Authorization
@using Microsoft.Extensions.Localization
@using Volo.Abp.Application.Dtos
@using Volo.Abp.Identity.Localization
@inject IIdentityRoleAppService RoleAppService
@inject IStringLocalizer<IdentityResource> L
@ -11,9 +10,9 @@
<h1>@L["Roles"]</h1>
<DataGrid TItem="IdentityRoleDto"
Data="roles"
ReadData="OnReadGridData"
TotalItems="totalCount"
Data="_roles"
ReadData="OnDataGridReadAsync"
TotalItems="_totalCount"
ShowPager="true">
<DataGridColumn TItem="IdentityRoleDto" Field="@nameof(IdentityRoleDto.Name)" Caption="Name" />
</DataGrid>
@ -22,52 +21,4 @@
<label>Name:</label>
<input type="text" name="RoleName" @bind="_newRole.Name" />
<button class="btn btn-primary" @onclick="CreateRoleAsync">Create!</button>
@code {
async Task OnReadGridData(DataGridReadDataEventArgs<IdentityRoleDto> e)
{
currentPage = e.Page - 1;
await GetRolesAsync();
StateHasChanged();
}
private IdentityRoleCreateDto _newRole = new IdentityRoleCreateDto();
int currentPage = 0;
readonly int pageSize = LimitedResultRequestDto.DefaultMaxResultCount;
int? totalCount = 0;
IReadOnlyList<IdentityRoleDto> roles = Array.Empty<IdentityRoleDto>();
protected override async Task OnInitializedAsync()
{
await GetRolesAsync();
}
private async Task GetRolesAsync()
{
var result = await RoleAppService.GetListAsync(
new PagedAndSortedResultRequestDto
{
SkipCount = currentPage * pageSize,
MaxResultCount = pageSize
});
roles = result.Items;
totalCount = (int?)result.TotalCount;
}
private async Task CreateRoleAsync()
{
await RoleAppService.CreateAsync(_newRole);
_newRole = new IdentityRoleCreateDto();
await GetRolesAsync();
}
private async Task DeleteRoleAsync(Guid id)
{
await RoleAppService.DeleteAsync(id);
await GetRolesAsync();
}
}
<button class="btn btn-primary" @onclick="CreateRoleAsync">Create!</button>

62
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/RoleManagement.razor.cs

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Blazorise.DataGrid;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Identity;
namespace MyCompanyName.MyProjectName.Blazor.Pages
{
//TODO: Idea: Can we create a base class to simplify all these, like CrudPage<...>..?
public partial class RoleManagement
{
private int _currentPage;
private int? _totalCount;
private IReadOnlyList<IdentityRoleDto> _roles;
private IdentityRoleCreateDto _newRole;
public RoleManagement()
{
_newRole = new IdentityRoleCreateDto();
}
protected override async Task OnInitializedAsync()
{
await GetRolesAsync();
}
private async Task GetRolesAsync()
{
var result = await RoleAppService.GetListAsync(
new PagedAndSortedResultRequestDto
{
SkipCount = _currentPage * LimitedResultRequestDto.DefaultMaxResultCount,
MaxResultCount = LimitedResultRequestDto.DefaultMaxResultCount
});
_roles = result.Items;
_totalCount = (int?)result.TotalCount;
}
private async Task OnDataGridReadAsync(DataGridReadDataEventArgs<IdentityRoleDto> e)
{
_currentPage = e.Page - 1;
await GetRolesAsync();
StateHasChanged();
}
private async Task CreateRoleAsync()
{
await RoleAppService.CreateAsync(_newRole);
_newRole = new IdentityRoleCreateDto();
await GetRolesAsync();
}
private async Task DeleteRoleAsync(Guid id)
{
await RoleAppService.DeleteAsync(id);
await GetRolesAsync();
}
}
}
Loading…
Cancel
Save