update module & roless

This commit is contained in:
ArifHilmi 2024-11-22 16:25:41 +08:00
parent 5569a72e68
commit 630a3175ce
2 changed files with 117 additions and 3 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.DBContext;
using PSTW_CentralSystem.Models;
namespace PSTW_CentralSystem.Controllers.API 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(); var qcList = await _authDbContext.ModuleSettings.Where(x => x.SettingId == id).FirstOrDefaultAsync();
return Json(qcList); return Json(qcList);
} }
[HttpPost("addMethod")]
public async Task<IActionResult> 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<MethodAllowedUserType>();
}
// Add the new module
moduleSetting.MethodAllowedUserType.Add(new MethodAllowedUserType
{
MethodName = module,
AllowedUserTypesArray = Array.Empty<string>()
});
// Save the changes to the database
_authDbContext.ModuleSettings.Update(moduleSetting);
await _authDbContext.SaveChangesAsync();
return Json(moduleSetting);
}
[HttpPost("saveData")]
public async Task<IActionResult> 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);
}
} }
} }

View File

@ -45,7 +45,7 @@
<option v-for="(methods, index) in availableMethod" :key="index" :value="methods">{{ methods }}</option> <option v-for="(methods, index) in availableMethod" :key="index" :value="methods">{{ methods }}</option>
</select> </select>
</div> </div>
<button class="btn col-md-3 f-icon mt-3 d-flex align-items-center" style="height: 36px; width: auto"> <button class="btn col-md-3 f-icon mt-3 d-flex align-items-center" style="height: 36px; width: auto" v-on:click="addMethod(selectedModule)">
<i class=" fas fa-plus-square fa-lg"></i>&ensp;Add method <i class=" fas fa-plus-square fa-lg"></i>&ensp;Add method
</button> </button>
</div> </div>
@ -83,7 +83,7 @@
</div> </div>
</div> </div>
<div class="border-top"> <div class="border-top">
<div class="card-body"> <div class="card-body" v-on:click="saveData">
<button type="button" class="btn btn-primary"> <button type="button" class="btn btn-primary">
Save Save
</button> </button>
@ -183,7 +183,10 @@
if (data != null) { if (data != null) {
this.roleData = data; this.roleData = data;
this.$nextTick(() => { 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); 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() { filterAvailableMethods() {
const moduleMethods = this.moduleData.methodAllowedUserType.map(method => method.methodName); 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