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; /// 注册表(参数配置)管理:按"命名空间 + 键名"维护配置值,供各业务模块读取 [ApiController] [Route("api/sys/registry")] [Authorize] public class SysRegistryController : ControllerBase { private readonly IMediator _mediator; public SysRegistryController(IMediator mediator) => _mediator = mediator; /// 全部配置项(含未启用),用于列表页构建命名空间树 [HttpGet] public async Task GetAll() => Ok(ApiResult.Ok(await _mediator.Send(new GetRegistryAllQuery()))); /// 按命名空间 + 键名查询(业务模块取值入口,未配置时返回 data=null) [HttpGet("by-key")] public async Task 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.Ok(null, $"未找到命名空间[{category}]下键[{keyName}]的配置") : ApiResult.Ok(result)); } [HttpGet("{id}")] public async Task GetById(long id) { try { return Ok(ApiResult.Ok(await _mediator.Send(new GetRegistryByIdForEditQuery(id)))); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } /// 新增配置项(已存在同"命名空间 + 键名"时自动更新) [HttpPost] public async Task Create([FromBody] Registry_CreateCommand command) { try { return Ok(ApiResult.Ok(new { id = await _mediator.Send(command) }, "保存成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } [HttpPut("{id}")] public async Task Update(long id, [FromBody] Registry_UpdateCommand command) { try { return Ok(ApiResult.Ok(await _mediator.Send(command with { Id = id }), "修改成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } /// 只修改值 [HttpPut("{id}/value")] public async Task SetValue(long id, [FromBody] Registry_SetValueCommand command) { try { return Ok(ApiResult.Ok(await _mediator.Send(command with { Id = id }), "修改成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } [HttpDelete("{id}")] public async Task Delete(long id) { try { return Ok(ApiResult.Ok(await _mediator.Send(new Registry_DelCommand(id)), "删除成功")); } catch (F9NotFoundException ex) { return NotFound(ApiResult.Fail(ex.Message, 404)); } catch (F9OperationException ex) { return BadRequest(ApiResult.Fail(ex.Message, 400)); } } }