LANCommander/LANCommander.Server/UI/Pages/Settings/Roles/Edit.razor
2025-03-12 01:10:03 -05:00

71 lines
2.1 KiB
Text

@page "/Settings/Roles/{id:guid}"
@using Microsoft.EntityFrameworkCore
@inject RoleService RoleService
@inject CollectionService CollectionService
@inject IMessageService MessageService
@inject ILogger<Edit> Logger
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
<PageHeader Title="Edit Role">
<PageHeaderExtra>
<Space Direction="SpaceDirection.Horizontal">
<SpaceItem>
<Button Type="ButtonType.Primary" OnClick="Save">Save</Button>
</SpaceItem>
</Space>
</PageHeaderExtra>
</PageHeader>
<PageContent>
<Form Model="@Role" Layout="@FormLayout.Vertical">
<FormItem Label="Name">
<Input @bind-Value="Role.Name" />
</FormItem>
<FormItem Label="Collections">
<TransferInput LeftTitle="Available" RightTitle="Allowed" DataSource="Collections" TitleSelector="c => c.Name" @bind-Values="SelectedCollections" />
</FormItem>
</Form>
</PageContent>
@code {
[Parameter] public Guid Id { get; set; }
Role Role = new Role();
IEnumerable<Collection> Collections = new List<Collection>();
ICollection<Collection> SelectedCollections = new List<Collection>();
protected override async Task OnInitializedAsync()
{
Role = await RoleService
.AsNoTracking()
.Query(q =>
{
return q
.Include(r => r.UserRoles)
.ThenInclude(ur => ur.User);
})
.Include(r => r.Collections)
.GetAsync(Id);
SelectedCollections = Role.Collections;
Collections = await CollectionService.GetAsync();
}
async Task Save()
{
try
{
await RoleService.UpdateAsync(Role);
await RoleService.AssignCollections(Id, SelectedCollections.Select(c => c.Id));
MessageService.Success("Role updated!");
}
catch (Exception ex)
{
MessageService.Error("Role could not be updated!");
Logger.LogError(ex, "Role could not be updated!");
}
}
}