68 lines
2.9 KiB
C#
68 lines
2.9 KiB
C#
using F9WebAiTest.Application.Common;
|
||
using F9WebAiTest.Application.Common.Exceptions;
|
||
using F9WebAiTest.Application.CQRS.SYS.WuziMulus.Commands;
|
||
using F9WebAiTest.Application.CQRS.SYS.WuziMulus.Queries;
|
||
using MediatR;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
|
||
namespace F9WebAiTest.Api.Controllers;
|
||
|
||
/// <summary>物资分组管理(物资目录的分类树)</summary>
|
||
[ApiController]
|
||
[Route("api/sys/wuzigroup")]
|
||
[Authorize]
|
||
public class SysWuziGroupController : ControllerBase
|
||
{
|
||
private readonly IMediator _mediator;
|
||
public SysWuziGroupController(IMediator mediator) => _mediator = mediator;
|
||
|
||
/// <summary>全部分组(构建分组树)</summary>
|
||
[HttpGet]
|
||
public async Task<IActionResult> GetAll()
|
||
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetWuziGroupAllQuery())));
|
||
|
||
/// <summary>生成新编号(一级分类传 parentId=0)</summary>
|
||
[HttpGet("new-code")]
|
||
public async Task<IActionResult> GetNewCode([FromQuery] long parentId = 0)
|
||
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetWuziGroupNewCodeQuery(parentId))));
|
||
|
||
/// <summary>是否存在下级分组(删除前校验)</summary>
|
||
[HttpGet("{id}/exist-child")]
|
||
public async Task<IActionResult> ExistChild(long id)
|
||
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetWuziGroupExistChildQuery(id))));
|
||
|
||
[HttpGet("{id}")]
|
||
public async Task<IActionResult> GetById(long id)
|
||
{
|
||
var result = await _mediator.Send(new GetWuziGroupByIdQuery(id));
|
||
return result == null
|
||
? NotFound(ApiResult<object>.Fail("物资分组不存在", 404))
|
||
: Ok(ApiResult<object>.Ok(result));
|
||
}
|
||
|
||
[HttpPost]
|
||
public async Task<IActionResult> Create([FromBody] WuziGroup_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] WuziGroup_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 WuziGroup_DelCommand(id)), "删除成功")); }
|
||
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
|
||
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
|
||
}
|
||
}
|