From 630a3175ce69554b11c44ef84a41359a3064c2ff Mon Sep 17 00:00:00 2001 From: ArifHilmi Date: Fri, 22 Nov 2024 16:25:41 +0800 Subject: [PATCH 1/4] update module & roless --- Controllers/API/ModuleAPI.cs | 70 ++++++++++++++++++++++++++++++++ Views/Admin/ModuleSetting.cshtml | 50 +++++++++++++++++++++-- 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/Controllers/API/ModuleAPI.cs b/Controllers/API/ModuleAPI.cs index ec7d77d..71cd97b 100644 --- a/Controllers/API/ModuleAPI.cs +++ b/Controllers/API/ModuleAPI.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PSTW_CentralSystem.DBContext; +using PSTW_CentralSystem.Models; namespace PSTW_CentralSystem.Controllers.API { @@ -29,5 +30,74 @@ namespace PSTW_CentralSystem.Controllers.API var qcList = await _authDbContext.ModuleSettings.Where(x => x.SettingId == id).FirstOrDefaultAsync(); 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); + } + } } diff --git a/Views/Admin/ModuleSetting.cshtml b/Views/Admin/ModuleSetting.cshtml index e0daeb7..6b963bc 100644 --- a/Views/Admin/ModuleSetting.cshtml +++ b/Views/Admin/ModuleSetting.cshtml @@ -45,7 +45,7 @@ - @@ -83,7 +83,7 @@
-
+
@@ -183,7 +183,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(); + }); }); } }) @@ -231,6 +234,47 @@ 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 From 87c447e93525313e94b287a861f1cd70f99248b8 Mon Sep 17 00:00:00 2001 From: ArifHilmi Date: Tue, 26 Nov 2024 09:48:19 +0800 Subject: [PATCH 2/4] add model --- Controllers/API/ModuleAPI.cs | 19 +++++++ Controllers/AdminController.cs | 6 +++ PSTW_CentralSystem.sln | 2 +- Views/Admin/AddModule.cshtml | 93 ++++++++++++++++++++++++++++++++++ appsettings.json | 3 +- 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 Views/Admin/AddModule.cshtml diff --git a/Controllers/API/ModuleAPI.cs b/Controllers/API/ModuleAPI.cs index 71cd97b..cbb9cab 100644 --- a/Controllers/API/ModuleAPI.cs +++ b/Controllers/API/ModuleAPI.cs @@ -99,5 +99,24 @@ namespace PSTW_CentralSystem.Controllers.API return Json(qcList); } + [HttpPost("addData")] + public async Task addData([FromBody] ModuleSettingModel modelSettingList) + { + var existingModule = await _authDbContext.ModuleSettings.Where(x => x.ModuleName.ToLower() == modelSettingList.ModuleName.ToLower()).FirstOrDefaultAsync(); + + if (existingModule != null) + { + return BadRequest("Module name already exists."); // Return a 400 Bad Request + } + + // Add new module + modelSettingList.ModuleStatus = 1; // Default status + + _authDbContext.ModuleSettings.Add(modelSettingList); + await _authDbContext.SaveChangesAsync(); + + + return Json(modelSettingList); + } } } 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..11c0e6d --- /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/appsettings.json b/appsettings.json index 7179cb0..669a30e 100644 --- a/appsettings.json +++ b/appsettings.json @@ -2,8 +2,9 @@ "ConnectionStrings": { //"DefaultConnection": "Server=localhost;uid=root;Password='';Database=web_interface;" //"DefaultConnection": "server=175.136.244.102;user id=root;password=tw_mysql_root;port=3306;database=web_interface" - "DefaultConnection": "Server=219.92.7.60;Port=3307;uid=installer;password='pstw_mysql_installer';database=pstw_cs;" //DB_dev connection + //"DefaultConnection": "Server=219.92.7.60;Port=3307;uid=installer;password='pstw_mysql_installer';database=pstw_cs;", //DB_dev connection //"DefaultConnection": "Server=219.92.7.60;Port=3307;uid=intern;password='intern_mysql_acct';database=web_interface;"//DB_dev connection + "DefaultConnection": "Server=127.0.0.1;Port=3306;user=root;password=;database=pstw_cs;" //DB_dev connection - Arif Hilmi }, "Logging": { "LogLevel": { From 09fa8fc6048867cc659e108469b21a59ad3646c6 Mon Sep 17 00:00:00 2001 From: ArifHilmi Date: Tue, 26 Nov 2024 11:40:53 +0800 Subject: [PATCH 3/4] reversed pull --- Views/Admin/AddModule.cshtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Views/Admin/AddModule.cshtml b/Views/Admin/AddModule.cshtml index 11c0e6d..1f2d012 100644 --- a/Views/Admin/AddModule.cshtml +++ b/Views/Admin/AddModule.cshtml @@ -58,7 +58,7 @@ moduleData:{ ModuleName : null, AllowedUserType : null, - MethodAllowedUserType : "[]", + MethodAllowedUserType : null, ModuleStatus : "1", Description : null, } @@ -66,7 +66,7 @@ }, methods: { saveData() { - fetch('/ModuleAPI/addData', { + fetch('/ModuleAPI/AddModule', { method: 'POST', headers: { 'Content-Type': 'application/json' From b378c73152ba9de0de29c5e51646c0a18a1b9170 Mon Sep 17 00:00:00 2001 From: ArifHilmi Date: Tue, 26 Nov 2024 12:31:45 +0800 Subject: [PATCH 4/4] update --- Controllers/API/ModuleAPI.cs | 33 +++++++------ Views/Admin/ModuleSetting.cshtml | 82 ++++++++++++++++---------------- 2 files changed, 60 insertions(+), 55 deletions(-) diff --git a/Controllers/API/ModuleAPI.cs b/Controllers/API/ModuleAPI.cs index cbb9cab..48b2f6c 100644 --- a/Controllers/API/ModuleAPI.cs +++ b/Controllers/API/ModuleAPI.cs @@ -99,24 +99,29 @@ namespace PSTW_CentralSystem.Controllers.API return Json(qcList); } - [HttpPost("addData")] - public async Task addData([FromBody] ModuleSettingModel modelSettingList) + [HttpPost("AddModule")] + public async Task AddModule([FromBody] ModuleSettingModel module) { - var existingModule = await _authDbContext.ModuleSettings.Where(x => x.ModuleName.ToLower() == modelSettingList.ModuleName.ToLower()).FirstOrDefaultAsync(); - - if (existingModule != null) + if (!ModelState.IsValid) { - return BadRequest("Module name already exists."); // Return a 400 Bad Request + return BadRequest(ModelState); + } + if (module == null) + { + return NotFound("Module is null"); } - // Add new module - modelSettingList.ModuleStatus = 1; // Default status - - _authDbContext.ModuleSettings.Add(modelSettingList); - await _authDbContext.SaveChangesAsync(); - - - return Json(modelSettingList); + try + { + _authDbContext.ModuleSettings.Add(module); + await _authDbContext.SaveChangesAsync(); + var updatedList = await _authDbContext.ModuleSettings.ToListAsync(); + return Json(updatedList); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } } } } diff --git a/Views/Admin/ModuleSetting.cshtml b/Views/Admin/ModuleSetting.cshtml index 6b963bc..426f3b6 100644 --- a/Views/Admin/ModuleSetting.cshtml +++ b/Views/Admin/ModuleSetting.cshtml @@ -50,37 +50,37 @@
- -
- -
-
-

Tab {{ setMethod.methodName }} selected

-
- -
- + +
+ +
+
+

Tab {{ setMethod.methodName }} selected

+
+ +
+ +
-
@@ -143,7 +143,7 @@ mounted() { this.fetchXModule(); this.fetchControllerMethodList(); - }, + }, watch: { // Watching allowedUserType directly // 'moduleData.allowedUserType'(newVal, oldVal) { @@ -158,20 +158,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', { @@ -196,7 +196,7 @@ }, fetchControllerMethodList() { var moduleName = '@Model.ModuleName' - fetch('/AdminAPI/GetClassAndMethodInformation?moduleName=' + moduleName, { + fetch('/AdminAPI/GetClassAndMethodInformation?moduleName=' + moduleName, { method: 'POST' }) .then(response => response.json()) @@ -277,7 +277,7 @@ 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 }, } })