PSTW_CentralizeSystem/Controllers/API/AdminAPI.cs
2024-12-04 16:29:41 +08:00

79 lines
3.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PSTW_CentralSystem.DBContext;
using System.Reflection;
namespace PSTW_CentralSystem.Controllers.API
{
[ApiController]
[Route("[controller]")]
public class AdminAPI : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly AuthDBContext _authDbContext;
public AdminAPI(ILogger<HomeController> logger, AuthDBContext authDbContext)
{
_logger = logger;
_authDbContext = authDbContext;
}
[HttpPost("GetClassAndMethodInformation")]
public async Task<IActionResult> GetClassAndMethodInformation(string moduleName)
{
var controllerAndMethodList = new object();
// Get the assembly containing the controllers
var assembly = Assembly.GetExecutingAssembly();
// Get all types in the assembly (controllers will typically be in the "Controllers" namespace)
//var controllerTypes = await Task.Run(() => assembly.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type) && type.IsClass && type.CompanyName.Contains("Controller") && type.CompanyName != "AdminController") .ToList());
var controllerTypes = await Task.Run(() => assembly.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type) && !type.Name.Contains("API") && type.IsClass && type.Name.Contains(moduleName)).FirstOrDefault());
// Iterate over the controller types and get their methods
var methods = controllerTypes?.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.DeclaringType == controllerTypes) // Filter methods declared directly in the controller (ignoring inherited ones)
.Select(m => m.Name) // Get the method names
.ToList();
controllerAndMethodList = (new
{
Controller = controllerTypes?.Name,
Methods = methods
});
// Return the list as JSON
return Json(controllerAndMethodList);
}
[HttpPost("GetListClassAndMethodInformation")]
public async Task<IActionResult> GetListClassAndMethodInformation()
{
var controllerAndMethodList = new List<object>();
// Get the assembly containing the controllers
var assembly = Assembly.GetExecutingAssembly();
// Get all types in the assembly (controllers will typically be in the "Controllers" namespace)
//var controllerTypes = await Task.Run(() => assembly.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type) && type.IsClass && type.CompanyName.Contains("Controller") && type.CompanyName != "AdminController") .ToList());
var controllerTypes = await Task.Run(() => assembly.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type) && type.IsClass && !type.Name.Contains("API") && !type.Name.Contains("Admin")).ToList());
// Iterate over the controller types and get their methods
foreach (var controllerType in controllerTypes) {
var methods = controllerType?.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.DeclaringType == controllerType) // Filter methods declared directly in the controller (ignoring inherited ones)
.Select(m => m.Name) // Get the method names
.ToList();
controllerAndMethodList.Add(new
{
Controller = controllerType?.Name.Replace("Controller", string.Empty),
Methods = methods
});
}
// Return the list as JSON
return Json(controllerAndMethodList);
}
}
}