feat(console): add RolesController with all CRUD endpoints

This commit is contained in:
Sam 2026-02-05 14:21:21 +08:00
parent 88c4bfa9cd
commit 4d7abd6fdb

View File

@ -0,0 +1,174 @@
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<RolesController> _logger;
public RolesController(IRoleService roleService, ILogger<RolesController> logger)
{
_roleService = roleService;
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<object>> 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<ActionResult<RoleDto>> 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<ActionResult<IEnumerable<UserDto>>> 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<ActionResult<RoleDto>> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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 });
}
}
}