using System.Text.Json; using AuthHex.Infra.UoW; using AuthHex.Interfaces; using AuthHex.Models; using Core_Archi.Helpers; using Microsoft.AspNetCore.Http; namespace AuthHex.Services.RoleManager; public class RoleManagerService { private readonly IRoleManageRepository _repository; private readonly IUnitOfWork _uow; public RoleManagerService(IRoleManageRepository repository, IUnitOfWork uow) { _repository = repository; _uow = uow; } public async Task CreateRole(object payload, HttpContext httpContext) { await _uow.BeginAsync(); try { var data = DeserializePayload.Deserialize(payload); var code = data.ContainsKey("code") ? data["code"].GetString() : null; var name = data.ContainsKey("name") ? data["name"].GetString() : null; var isSystemRole = data.ContainsKey("isSystemRole") && data["isSystemRole"].ValueKind != JsonValueKind.Null && data["isSystemRole"].GetBoolean(); if (string.IsNullOrWhiteSpace(code)) throw new ArgumentException("code is required"); var role = new Role { RoleId = Guid.NewGuid(), Code = code, Name = string.IsNullOrWhiteSpace(name) ? code : name, IsSystemRole = isSystemRole, CreatedAt = DateTime.UtcNow }; await _repository.AddRoleAsync(role); await _uow.CommitAsync(); return ToDto(role); } catch { await _uow.RollbackAsync(); throw; } } public async Task GetRole(object payload, HttpContext httpContext) { var data = DeserializePayload.Deserialize(payload); if (!data.ContainsKey("roleId") || !Guid.TryParse(data["roleId"].GetString(), out var roleId)) throw new ArgumentException("Invalid roleId"); var role = await _repository.GetRoleByIdAsync(roleId) ?? throw new KeyNotFoundException("Role not found"); return ToDto(role); } public async Task ListRoles(HttpContext httpContext) { var roles = await _repository.ListRolesAsync(); return roles.Select(ToDto).ToList(); } public async Task UpdateRole(object payload, HttpContext httpContext) { await _uow.BeginAsync(); try { var data = DeserializePayload.Deserialize(payload); if (!data.ContainsKey("roleId") || !Guid.TryParse(data["roleId"].GetString(), out var roleId)) throw new ArgumentException("Invalid roleId"); var role = await _repository.GetRoleByIdAsync(roleId) ?? throw new KeyNotFoundException("Role not found"); if (data.ContainsKey("code")) { var code = data["code"].GetString(); if (string.IsNullOrWhiteSpace(code)) throw new ArgumentException("code cannot be empty"); role.Code = code; } if (data.ContainsKey("name")) role.Name = data["name"].GetString(); if (data.ContainsKey("isSystemRole") && data["isSystemRole"].ValueKind != JsonValueKind.Null) role.IsSystemRole = data["isSystemRole"].GetBoolean(); await _uow.CommitAsync(); return ToDto(role); } catch { await _uow.RollbackAsync(); throw; } } public async Task DeleteRole(object payload, HttpContext httpContext) { await _uow.BeginAsync(); try { var data = DeserializePayload.Deserialize(payload); if (!data.ContainsKey("roleId") || !Guid.TryParse(data["roleId"].GetString(), out var roleId)) throw new ArgumentException("Invalid roleId"); var role = await _repository.GetRoleByIdAsync(roleId) ?? throw new KeyNotFoundException("Role not found"); if (await _repository.RoleInUseAsync(roleId)) throw new InvalidOperationException("ROLE_IN_USE: role is assigned to one or more users"); await _repository.DeleteRoleAsync(role); await _uow.CommitAsync(); return new { message = "Role deleted successfully" }; } catch { await _uow.RollbackAsync(); throw; } } private static object ToDto(Role role) => new { role.RoleId, role.Code, role.Name, role.IsSystemRole, role.CreatedAt }; }