From 630a3175ce69554b11c44ef84a41359a3064c2ff Mon Sep 17 00:00:00 2001 From: ArifHilmi Date: Fri, 22 Nov 2024 16:25:41 +0800 Subject: [PATCH] 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