feat: general接口拦截器
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import traceback
|
||||
|
||||
from app.config.env import env
|
||||
from app.general.general_interceptors import invoke_interceptor
|
||||
from app.general.general_utils.build_delete_sql import build_delete_sql
|
||||
from app.general.general_utils.build_insert_sql import build_insert_sql
|
||||
from app.general.general_utils.build_query_sql import build_query_sql
|
||||
@@ -52,6 +53,8 @@ async def list(session: AsyncSessionDep, query_config, module_config, debug_data
|
||||
"orders": get_default_orders(query_config, module_config)
|
||||
}
|
||||
|
||||
await invoke_interceptor('before_list', module_config['base'], target_query_config, session)
|
||||
|
||||
sql, values = build_query_sql(target_query_config, module_config)
|
||||
|
||||
try:
|
||||
@@ -62,6 +65,8 @@ async def list(session: AsyncSessionDep, query_config, module_config, debug_data
|
||||
|
||||
create_convertor(module_config)['decode_list'](result)
|
||||
|
||||
await invoke_interceptor('after_list', module_config['base'], result, session)
|
||||
|
||||
if n_only_count:
|
||||
return {
|
||||
"total": result[0]['total']
|
||||
@@ -120,6 +125,8 @@ async def insert(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
row_id = (await get_id(session, 1))[0]
|
||||
row['id'] = row_id
|
||||
|
||||
await invoke_interceptor('before_insert', module_config['base'], row, session)
|
||||
|
||||
try:
|
||||
sql, values = build_insert_sql(module_config, row)
|
||||
|
||||
@@ -134,6 +141,8 @@ async def insert(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
|
||||
item_dict = get_value(result, 'result', None)
|
||||
|
||||
await invoke_interceptor('after_insert', module_config['base'], item_dict, session)
|
||||
|
||||
if item_dict is not None:
|
||||
return {
|
||||
"result": item_dict
|
||||
@@ -167,6 +176,8 @@ async def batch_insert(session: AsyncSessionDep, query_config, module_config, de
|
||||
for index, row in enumerate(rows_without_id):
|
||||
row['id'] = new_id_list[index]
|
||||
|
||||
await invoke_interceptor('before_batch_insert', module_config['base'], rows, session)
|
||||
|
||||
try:
|
||||
for row in rows:
|
||||
sql, values = build_insert_sql(module_config, row)
|
||||
@@ -182,6 +193,9 @@ async def batch_insert(session: AsyncSessionDep, query_config, module_config, de
|
||||
result = get_value(result, 'list', [])
|
||||
|
||||
if len(result) > 0:
|
||||
|
||||
await invoke_interceptor('after_batch_insert', module_config['base'], result, session)
|
||||
|
||||
return {
|
||||
"result": result
|
||||
}
|
||||
@@ -218,6 +232,8 @@ async def update(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
|
||||
create_convertor(module_config)['encode_list']([row])
|
||||
|
||||
await invoke_interceptor('before_update', module_config['base'], row, session)
|
||||
|
||||
try:
|
||||
sql, values = build_update_sql(module_config, row, row.keys() if update_by_fields else None)
|
||||
|
||||
@@ -232,6 +248,8 @@ async def update(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
|
||||
item_dict = get_value(result, 'result', None)
|
||||
|
||||
await invoke_interceptor('after_update', module_config['base'], item_dict, session)
|
||||
|
||||
if item_dict is not None:
|
||||
return {
|
||||
"result": item_dict
|
||||
@@ -268,6 +286,8 @@ async def batch_update(session: AsyncSessionDep, query_config, module_config, de
|
||||
"rows": rows_without_id,
|
||||
}
|
||||
|
||||
await invoke_interceptor('before_batch_update', module_config['base'], rows, session)
|
||||
|
||||
try:
|
||||
for row in rows:
|
||||
sql, values = build_update_sql(module_config, row, row.keys() if update_by_fields else None)
|
||||
@@ -283,6 +303,9 @@ async def batch_update(session: AsyncSessionDep, query_config, module_config, de
|
||||
result = get_value(result, 'list', [])
|
||||
|
||||
if len(result) > 0:
|
||||
|
||||
await invoke_interceptor('after_batch_update', module_config['base'], result, session)
|
||||
|
||||
return {
|
||||
"result": result
|
||||
}
|
||||
@@ -308,6 +331,8 @@ async def delete(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
"error": "id parameter is missing",
|
||||
}
|
||||
|
||||
await invoke_interceptor('before_delete', module_config['base'], id, session)
|
||||
|
||||
try:
|
||||
sql, values = build_delete_sql(module_config, id)
|
||||
debug_data.append({"sql": sql, "values": values})
|
||||
@@ -316,6 +341,9 @@ async def delete(session: AsyncSessionDep, query_config, module_config, debug_da
|
||||
deleted_rows = result.rowcount
|
||||
|
||||
if deleted_rows >= 1:
|
||||
|
||||
await invoke_interceptor('after_delete', module_config['base'], id, session)
|
||||
|
||||
return {"deletedRows": deleted_rows}
|
||||
else:
|
||||
return {"error": f"delete failed, delete rows is {deleted_rows}", }
|
||||
|
||||
Reference in New Issue
Block a user