diff --git a/Controllers/API/ModuleAPI.cs b/Controllers/API/ModuleAPI.cs index 558064c..b79a84f 100644 --- a/Controllers/API/ModuleAPI.cs +++ b/Controllers/API/ModuleAPI.cs @@ -32,6 +32,74 @@ namespace PSTW_CentralSystem.Controllers.API return Json(qcList); } + [HttpPost("addMethod")] + public async Task AddMethod(int? id, string? module) + { + + // Retrieve the module setting from the database + var moduleSetting = await _authDbContext.ModuleSettings + .Where(x => x.SettingId == id) + .FirstOrDefaultAsync(); + + if (moduleSetting == null) + { + return NotFound("Module setting not found"); + } + + // Initialize the list if null + if (moduleSetting.MethodAllowedUserType == null) + { + moduleSetting.MethodAllowedUserType = new List(); + } + + // Add the new module + moduleSetting.MethodAllowedUserType.Add(new MethodAllowedUserType + { + MethodName = module, + AllowedUserTypesArray = Array.Empty() + }); + + // Save the changes to the database + _authDbContext.ModuleSettings.Update(moduleSetting); + await _authDbContext.SaveChangesAsync(); + + return Json(moduleSetting); + } + + + [HttpPost("saveData")] + public async Task saveData([FromBody] ModuleSettingModel modelSettingList) + { + var qcList = await _authDbContext.ModuleSettings + .Where(x => x.SettingId == modelSettingList.SettingId) + .FirstOrDefaultAsync(); + + if (qcList != null) + { + + qcList.ModuleName = modelSettingList.ModuleName; + qcList.AllowedUserType = modelSettingList.AllowedUserType; + + if (modelSettingList.MethodAllowedUserType != null) + { + qcList.MethodAllowedUserType = modelSettingList.MethodAllowedUserType + .Select(m => new MethodAllowedUserType + { + MethodName = m.MethodName, + AllowedUserTypesArray = m.AllowedUserTypesArray + }).ToList(); + } + + qcList.ModuleStatus = modelSettingList.ModuleStatus; + qcList.Description = modelSettingList.Description; + + _authDbContext.ModuleSettings.Update(qcList); + await _authDbContext.SaveChangesAsync(); + } + + return Json(qcList); + } + [HttpPost("AddModule")] public async Task AddModule([FromBody] ModuleSettingModel module) { diff --git a/Controllers/AdminController.cs b/Controllers/AdminController.cs index 646bcc5..4248a1b 100644 --- a/Controllers/AdminController.cs +++ b/Controllers/AdminController.cs @@ -42,6 +42,12 @@ namespace PSTW_CentralSystem.Controllers return View(moduleSettings); } + + public IActionResult AddModule() + { + return View(); + } + public IActionResult ModuleCreate() { return View(); diff --git a/PSTW_CentralSystem.sln b/PSTW_CentralSystem.sln index bae8660..c9c5146 100644 --- a/PSTW_CentralSystem.sln +++ b/PSTW_CentralSystem.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSTW_CentralSystem", "PSTW_CentralSystem.csproj", "{1B3D8BB0-F297-4F4B-8C09-6D97CE5D44F1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSTW_CentralSystem", "PSTW_CentralSystem.csproj", "{1B3D8BB0-F297-4F4B-8C09-6D97CE5D44F1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Views/Admin/AddModule.cshtml b/Views/Admin/AddModule.cshtml new file mode 100644 index 0000000..1f2d012 --- /dev/null +++ b/Views/Admin/AddModule.cshtml @@ -0,0 +1,93 @@ + +@{ + ViewData["Title"] = "Add Module"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +
+
+
+
+
+
+
+
+
Settings
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+@section Scripts { + @{ + await Html.RenderPartialAsync("_ValidationScriptsPartial"); + } + + +} diff --git a/Views/Admin/ModuleSetting.cshtml b/Views/Admin/ModuleSetting.cshtml index c566de8..73a5bb4 100644 --- a/Views/Admin/ModuleSetting.cshtml +++ b/Views/Admin/ModuleSetting.cshtml @@ -45,45 +45,45 @@ -
- -
- -
-
-

Tab {{ setMethod.methodName }} selected

-
- -
- + +
+ +
+
+

Tab {{ setMethod.methodName }} selected

+
+ +
+ +
-
-
+
@@ -141,7 +141,7 @@ mounted() { this.fetchXModule(); this.fetchControllerMethodList(); - }, + }, watch: { // Watching allowedUserType directly // 'moduleData.allowedUserType'(newVal, oldVal) { @@ -156,20 +156,20 @@ fetchXModule() { var id = @Model.SettingId fetch('/ModuleAPI/GetXModuleInformation?id=' + id, { - method: 'POST' - }) - .then(response => response.json()) - .then(data => { - console.log(data); - if (data != null) { - this.moduleData = data; - - this.fetchRoleList(); - } + method: 'POST' }) - .catch(error => { - console.error('There was a problem with the fetch operation:', error); - }); + .then(response => response.json()) + .then(data => { + console.log(data); + if (data != null) { + this.moduleData = data; + + this.fetchRoleList(); + } + }) + .catch(error => { + console.error('There was a problem with the fetch operation:', error); + }); }, fetchRoleList() { fetch('/RoleAPI/GetRoleList', { @@ -181,7 +181,10 @@ if (data != null) { this.roleData = data; this.$nextTick(() => { - $(".select2").select2(); // Initialize Select2 after DOM update + $(".select2").select2().on("change", (event) => { + const index = $(event.target).closest('.tab-pane').index(); + this.moduleData.methodAllowedUserType[index].allowedUserTypesArray = $(event.target).val(); + }); }); } }) @@ -191,7 +194,7 @@ }, fetchControllerMethodList() { var moduleName = '@Model.ModuleName' - fetch('/AdminAPI/GetClassAndMethodInformation?moduleName=' + moduleName, { + fetch('/AdminAPI/GetClassAndMethodInformation?moduleName=' + moduleName, { method: 'POST' }) .then(response => response.json()) @@ -229,9 +232,50 @@ console.error('Failed to delete module with status:', error); }); }, + addMethod(module) { + var id = @Model.SettingId; + fetch('/ModuleAPI/addMethod?id=' + id + '&module=' + encodeURIComponent(module), { + method: 'POST' + }) + .then(response => { + if (!response.ok) { + throw new Error('HTTP error! status: ' + response.status); + } + return response.json(); + }) + .then(data => { + console.log('Method added successfully:', data); + alert('Method added successfully'); + this.fetchXModule(); // Refresh the module data + }) + .catch(error => { + console.error('There was a problem when adding the module operation:', error); + alert('Failed to add method: ' + error.message); + }); + }, + saveData() { + fetch('/ModuleAPI/saveData', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(this.moduleData) + }) + .then(response => { + if (!response.ok) { + throw new Error('Failed to save module information'); + } + alert('Module information saved successfully'); + }) + .catch(error => { + console.error('There was a problem with the update operation:', error); + alert('Failed to save data: ' + error.message); + }); + }, + filterAvailableMethods() { const moduleMethods = this.moduleData.methodAllowedUserType.map(method => method.methodName); - this.availableMethod = this.controllerMethodData.methods.filter(method => !moduleMethods.includes(method)); // exclude methods that are already in moduleMethods + this.availableMethod = this.controllerMethodData.methods.filter(method => !moduleMethods.includes(method)); // exclude methods that are already in moduleMethods }, }, })