using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.Models; using System.Reflection; namespace PSTW_CentralSystem.Controllers.API { [ApiController] [Route("[controller]")] public class AdminAPI : Controller { private readonly ILogger _logger; private readonly IdentityDBContext _authDbContext; private readonly UserManager _userManager; public AdminAPI(ILogger logger, IdentityDBContext authDbContext, UserManager userManager) { _logger = logger; _authDbContext = authDbContext; _userManager = userManager; } [HttpPost("GetClassAndMethodInformation")] public async Task 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 GetListClassAndMethodInformation() { var controllerAndMethodList = new List(); // 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); } [HttpPost("GetUserList")] public async Task GetUserList() { try { var user = await _userManager.GetUserAsync(User); var userRole = await _userManager.GetRolesAsync(user??new UserModel()); List userInfo = new List(); if (userRole == null || userRole.Count == 0) { // Fetch all users excluding those with roles SuperAdmin or SystemAdmin var allUsers = await _authDbContext.Users .Include(u => u.Department) .ToListAsync(); foreach (var u in allUsers) { var roles = await _userManager.GetRolesAsync(u); if (!roles.Contains("SuperAdmin") && !roles.Contains("SystemAdmin")) { userInfo.Add(u); } } } else { userInfo = await _authDbContext.Users.Include(u => u.Department).ToListAsync(); } return Ok(new { UserInfo = userInfo }); } catch (Exception ex) { return StatusCode(500, $"An error occurred: {ex.Message}"); } } } }