56 lines
2.5 KiB
C#
56 lines
2.5 KiB
C#
using F9WebAiTest.Application.Common;
|
||
using F9WebAiTest.Application.Common.Exceptions;
|
||
using F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
||
using F9WebAiTest.Application.CQRS.SYS.Departments.Queries;
|
||
using MediatR;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
|
||
namespace F9WebAiTest.Api.Controllers;
|
||
|
||
/// <summary>系统部门管理(DDD + CQRS)</summary>
|
||
[ApiController]
|
||
[Route("api/sys/department")]
|
||
[Authorize]
|
||
public class SysDepartmentController : ControllerBase
|
||
{
|
||
private readonly IMediator _mediator;
|
||
public SysDepartmentController(IMediator mediator) => _mediator = mediator;
|
||
|
||
[HttpGet]
|
||
public async Task<IActionResult> GetAll()
|
||
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetDepartmentAllQuery())));
|
||
|
||
[HttpPost]
|
||
public async Task<IActionResult> Create([FromBody] Department_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] Department_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 Department_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] Department_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)); }
|
||
}
|
||
}
|