From 5d163b339580556b4731b8a1677799eaa3d82d5a Mon Sep 17 00:00:00 2001 From: Sam <315859133@qq.com> Date: Thu, 5 Feb 2026 14:20:11 +0800 Subject: [PATCH] feat(console): add UsersController with all CRUD endpoints --- Controllers/UsersController.cs | 145 +++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 Controllers/UsersController.cs diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs new file mode 100644 index 0000000..4147cca --- /dev/null +++ b/Controllers/UsersController.cs @@ -0,0 +1,145 @@ +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 UsersController : ControllerBase +{ + private readonly IUserService _userService; + private readonly ILogger _logger; + + public UsersController(IUserService userService, ILogger logger) + { + _userService = userService; + _logger = logger; + } + + [HttpGet] + public async Task> GetUsers( + [FromQuery] int page = 1, + [FromQuery] int pageSize = 10, + [FromQuery] string? userName = null, + [FromQuery] string? email = null, + [FromQuery] string? tenantId = null) + { + try + { + var (items, totalCount) = await _userService.GetUsersAsync(page, pageSize, userName, email, tenantId); + return Ok(new { items, totalCount, page, pageSize }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error getting users"); + return StatusCode(500, new { message = ex.Message }); + } + } + + [HttpGet("{id}")] + public async Task> GetUser(long id) + { + try + { + var user = await _userService.GetUserAsync(id); + if (user == null) + { + return NotFound(); + } + return Ok(user); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error getting user {UserId}", id); + return StatusCode(500, new { message = ex.Message }); + } + } + + [HttpPost] + public async Task> CreateUser([FromBody] CreateUserDto dto) + { + try + { + var user = await _userService.CreateUserAsync(dto); + return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user); + } + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Validation error creating user"); + return BadRequest(new { message = ex.Message }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error creating user"); + return StatusCode(500, new { message = ex.Message }); + } + } + + [HttpPut("{id}")] + public async Task UpdateUser(long id, [FromBody] UpdateUserDto dto) + { + try + { + await _userService.UpdateUserAsync(id, dto); + return NoContent(); + } + catch (KeyNotFoundException ex) + { + _logger.LogWarning(ex, "User not found: {UserId}", id); + return NotFound(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error updating user {UserId}", id); + return StatusCode(500, new { message = ex.Message }); + } + } + + [HttpPut("{id}/password")] + public async Task ResetPassword(long id, [FromBody] ResetPasswordDto dto) + { + try + { + await _userService.ResetPasswordAsync(id, dto); + return NoContent(); + } + catch (KeyNotFoundException ex) + { + _logger.LogWarning(ex, "User not found: {UserId}", id); + return NotFound(); + } + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Validation error resetting password for user {UserId}", id); + return BadRequest(new { message = ex.Message }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error resetting password for user {UserId}", id); + return StatusCode(500, new { message = ex.Message }); + } + } + + [HttpDelete("{id}")] + public async Task DeleteUser(long id) + { + try + { + await _userService.DeleteUserAsync(id); + return NoContent(); + } + catch (KeyNotFoundException ex) + { + _logger.LogWarning(ex, "User not found: {UserId}", id); + return NotFound(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error deleting user {UserId}", id); + return StatusCode(500, new { message = ex.Message }); + } + } +}