From b378c73152ba9de0de29c5e51646c0a18a1b9170 Mon Sep 17 00:00:00 2001 From: ArifHilmi Date: Tue, 26 Nov 2024 12:31:45 +0800 Subject: [PATCH] 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 }, } })