OpenAuth.Net/OpenAuth.WebApi/Controllers/DynamicApiController.cs

176 lines
4.8 KiB
C#
Raw Normal View History

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