This commit is contained in:
MOHD ARIFF 2024-12-23 08:46:22 +08:00
parent b5d3829457
commit 8ddf9752f4
4 changed files with 49 additions and 24 deletions

View File

@ -650,7 +650,7 @@
async fetchCompanies() { async fetchCompanies() {
try { try {
const response = await fetch('/InvMainAPI/CompanyDepartmentList', { const response = await fetch('/AdminAPI/GetDepartmentWithCompanyList', {
method: 'POST', // Specify the HTTP method method: 'POST', // Specify the HTTP method
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'

View File

@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.DBContext;
using PSTW_CentralSystem.Models; using PSTW_CentralSystem.Models;
using System.ComponentModel.DataAnnotations;
namespace PSTW_CentralSystem.Controllers.API namespace PSTW_CentralSystem.Controllers.API
{ {
@ -31,12 +32,12 @@ namespace PSTW_CentralSystem.Controllers.API
} }
[HttpPost("AddRole")] [HttpPost("AddRole")]
public async Task<IActionResult> AddRole([FromBody] string roleName) public async Task<IActionResult> AddRole([FromBody] RoleInput newRole)
{ {
try try
{ {
await _roleManager.CreateAsync(new RoleModel { Name = roleName }); await _roleManager.CreateAsync(new RoleModel { Name = newRole.newRoleName, Description = newRole.newRoleDescription ?? null });
return Ok(new { message = "Role added successfully" }); return Ok(new { message = "Role added successfully", newRole = newRole.newRoleName });
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -57,4 +58,10 @@ namespace PSTW_CentralSystem.Controllers.API
return Ok(new { message = "Role deleted successfully" }); return Ok(new { message = "Role deleted successfully" });
} }
} }
public class RoleInput
{
[Required]
public string? newRoleName { get; set; }
public string? newRoleDescription { get; set; }
}
} }

View File

@ -172,7 +172,7 @@
}) })
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
console.log(data) // console.log(data)
this.compDeptList = data ? data : []; this.compDeptList = data ? data : [];
if (this.compDeptDatatable != null) { if (this.compDeptDatatable != null) {
this.compDeptDatatable.clear().destroy(); this.compDeptDatatable.clear().destroy();

View File

@ -15,6 +15,16 @@
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<h4 class="card-title">Role List</h4> <h4 class="card-title">Role List</h4>
<div class="row">
<div class="col-md-6 col-lg-6">
<form class="form-group" v-on:submit.prevent="addRole">
<label class="form-label">Role Name</label>
<input type="text" class="form-control" placeholder="Role Name" v-model="newRoleName" v-on:input="newRoleName=sentenceCapitalization($event)" required>
<textarea class="form-control" placeholder="Role Description" v-model="newRoleDescription" v-on:input="newRoleDescription=sentenceCapitalization($event)"></textarea>
<button type="submit" class="btn btn-primary m-1">Add</button>
</form>
</div>
</div>
<div class="col-md-12 col-lg-12"> <div class="col-md-12 col-lg-12">
<div> <div>
<table class="table table-bordered table-hover table-striped no-wrap align-middle" id="roleDatatable" style="width:100%;border-style: solid; border-width: 1px"></table> <table class="table table-bordered table-hover table-striped no-wrap align-middle" id="roleDatatable" style="width:100%;border-style: solid; border-width: 1px"></table>
@ -66,6 +76,8 @@
roleList: null, roleList: null,
selectedRole: null, selectedRole: null,
roleDatatable: null, roleDatatable: null,
newRoleName: null,
newRoleDescription: null,
}; };
}, },
mounted() { mounted() {
@ -96,12 +108,14 @@
$('#confirm-dialog').modal('show'); // Show the modal $('#confirm-dialog').modal('show'); // Show the modal
}, },
confirmDelete(selectedRole) { async confirmDelete(selectedRole) {
try{ try{
var response = fetch('/RoleAPI/DeleteRole/' + selectedRole.id, { var response = await fetch('/RoleAPI/DeleteRole/' + selectedRole.id, {
method: 'DELETE' method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
}); });
if (response.ok) { if (response.ok) {
this.roleList = this.roleList.filter(role => role.id !== selectedRole.id); this.roleList = this.roleList.filter(role => role.id !== selectedRole.id);
this.$nextTick(() => { this.$nextTick(() => {
@ -160,32 +174,36 @@
this.loading = false; this.loading = false;
}, },
async updateRole(thisUserRole, thisUserId) { async addRole() {
try try
{ {
const response = await fetch(`/AdminAPI/UpdateUserStatusAndRole/${thisUserId}`, { const response = await fetch(`/RoleAPI/AddRole`, {
method: 'PATCH', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(thisUserRole) body: JSON.stringify({
newRoleName: this.newRoleName,
newRoleDescription: this.newRoleDescription
})
}); });
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to update role'); throw new Error('Failed to add role');
} }
const data = await response.json();
console.log('Role updated successfully'); console.log('Role updated successfully');
} }
catch (error) { catch (error) {
console.error('Failed to update role:', error); const errorResponse = await response.json();
console.error('Failed to add role:', errorResponse.message);
} }
// console.log("User ID:" + thisUserId + " Role:" + thisUserRole); this.fetchRoles();
},
//How to reload the table with new data from this.userList sentenceCapitalization(event) {
const value = event.target.value.trimStart();
this.fetchUsers(); return value.charAt(0).toUpperCase() + value.slice(1);
}, },
} }
}) })