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 }); } } }