From b5d382945736494dfa9184be1ef667591bdf4f3d Mon Sep 17 00:00:00 2001 From: Mohd Ariff Date: Thu, 19 Dec 2024 16:30:59 +0800 Subject: [PATCH] Update --- Controllers/API/AdminAPI.cs | 8 +- Controllers/API/RoleAPI.cs | 44 ++++++-- Views/Admin/Index.cshtml | 10 +- Views/Admin/RoleAdmin.cshtml | 193 +++++++++++++++++++++++++++++++++++ Views/Admin/UserAdmin.cshtml | 2 +- 5 files changed, 238 insertions(+), 19 deletions(-) create mode 100644 Views/Admin/RoleAdmin.cshtml diff --git a/Controllers/API/AdminAPI.cs b/Controllers/API/AdminAPI.cs index 206d35f..e711d1d 100644 --- a/Controllers/API/AdminAPI.cs +++ b/Controllers/API/AdminAPI.cs @@ -166,13 +166,6 @@ namespace PSTW_CentralSystem.Controllers.API } } - [HttpPost("GetRoleList")] - public async Task GetRoleList() - { - var roles = await _roleManager.Roles.Select(r => new { r.Id, r.Name }).Where(r => r.Name != "SuperAdmin" && r.Name != "SystemAdmin").ToListAsync(); - return Json(roles); - } - [HttpPost("GetDepartmentWithCompanyList")] public async Task GetDepartmentWithCompanyList() { @@ -241,6 +234,7 @@ namespace PSTW_CentralSystem.Controllers.API return StatusCode(500, new { message = $"An error occurred: {ex.Message}" }); } } + public class UpdateDepartmentCompany { [Required] diff --git a/Controllers/API/RoleAPI.cs b/Controllers/API/RoleAPI.cs index cbb0961..1981e28 100644 --- a/Controllers/API/RoleAPI.cs +++ b/Controllers/API/RoleAPI.cs @@ -1,28 +1,60 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PSTW_CentralSystem.DBContext; +using PSTW_CentralSystem.Models; namespace PSTW_CentralSystem.Controllers.API { [ApiController] [Route("[controller]")] + [Authorize] public class RoleAPI : Controller { - - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly IdentityDBContext _authDbContext; + private readonly RoleManager _roleManager; - public RoleAPI(ILogger logger, IdentityDBContext authDbContext) + public RoleAPI(ILogger logger, IdentityDBContext authDbContext, RoleManager roleManager) { _logger = logger; _authDbContext = authDbContext; + _roleManager = roleManager; } [HttpPost("GetRoleList")] public async Task GetRoleList() { - var roleList = await _authDbContext.Roles.Where(r => r.Id != 1 && r.Id != 2).ToListAsync(); - return Json(roleList); + var roles = await _roleManager.Roles.Select(r => new { r.Id, r.Name, r.Description }).Where(r => r.Name != "SuperAdmin" && r.Name != "SystemAdmin").ToListAsync(); + return Json(roles); + } + + [HttpPost("AddRole")] + public async Task AddRole([FromBody] string roleName) + { + try + { + await _roleManager.CreateAsync(new RoleModel { Name = roleName }); + return Ok(new { message = "Role added successfully" }); + } + catch (Exception ex) + { + return StatusCode(500, new { message = $"An error occurred: {ex.Message}" }); + } + } + + [HttpDelete("DeleteRole/{id}")] + public async Task DeleteRole(string id) + { + var role = await _roleManager.FindByIdAsync(id); + if (role == null) + { + return NotFound(new { message = "Role not found" }); + } + + await _roleManager.DeleteAsync(role); + return Ok(new { message = "Role deleted successfully" }); } } } diff --git a/Views/Admin/Index.cshtml b/Views/Admin/Index.cshtml index 052f8fe..4025961 100644 --- a/Views/Admin/Index.cshtml +++ b/Views/Admin/Index.cshtml @@ -11,21 +11,21 @@

- +

-
Dashboard
+
User Administration
- +

- +

-
User Administration
+
Role Administration
diff --git a/Views/Admin/RoleAdmin.cshtml b/Views/Admin/RoleAdmin.cshtml new file mode 100644 index 0000000..bbab823 --- /dev/null +++ b/Views/Admin/RoleAdmin.cshtml @@ -0,0 +1,193 @@ +@* + For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 +*@ +@{ + ViewData["Title"] = "Role Administration"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +

+ @* Create New *@ +

+
+
+
+
+
+

Role List

+
+
+
+
+
+
+
+
+
+ + +
+@section Scripts { + @{ + await Html.RenderPartialAsync("_ValidationScriptsPartial"); + } + +} diff --git a/Views/Admin/UserAdmin.cshtml b/Views/Admin/UserAdmin.cshtml index 9051bd5..0580b8e 100644 --- a/Views/Admin/UserAdmin.cshtml +++ b/Views/Admin/UserAdmin.cshtml @@ -93,7 +93,7 @@ }); }, async fetchRoles() { - fetch('/AdminAPI/GetRoleList', { + fetch('/RoleAPI/GetRoleList', { method: 'POST' }) .then(response => response.json())