From 8ddf9752f4939ea895ef21b56dc15366adc50ad6 Mon Sep 17 00:00:00 2001 From: Mohd Ariff Date: Mon, 23 Dec 2024 08:46:22 +0800 Subject: [PATCH] Update --- .../Views/Item/ItemRegistration.cshtml | 2 +- Controllers/API/RoleAPI.cs | 13 ++++- Views/Admin/CompDeptAdmin.cshtml | 2 +- Views/Admin/RoleAdmin.cshtml | 56 ++++++++++++------- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/Areas/Inventory/Views/Item/ItemRegistration.cshtml b/Areas/Inventory/Views/Item/ItemRegistration.cshtml index 058e150..cfbe9dd 100644 --- a/Areas/Inventory/Views/Item/ItemRegistration.cshtml +++ b/Areas/Inventory/Views/Item/ItemRegistration.cshtml @@ -650,7 +650,7 @@ async fetchCompanies() { try { - const response = await fetch('/InvMainAPI/CompanyDepartmentList', { + const response = await fetch('/AdminAPI/GetDepartmentWithCompanyList', { method: 'POST', // Specify the HTTP method headers: { 'Content-Type': 'application/json' diff --git a/Controllers/API/RoleAPI.cs b/Controllers/API/RoleAPI.cs index 1981e28..4d305fd 100644 --- a/Controllers/API/RoleAPI.cs +++ b/Controllers/API/RoleAPI.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.Models; +using System.ComponentModel.DataAnnotations; namespace PSTW_CentralSystem.Controllers.API { @@ -31,12 +32,12 @@ namespace PSTW_CentralSystem.Controllers.API } [HttpPost("AddRole")] - public async Task AddRole([FromBody] string roleName) + public async Task AddRole([FromBody] RoleInput newRole) { try { - await _roleManager.CreateAsync(new RoleModel { Name = roleName }); - return Ok(new { message = "Role added successfully" }); + await _roleManager.CreateAsync(new RoleModel { Name = newRole.newRoleName, Description = newRole.newRoleDescription ?? null }); + return Ok(new { message = "Role added successfully", newRole = newRole.newRoleName }); } catch (Exception ex) { @@ -57,4 +58,10 @@ namespace PSTW_CentralSystem.Controllers.API return Ok(new { message = "Role deleted successfully" }); } } + public class RoleInput + { + [Required] + public string? newRoleName { get; set; } + public string? newRoleDescription { get; set; } + } } diff --git a/Views/Admin/CompDeptAdmin.cshtml b/Views/Admin/CompDeptAdmin.cshtml index 9575c56..dbafb1b 100644 --- a/Views/Admin/CompDeptAdmin.cshtml +++ b/Views/Admin/CompDeptAdmin.cshtml @@ -172,7 +172,7 @@ }) .then(response => response.json()) .then(data => { - console.log(data) + // console.log(data) this.compDeptList = data ? data : []; if (this.compDeptDatatable != null) { this.compDeptDatatable.clear().destroy(); diff --git a/Views/Admin/RoleAdmin.cshtml b/Views/Admin/RoleAdmin.cshtml index bbab823..be3cf9f 100644 --- a/Views/Admin/RoleAdmin.cshtml +++ b/Views/Admin/RoleAdmin.cshtml @@ -15,6 +15,16 @@

Role List

+
+
+
+ + + + +
+
+
@@ -66,6 +76,8 @@ roleList: null, selectedRole: null, roleDatatable: null, + newRoleName: null, + newRoleDescription: null, }; }, mounted() { @@ -78,13 +90,13 @@ }) .then(response => response.json()) .then(data => { - this.roleList = data.length ? data : []; - this.$nextTick(() => { + this.roleList = data.length ? data : []; + this.$nextTick(() => { if (this.roleDatatable != null) { this.roleDatatable.clear().destroy(); } this.initiateTable(); - }); + }); }) .catch(error => { console.error('There was a problem with the fetch operation:', error); @@ -96,12 +108,14 @@ $('#confirm-dialog').modal('show'); // Show the modal }, - confirmDelete(selectedRole) { + async confirmDelete(selectedRole) { try{ - var response = fetch('/RoleAPI/DeleteRole/' + selectedRole.id, { - method: 'DELETE' + var response = await fetch('/RoleAPI/DeleteRole/' + selectedRole.id, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json' + }, }); - if (response.ok) { this.roleList = this.roleList.filter(role => role.id !== selectedRole.id); this.$nextTick(() => { @@ -160,32 +174,36 @@ this.loading = false; }, - async updateRole(thisUserRole, thisUserId) { + async addRole() { try { - const response = await fetch(`/AdminAPI/UpdateUserStatusAndRole/${thisUserId}`, { - method: 'PATCH', + const response = await fetch(`/RoleAPI/AddRole`, { + method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(thisUserRole) + body: JSON.stringify({ + newRoleName: this.newRoleName, + newRoleDescription: this.newRoleDescription + }) }); 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'); } 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); - - //How to reload the table with new data from this.userList - - this.fetchUsers(); + this.fetchRoles(); + }, + sentenceCapitalization(event) { + const value = event.target.value.trimStart(); + return value.charAt(0).toUpperCase() + value.slice(1); }, } })