using Fengling.Console.Models.Dtos; using Fengling.Console.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Fengling.Console.Controllers; [ApiController] [Route("api/[controller]")] [Authorize] public class RolesController : ControllerBase { private readonly IRoleService _roleService; private readonly ILogger _logger; public RolesController(IRoleService roleService, ILogger logger) { _roleService = roleService; _logger = logger; } [HttpGet] public async Task> GetRoles( [FromQuery] int page = 1, [FromQuery] int pageSize = 10, [FromQuery] string? name = null, [FromQuery] string? tenantId = null) { try { var (items, totalCount) = await _roleService.GetRolesAsync(page, pageSize, name, tenantId); return Ok(new { items, totalCount, page, pageSize }); } catch (Exception ex) { _logger.LogError(ex, "Error getting roles"); return StatusCode(500, new { message = ex.Message }); } } [HttpGet("{id}")] public async Task> GetRole(long id) { try { var role = await _roleService.GetRoleAsync(id); if (role == null) { return NotFound(); } return Ok(role); } catch (Exception ex) { _logger.LogError(ex, "Error getting role {RoleId}", id); return StatusCode(500, new { message = ex.Message }); } } [HttpGet("{id}/users")] public async Task>> GetRoleUsers(long id) { try { var users = await _roleService.GetRoleUsersAsync(id); return Ok(users); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Role not found: {RoleId}", id); return NotFound(); } catch (Exception ex) { _logger.LogError(ex, "Error getting users for role {RoleId}", id); return StatusCode(500, new { message = ex.Message }); } } [HttpPost] public async Task> CreateRole([FromBody] CreateRoleDto dto) { try { var role = await _roleService.CreateRoleAsync(dto); return CreatedAtAction(nameof(GetRole), new { id = role.Id }, role); } catch (InvalidOperationException ex) { _logger.LogWarning(ex, "Validation error creating role"); return BadRequest(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error creating role"); return StatusCode(500, new { message = ex.Message }); } } [HttpPut("{id}")] public async Task UpdateRole(long id, [FromBody] UpdateRoleDto dto) { try { await _roleService.UpdateRoleAsync(id, dto); return NoContent(); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Role not found: {RoleId}", id); return NotFound(); } catch (InvalidOperationException ex) { _logger.LogWarning(ex, "Validation error updating role {RoleId}", id); return BadRequest(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error updating role {RoleId}", id); return StatusCode(500, new { message = ex.Message }); } } [HttpDelete("{id}")] public async Task DeleteRole(long id) { try { await _roleService.DeleteRoleAsync(id); return NoContent(); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Role not found: {RoleId}", id); return NotFound(); } catch (InvalidOperationException ex) { _logger.LogWarning(ex, "Validation error deleting role {RoleId}", id); return BadRequest(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error deleting role {RoleId}", id); return StatusCode(500, new { message = ex.Message }); } } [HttpDelete("{id}/users/{userId}")] public async Task RemoveUserFromRole(long id, long userId) { try { await _roleService.RemoveUserFromRoleAsync(id, userId); return NoContent(); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Role or user not found: RoleId={RoleId}, UserId={UserId}", id, userId); return NotFound(); } catch (InvalidOperationException ex) { _logger.LogWarning(ex, "Validation error removing user {UserId} from role {RoleId}", userId, id); return BadRequest(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error removing user {UserId} from role {RoleId}", userId, id); return StatusCode(500, new { message = ex.Message }); } } }