feat: general_route
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
ModuleConfigModule = {
|
||||
"tableName": "pl_module",
|
||||
"base": "/module",
|
||||
"columns": {
|
||||
"id": {
|
||||
"valueType": "string",
|
||||
"front": {
|
||||
"title": "编号"
|
||||
}
|
||||
},
|
||||
"createdAt": {
|
||||
"valueType": "datetime",
|
||||
"front": {
|
||||
"title": "创建时间"
|
||||
}
|
||||
},
|
||||
"createdBy": {
|
||||
"valueType": "string",
|
||||
"front": {
|
||||
"title": "创建人"
|
||||
}
|
||||
},
|
||||
"updatedAt": {
|
||||
"valueType": "datetime",
|
||||
"front": {
|
||||
"title": "更新时间"
|
||||
}
|
||||
},
|
||||
"updatedBy": {
|
||||
"valueType": "string",
|
||||
"front": {
|
||||
"title": "更新人"
|
||||
}
|
||||
},
|
||||
"label": {
|
||||
"valueType": "string",
|
||||
"front": {
|
||||
"title": "模块名称"
|
||||
}
|
||||
},
|
||||
"code": {
|
||||
"valueType": "string",
|
||||
"front": {
|
||||
"title": "模块标识"
|
||||
}
|
||||
},
|
||||
"remarks": {
|
||||
"valueType": "string",
|
||||
"front": {
|
||||
"title": "备注"
|
||||
}
|
||||
},
|
||||
"moduleConfig": {
|
||||
"valueType": "string",
|
||||
"front": {
|
||||
"title": "模块配置信息"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import json
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import FastAPI
|
||||
from starlette.responses import JSONResponse
|
||||
|
||||
from app.general.ModuleConfigModule import ModuleConfigModule
|
||||
from app.general.general_utils.sql_service import sql_service
|
||||
from app.utils.db_utils import AsyncSessionDep
|
||||
|
||||
|
||||
def add_general_route(app: FastAPI):
|
||||
types = ['list', 'item', 'insert', 'update', 'delete', 'batch_insert', 'batch_update']
|
||||
for type in types:
|
||||
@app.post("/general/{module}/" + type)
|
||||
async def func(module: str, query_param: dict, session: AsyncSessionDep, type=type):
|
||||
try:
|
||||
result = await perform_general_operation(session=session, module=module, data=query_param, debug_data=[], type=type)
|
||||
if "error" in result:
|
||||
return JSONResponse(content=result, status_code=500)
|
||||
else:
|
||||
return result
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
|
||||
async def perform_general_operation(
|
||||
session: AsyncSessionDep,
|
||||
module: str,
|
||||
data: dict,
|
||||
debug_data=None,
|
||||
type: Literal['list', 'item', 'insert', 'update', 'delete', 'batch_insert', 'batch_update'] = 'query'
|
||||
):
|
||||
if debug_data is None:
|
||||
debug_data = []
|
||||
|
||||
module_query_result = await get_module_config(session, module, debug_data)
|
||||
|
||||
if 'error' in module_query_result:
|
||||
return module_query_result
|
||||
|
||||
module_config = module_query_result.get('result', {"": ""}).get('moduleConfig', None)
|
||||
|
||||
if module_config is not None:
|
||||
module_config = json.loads(module_config)
|
||||
|
||||
if module_config is None:
|
||||
return {
|
||||
"error": f"Can't find module config match: {module}",
|
||||
"body": module_query_result,
|
||||
}
|
||||
|
||||
if type == 'list':
|
||||
return await sql_service.list(session, data, module_config, debug_data)
|
||||
elif type == 'item':
|
||||
return await sql_service.item(session, data, module_config, debug_data)
|
||||
elif type == 'insert':
|
||||
return await sql_service.insert(session, data, module_config, debug_data)
|
||||
elif type == 'update':
|
||||
return await sql_service.update(session, data, module_config, debug_data)
|
||||
elif type == 'batch_insert':
|
||||
return await sql_service.batch_insert(session, data, module_config, debug_data)
|
||||
elif type == 'batch_update':
|
||||
return await sql_service.batch_update(session, data, module_config, debug_data)
|
||||
elif type == 'delete':
|
||||
return await sql_service.delete(session, data, module_config, debug_data)
|
||||
else:
|
||||
return {"error": f"Can't handle operation type: {type}"}
|
||||
|
||||
|
||||
async def get_module_config(session: AsyncSessionDep, module: str, debug_data=None):
|
||||
return await sql_service.item(session, {"code": module}, ModuleConfigModule, debug_data)
|
||||
@@ -25,6 +25,7 @@ from app.controller.test_sqlmodel import add_test_sqlmodel_route
|
||||
from app.controller.test_sync import add_test_sync_route
|
||||
from app.controller.translate_controller import add_translate_route
|
||||
from app.create_app import create_app
|
||||
from app.general.add_general_route import add_general_route
|
||||
from app.model.ApiSecretModel import ApiSecretService
|
||||
from app.model.ApproveModel import ApproveService
|
||||
from app.model.ConversationModel import ConversationService
|
||||
@@ -74,6 +75,7 @@ add_file_route(app)
|
||||
add_knowledge_route(app)
|
||||
add_api_route(app)
|
||||
add_redis_route(app)
|
||||
add_general_route(app)
|
||||
|
||||
|
||||
@app.get("/get_env")
|
||||
|
||||
Reference in New Issue
Block a user