feat(console): add UsersController with all CRUD endpoints
This commit is contained in:
parent
a4da3b26fa
commit
5d163b3395
145
Controllers/UsersController.cs
Normal file
145
Controllers/UsersController.cs
Normal file
@ -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<UsersController> _logger;
|
||||||
|
|
||||||
|
public UsersController(IUserService userService, ILogger<UsersController> logger)
|
||||||
|
{
|
||||||
|
_userService = userService;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<ActionResult<object>> 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<ActionResult<UserDto>> 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<ActionResult<UserDto>> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user