完善基础数据
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using F9WebAiTest.Application.Common;
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.CQRS.SYS.Registries.Commands;
|
||||
using F9WebAiTest.Application.CQRS.SYS.Registries.Queries;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace F9WebAiTest.Api.Controllers;
|
||||
|
||||
/// <summary>注册表(参数配置)管理:按"命名空间 + 键名"维护配置值,供各业务模块读取</summary>
|
||||
[ApiController]
|
||||
[Route("api/sys/registry")]
|
||||
[Authorize]
|
||||
public class SysRegistryController : ControllerBase
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
public SysRegistryController(IMediator mediator) => _mediator = mediator;
|
||||
|
||||
/// <summary>全部配置项(含未启用),用于列表页构建命名空间树</summary>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll()
|
||||
=> Ok(ApiResult<object>.Ok(await _mediator.Send(new GetRegistryAllQuery())));
|
||||
|
||||
/// <summary>按命名空间 + 键名查询(业务模块取值入口,未配置时返回 data=null)</summary>
|
||||
[HttpGet("by-key")]
|
||||
public async Task<IActionResult> GetByCategoryAndKey([FromQuery] string category, [FromQuery] string keyName)
|
||||
{
|
||||
var result = await _mediator.Send(new GetByCategoryAndKeyQuery(category ?? string.Empty, keyName ?? string.Empty));
|
||||
return Ok(result == null
|
||||
? ApiResult<object>.Ok(null, $"未找到命名空间[{category}]下键[{keyName}]的配置")
|
||||
: ApiResult<object>.Ok(result));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetById(long id)
|
||||
{
|
||||
try { return Ok(ApiResult<object>.Ok(await _mediator.Send(new GetRegistryByIdForEditQuery(id)))); }
|
||||
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
|
||||
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
|
||||
}
|
||||
|
||||
/// <summary>新增配置项(已存在同"命名空间 + 键名"时自动更新)</summary>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] Registry_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] Registry_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)); }
|
||||
}
|
||||
|
||||
/// <summary>只修改值</summary>
|
||||
[HttpPut("{id}/value")]
|
||||
public async Task<IActionResult> SetValue(long id, [FromBody] Registry_SetValueCommand 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 Registry_DelCommand(id)), "删除成功")); }
|
||||
catch (F9NotFoundException ex) { return NotFound(ApiResult<object>.Fail(ex.Message, 404)); }
|
||||
catch (F9OperationException ex) { return BadRequest(ApiResult<object>.Fail(ex.Message, 400)); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user