BotSharp/src/Infrastructure/BotSharp.Core/Roles/Services/RoleService.cs

66 lines
2 KiB
C#
Raw Normal View History

2024-11-13 23:16:49 +00:00
using BotSharp.Abstraction.Users.Enums;
using System.Reflection;
namespace BotSharp.Core.Roles.Services;
public class RoleService : IRoleService
{
private readonly IServiceProvider _services;
private readonly ILogger<RoleService> _logger;
public RoleService(
IServiceProvider services,
ILogger<RoleService> logger)
{
_services = services;
_logger = logger;
}
2024-11-14 23:33:26 +00:00
public async Task<bool> RefreshRoles()
{
var allRoles = await GetRoleOptions();
var roles = allRoles.Select(x => new Role { Id = Guid.NewGuid().ToString(), Name = x }).ToList();
var db = _services.GetRequiredService<IBotSharpRepository>();
return db.RefreshRoles(roles);
}
2024-11-13 23:16:49 +00:00
public async Task<IEnumerable<string>> GetRoleOptions()
{
var fields = typeof(UserRole).GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.IsLiteral && !x.IsInitOnly).ToList();
return fields.Select(x => x.GetValue(null)?.ToString())
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToList();
}
public async Task<IEnumerable<Role>> GetRoles(RoleFilter filter)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var roles = db.GetRoles(filter);
return roles;
}
public async Task<Role?> GetRoleDetails(string roleId)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
2024-11-14 23:33:26 +00:00
var role = db.GetRoleDetails(roleId, includeAgent: true);
2024-11-13 23:16:49 +00:00
return role;
}
public async Task<bool> UpdateRole(Role role, bool isUpdateRoleAgents = false)
{
if (role == null) return false;
if (string.IsNullOrEmpty(role.Id))
{
role.Id = Guid.NewGuid().ToString();
}
var db = _services.GetRequiredService<IBotSharpRepository>();
return db.UpdateRole(role, isUpdateRoleAgents);
}
}