using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.Models; using System.Reflection; using static System.Runtime.InteropServices.JavaScript.JSType; using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.ComponentModel.DataAnnotations; namespace PSTW_CentralSystem.Controllers.API { [ApiController] [Route("[controller]")] public class AdminAPI : Controller { private readonly ILogger _logger; private readonly IdentityDBContext _identityDbContext; private readonly UserManager _userManager; private readonly SignInManager _signInManager; private readonly RoleManager _roleManager; public AdminAPI(ILogger logger, IdentityDBContext authDbContext, UserManager userManager, SignInManager signInManager, RoleManager roleManager) { _logger = logger; _identityDbContext = authDbContext; _userManager = userManager; _signInManager = signInManager; _roleManager = roleManager; } [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(); // Fetch all users excluding those with roles SuperAdmin or SystemAdmin var allUsers = await _identityDbContext.Users .Include(u => u.Department.Company) .ToListAsync(); if (userRole == null || userRole.Count == 0) { foreach (var u in allUsers) { var roles = await _userManager.GetRolesAsync(u); if (!roles.Contains("SuperAdmin") && !roles.Contains("SystemAdmin")) { userInfo.Add(u); } } } else { userInfo = allUsers; } var userList = userInfo.Select(u => new { id = u.Id, email = u.NormalizedEmail, company = u.Department?.Company?.CompanyName, department = u.Department?.DepartmentName, role = _userManager.GetRolesAsync(u).Result, status = u.UserInfoStatus, }).ToList(); return Ok(new { UserInfo = userList }); } catch (Exception ex) { return StatusCode(500, $"An error occurred: {ex.Message}"); } } [HttpPatch("UpdateUserStatusAndRole/{id}")] public async Task UpdateUserStatusAndRole(int id, [FromBody] string role) { try { var user = await _identityDbContext.Users.FindAsync(id); if (user == null) { return NotFound(new { message = "User not found" }); } var existingUserRoles = await _userManager.GetRolesAsync(user); if (existingUserRoles != null && existingUserRoles.Count > 0) { await _userManager.RemoveFromRolesAsync(user, existingUserRoles); } await _userManager.AddToRoleAsync(user, role); user.UserInfoStatus = 1; await _identityDbContext.SaveChangesAsync(); return Ok(new { message = "User updated successfully" }); } catch (Exception ex) { return StatusCode(500, $"An error occurred: {ex.Message}"); } } [HttpPost("GetDepartmentWithCompanyList")] public async Task GetDepartmentWithCompanyList() { var companyList = await _identityDbContext.Companies .Include(c => c.Departments) .Select(c => new { c.CompanyId, c.CompanyName, Departments = c.Departments .OrderBy(d => d.DepartmentId) .Select(d => new { d.DepartmentId, d.DepartmentName, d.DepartmentCode }) }) .ToListAsync(); return Json(companyList); } [HttpPost("GetDepartmentWithCompany")] public async Task GetDepartmentWithCompany(int companyId, int departmentId) { var departmentList = await _identityDbContext.Departments.FirstOrDefaultAsync(d => d.DepartmentId == departmentId); var companyList = await _identityDbContext.Companies.FirstOrDefaultAsync(c => c.CompanyId == companyId); // Create a new list to store departments with their company name var departmentWithCompany = new DepartmentCompany { DepartmentId = departmentList!.DepartmentId, DepartmentName = departmentList.DepartmentName, CompanyId = departmentList.CompanyId, CompanyName = companyList?.CompanyName, }; // Return the constructed list as JSON return departmentWithCompany; } [HttpPost("AddCompanyDepartment")] public async Task AddCompanyDepartment([FromBody] UpdateDepartmentCompany departmentCompanyDetails) { try { CompanyModel companyModel = new CompanyModel { CompanyName = departmentCompanyDetails.Company! }; _identityDbContext.Companies.Add(companyModel); await _identityDbContext.SaveChangesAsync(); foreach (var department in departmentCompanyDetails.Department!) { DepartmentModel departmentModel = new DepartmentModel { CompanyId = companyModel.CompanyId, DepartmentName = department.DepartmentName ?? string.Empty, DepartmentCode = department.DepartmentCode ?? string.Empty }; _identityDbContext.Departments.Add(departmentModel); await _identityDbContext.SaveChangesAsync(); } return Ok( new { message = "Company and department added successfully" }); } catch (Exception ex) { return StatusCode(500, new { message = $"An error occurred: {ex.Message}" }); } } public class UpdateDepartmentCompany { [Required] public string? Company { get; set; } [Required] public List? Department { get; set; } } public class DepartmentInfo { [Required] public string? DepartmentName { get; set; } [Required] public string? DepartmentCode { get; set; } } public class DepartmentCompany { public int DepartmentId { get; set; } public string? DepartmentName { get; set; } public int CompanyId { get; set; } public string? CompanyName { get; set; } } } }