using System; using System.Threading.Tasks; using Infrastructure; using Microsoft.AspNetCore.Mvc; using OpenAuth.App; using OpenAuth.App.Response; using OpenAuth.App.Request; using Microsoft.AspNetCore.Authorization; namespace OpenAuth.WebApi.Controllers { /// /// 动态API控制器 /// 用于处理任意表的CRUD操作及直接调用OpenAuth.App的各种应用 /// [Route("api/dynamic/[action]")] [ApiController] [ApiExplorerSettings(GroupName = "动态API_DynamicApi")] public class DynamicApiController : ControllerBase { private readonly DynamicApiApp _app; public DynamicApiController(DynamicApiApp app) { _app = app; } /// /// 获取表数据列表 /// /// 查询参数 /// [HttpPost] [AllowAnonymous] public async Task GetList([FromBody] QueryDynamicListReq req) { TableData result = new TableData(); try { // 获取实体类型 result = await _app.GetList(req); } catch (Exception ex) { result.code = 500; result.msg = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 获取表数据详情 /// /// 查询参数 /// [HttpPost] [AllowAnonymous] public async Task> Get([FromBody] QueryDynamicEntityReq req) { var result = new Response(); try { // 获取实体类型 result = await _app.Get(req); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 添加表数据 /// /// 添加参数 /// [HttpPost] [AllowAnonymous] public async Task Add([FromBody] AddOrUpdateDynamicEntityReq req) { var result = new Response(); try { // 获取实体类型 result = await _app.Add(req); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 更新实体 /// /// 实体名称,例如:ExternalDataSource /// 实体对象 /// [HttpPost] [AllowAnonymous] public async Task Update([FromBody] AddOrUpdateDynamicEntityReq req) { var result = new Response(); try { // 获取实体类型 result = await _app.Update(req); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 批量删除实体 /// /// 实体名称,例如:ExternalDataSource /// 实体ID数组 /// [HttpPost] [AllowAnonymous] public async Task Delete([FromBody] DelDynamicReq req) { var result = new Response(); try { result = await _app.Delete(req); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 直接调用OpenAuth.App的各种应用 /// /// 调用参数 /// [HttpPost] [AllowAnonymous] public TableData Invoke([FromBody] InvokeDynamicReq req) { var result = new TableData(); try { result = _app.Invoke(req); } catch (Exception ex) { result.code = 500; result.msg = ex.InnerException?.Message ?? ex.Message; } return result; } } }