91 lines
2.3 KiB
C#
91 lines
2.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MilliSim.Common.Dtos;
|
|
using MilliSim.Common.Models;
|
|
using MilliSim.Services;
|
|
|
|
namespace MilliSim.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/users")]
|
|
[Authorize]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly IUserService _userService;
|
|
|
|
public UsersController(IUserService userService)
|
|
{
|
|
_userService = userService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户列表
|
|
/// </summary>
|
|
[HttpGet]
|
|
[Authorize(Policy = "Staff")]
|
|
public async Task<ApiResponse<PagedResult<UserDto>>> GetUsers([FromQuery] UserQueryRequest request)
|
|
{
|
|
return await _userService.GetUsersAsync(request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户详情
|
|
/// </summary>
|
|
[HttpGet("{id:long}")]
|
|
[Authorize(Policy = "Staff")]
|
|
public async Task<ApiResponse<UserDto>> GetUserById(long id)
|
|
{
|
|
return await _userService.GetUserByIdAsync(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建用户
|
|
/// </summary>
|
|
[HttpPost]
|
|
[Authorize(Policy = "Staff")]
|
|
public async Task<ApiResponse<UserDto>> CreateUser([FromBody] CreateUserRequest request)
|
|
{
|
|
return await _userService.CreateUserAsync(request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新用户
|
|
/// </summary>
|
|
[HttpPost("{id:long}")]
|
|
[Authorize(Policy = "Staff")]
|
|
public async Task<ApiResponse<UserDto>> UpdateUser(long id, [FromBody] UpdateUserRequest request)
|
|
{
|
|
return await _userService.UpdateUserAsync(id, request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除用户(软删除)
|
|
/// </summary>
|
|
[HttpPost("{id:long}/delete")]
|
|
[Authorize(Policy = "Admin")]
|
|
public async Task<ApiResponse> DeleteUser(long id)
|
|
{
|
|
return await _userService.DeleteUserAsync(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置密码
|
|
/// </summary>
|
|
[HttpPost("{id:long}/reset-password")]
|
|
[Authorize(Policy = "Staff")]
|
|
public async Task<ApiResponse> ResetPassword(long id)
|
|
{
|
|
return await _userService.ResetPasswordAsync(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换用户状态
|
|
/// </summary>
|
|
[HttpPost("{id:long}/toggle-status")]
|
|
[Authorize(Policy = "Admin")]
|
|
public async Task<ApiResponse> ToggleStatus(long id)
|
|
{
|
|
return await _userService.ToggleStatusAsync(id);
|
|
}
|
|
}
|