Files
2026-10-08 10:43:59 +08:00

88 lines
4.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using F9WebAiTest.Application.Common;
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
using F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Queries;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace F9WebAiTest.Api.Controllers;
/// <summary>系统用户管理(DDD + CQRS)</summary>
[ApiController]
[Route("api/sys/user")]
[Authorize]
public class SysUserController : ControllerBase
{
private readonly IMediator _mediator;
public SysUserController(IMediator mediator) => _mediator = mediator;
[HttpGet]
public async Task<IActionResult> GetPaged([FromQuery] long departmentId = 0, [FromQuery] string like = "", [FromQuery] int page = 1, [FromQuery] int pageSize = 10)
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetUserPagedQuery(departmentId, like, page, pageSize))));
[HttpGet("{id}")]
public async Task<IActionResult> GetById(long id)
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetUserByIdQuery(id))));
[HttpGet("{id}/roles")]
public async Task<IActionResult> GetUserRoles(long id)
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetUserRolesByUserIdQuery(id))));
[HttpPost]
public async Task<IActionResult> Create([FromBody] User_CreateCommand command)
{
try { return Ok(ApiResult<object>.Ok(new { id = await _mediator.Send(command) }, "创建成功")); }
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
}
[HttpPut("{id}")]
public async Task<IActionResult> Update(long id, [FromBody] User_UpdateCommand command)
{
try { return Ok(ApiResult<object>.Ok(await _mediator.Send(command with { Id = id }), "修改成功")); }
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(long id)
{
try { return Ok(ApiResult<object>.Ok(await _mediator.Send(new User_DelCommand(id)), "删除成功")); }
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
}
[HttpPut("{id}/active")]
public async Task<IActionResult> ActiveOrDeactivate(long id, [FromBody] User_ActiveOrDeactivateCommand command)
{
try { return Ok(ApiResult<object>.Ok(await _mediator.Send(command with { Id = id }), "操作成功")); }
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
}
[HttpPut("{id}/reset-password")]
public async Task<IActionResult> ResetPassword(long id)
{
try { return Ok(ApiResult<object>.Ok(await _mediator.Send(new User_ResetPasswordCommand(id)), "密码已重置")); }
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
}
[HttpPost("{id}/roles")]
public async Task<IActionResult> AssignRole(long id, [FromBody] UserRole_AssignCommand command)
{
try { return Ok(ApiResult<object>.Ok(await _mediator.Send(command with { UserId = id }), "绑定成功")); }
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
}
[HttpDelete("{id}/roles/{roleId}")]
public async Task<IActionResult> RemoveRole(long id, long roleId)
{
try { return Ok(ApiResult<object>.Ok(await _mediator.Send(new UserRole_RemoveCommand(id, roleId)), "解绑成功")); }
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
}
}