From 6029e86072c2a21db808f23a0df162bac3ec6d55 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 21 Dec 2021 11:47:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20IRole=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/RoleService.cs | 119 +++++++++++------- .../admin/BootstrapAdmin.Web.Core/IRole.cs | 25 +++- .../Pages/Admin/Menus.razor.cs | 25 ++-- .../Pages/Admin/Users.razor.cs | 20 ++- 4 files changed, 119 insertions(+), 70 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs index c4162fca..46720abd 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs @@ -2,54 +2,83 @@ using BootstrapAdmin.Web.Core; using PetaPoco; -namespace BootstrapAdmin.DataAccess.PetaPoco.Services +namespace BootstrapAdmin.DataAccess.PetaPoco.Services; + +class RoleService : BaseDatabase, IRole { - class RoleService : BaseDatabase, IRole + /// + /// + /// + /// + public RoleService(IDatabase db) => Database = db; + + /// + /// + /// + /// + public List GetAll() => Database.Fetch(); + + /// + /// + /// + /// + /// + public List GetRolesByGroupId(string? groupId) => Database.Fetch("select RoleID from RoleGroup where GroupID = @0", groupId); + + /// + /// + /// + /// + /// + /// + public bool SaveRolesByGroupId(string? groupId, IEnumerable roleIds) { - /// - /// - /// - /// - public RoleService(IDatabase db) => Database = db; - - /// - /// - /// - /// - public List GetAll() => Database.Fetch(); - - /// - /// - /// - /// - /// - /// - public List GetUsersByRoleId(string? id) => Database.Fetch("select UserID from UserRole where RoleID = @0", id); - - /// - /// - /// - /// - /// - /// - /// - public bool SaveUsersByRoleId(string? id, IEnumerable userIds) + var ret = false; + try { - var ret = false; - try - { - Database.BeginTransaction(); - Database.Execute("delete from UserRole where RoleID = @0", id); - Database.InsertBatch("UserRole", userIds.Select(g => new { UserID = g, RoleID = id })); - Database.CompleteTransaction(); - ret = true; - } - catch (Exception) - { - Database.AbortTransaction(); - throw; - } - return ret; + Database.BeginTransaction(); + Database.Execute("delete from RoleGroup where GroupID = @0", groupId); + Database.InsertBatch("RoleGroup", roleIds.Select(g => new { RoleID = g, GroupID = groupId })); + Database.CompleteTransaction(); + ret = true; } + catch (Exception) + { + Database.AbortTransaction(); + throw; + } + return ret; + } + + /// + /// + /// + /// + /// + public List GetRolesByUserId(string? userId) => Database.Fetch("select RoleID from UserRole where UserID = @0", userId); + + /// + /// + /// + /// + /// + /// + public bool SaveRolesByUserId(string? userId, IEnumerable roleIds) + { + var ret = false; + try + { + Database.BeginTransaction(); + Database.Execute("delete from UserRole where UserID = @0", userId); + Database.InsertBatch("UserRole", roleIds.Select(g => new { RoleID = g, UserID = userId })); + Database.CompleteTransaction(); + ret = true; + } + catch (Exception) + { + Database.AbortTransaction(); + throw; + } + return ret; } } diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IRole.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IRole.cs index 1cb66085..891b5e2f 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/IRole.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IRole.cs @@ -16,15 +16,30 @@ public interface IRole /// /// /// - /// + /// /// - List GetUsersByRoleId(string? id); + List GetRolesByGroupId(string? userId); /// /// /// - /// - /// + /// + /// /// - bool SaveUsersByRoleId(string? id, IEnumerable values); + bool SaveRolesByGroupId(string? groupId, IEnumerable roleIds); + + /// + /// + /// + /// + /// + List GetRolesByUserId(string? groupId); + + /// + /// + /// + /// + /// + /// + bool SaveRolesByUserId(string? groupId, IEnumerable roleIds); } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Menus.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Menus.razor.cs index c4e31aae..2f1724a3 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Menus.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Menus.razor.cs @@ -1,21 +1,18 @@ -using BootstrapAdmin.Web.Components; +namespace BootstrapAdmin.Web.Pages.Admin; -namespace BootstrapAdmin.Web.Pages.Admin +public partial class Menus { - public partial class Menus + [Inject] + [NotNull] + private DialogService? DialogService { get; set; } + + private async Task OnAssignmentRoles(DataAccess.Models.Navigation menu) { - [Inject] - [NotNull] - private DialogService? DialogService { get; set; } - - private async Task OnAssignmentRoles(DataAccess.Models.Navigation menu) + var option = new DialogOption() { - var option = new DialogOption() - { - Title = $"分配角色 - {menu}", - }; + Title = $"分配角色 - {menu}", + }; - await DialogService.Show(option); - } + await DialogService.Show(option); } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs index 29e38dd7..f3e641cb 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs @@ -1,5 +1,6 @@ using BootstrapAdmin.DataAccess.Models; -using BootstrapAdmin.Web.Components; +using BootstrapAdmin.Web.Core; +using BootstrapAdmin.Web.Extensions; namespace BootstrapAdmin.Web.Pages.Admin { @@ -19,6 +20,11 @@ namespace BootstrapAdmin.Web.Pages.Admin [Inject] [NotNull] private IGroup? GroupService { get; set; } + + [Inject] + [NotNull] + private IRole? RoleService { get; set; } + private async Task OnAssignmentGroups(User user) { var groups = GroupService.GetAll().ToSelectedItemList(); @@ -33,12 +39,14 @@ namespace BootstrapAdmin.Web.Pages.Admin private async Task OnAssignmentRoles(User user) { - var option = new DialogOption() - { - Title = $"分配角色 - {user}", - }; + var groups = RoleService.GetAll().ToSelectedItemList(); + var values = RoleService.GetRolesByUserId(user.Id); - await DialogService.Show(option); + await DialogService.ShowAssignmentDialog($"分配角色 - {user}", groups, values, () => + { + var ret = RoleService.SaveRolesByUserId(user.Id, values); + return Task.FromResult(ret); + }, ToastService); } } }