feat: 更新 IRole 接口方法

This commit is contained in:
Argo-Tianyi 2021-12-21 11:47:03 +08:00
parent 30614bd223
commit 6029e86072
4 changed files with 119 additions and 70 deletions

View File

@ -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
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public RoleService(IDatabase db) => Database = db;
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Role> GetAll() => Database.Fetch<Role>();
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
public List<string> GetRolesByGroupId(string? groupId) => Database.Fetch<string>("select RoleID from RoleGroup where GroupID = @0", groupId);
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
public bool SaveRolesByGroupId(string? groupId, IEnumerable<string> roleIds)
{
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public RoleService(IDatabase db) => Database = db;
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Role> GetAll() => Database.Fetch<Role>();
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<string> GetUsersByRoleId(string? id) => Database.Fetch<string>("select UserID from UserRole where RoleID = @0", id);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="values"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveUsersByRoleId(string? id, IEnumerable<string> 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;
}
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public List<string> GetRolesByUserId(string? userId) => Database.Fetch<string>("select RoleID from UserRole where UserID = @0", userId);
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
public bool SaveRolesByUserId(string? userId, IEnumerable<string> 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;
}
}

View File

@ -16,15 +16,30 @@ public interface IRole
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="userId"></param>
/// <returns></returns>
List<string> GetUsersByRoleId(string? id);
List<string> GetRolesByGroupId(string? userId);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="values"></param>
/// <param name="groupId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
bool SaveUsersByRoleId(string? id, IEnumerable<string> values);
bool SaveRolesByGroupId(string? groupId, IEnumerable<string> roleIds);
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
List<string> GetRolesByUserId(string? groupId);
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
bool SaveRolesByUserId(string? groupId, IEnumerable<string> roleIds);
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}