优化代码风格

This commit is contained in:
ZZX9599
2025-09-08 17:34:23 +08:00
parent 9b3d20511a
commit 8ceb92c572
20 changed files with 223 additions and 192 deletions

View File

@ -21,7 +21,7 @@ router = APIRouter(
async def create_sensitive(
sensitive: SensitiveCreateRequest): # 添加了登录认证依赖
"""
创建敏感信息记录
创建敏感信息记录:
- 需登录认证
- 插入新的敏感信息记录到数据库ID由数据库自动生成
- 返回创建成功信息
@ -32,7 +32,7 @@ async def create_sensitive(
conn = db.get_connection()
cursor = conn.cursor(dictionary=True)
# 插入新敏感信息记录到数据库不包含ID由数据库自动生成)
# 插入新敏感信息记录到数据库不包含ID由数据库自动生成)
insert_query = """
INSERT INTO sensitives (name)
VALUES (%s)
@ -56,7 +56,7 @@ async def create_sensitive(
except MySQLError as e:
if conn:
conn.rollback()
raise Exception(f"创建敏感信息记录失败{str(e)}") from e
raise Exception(f"创建敏感信息记录失败: {str(e)}") from e
finally:
db.close_connection(conn, cursor)
@ -71,7 +71,7 @@ async def get_sensitive(
current_user: UserResponse = Depends(get_current_user) # 需登录认证
):
"""
获取单个敏感信息记录
获取单个敏感信息记录:
- 需登录认证
- 根据ID查询敏感信息记录
- 返回查询到的敏感信息
@ -98,7 +98,7 @@ async def get_sensitive(
data=SensitiveResponse(**sensitive)
)
except MySQLError as e:
raise Exception(f"查询敏感信息记录失败{str(e)}") from e
raise Exception(f"查询敏感信息记录失败: {str(e)}") from e
finally:
db.close_connection(conn, cursor)
@ -109,7 +109,7 @@ async def get_sensitive(
@router.get("", response_model=APIResponse, summary="获取所有敏感信息记录")
async def get_all_sensitives():
"""
获取所有敏感信息记录
获取所有敏感信息记录:
- 需登录认证
- 查询所有敏感信息记录(不需要分页)
- 返回所有敏感信息列表
@ -130,7 +130,7 @@ async def get_all_sensitives():
data=[SensitiveResponse(**sensitive) for sensitive in sensitives]
)
except MySQLError as e:
raise Exception(f"查询所有敏感信息记录失败{str(e)}") from e
raise Exception(f"查询所有敏感信息记录失败: {str(e)}") from e
finally:
db.close_connection(conn, cursor)
@ -145,7 +145,7 @@ async def update_sensitive(
current_user: UserResponse = Depends(get_current_user) # 需登录认证
):
"""
更新敏感信息记录
更新敏感信息记录:
- 需登录认证
- 根据ID更新敏感信息记录
- 返回更新后的敏感信息
@ -203,7 +203,7 @@ async def update_sensitive(
except MySQLError as e:
if conn:
conn.rollback()
raise Exception(f"更新敏感信息记录失败{str(e)}") from e
raise Exception(f"更新敏感信息记录失败: {str(e)}") from e
finally:
db.close_connection(conn, cursor)
@ -217,7 +217,7 @@ async def delete_sensitive(
current_user: UserResponse = Depends(get_current_user) # 需登录认证
):
"""
删除敏感信息记录
删除敏感信息记录:
- 需登录认证
- 根据ID删除敏感信息记录
- 返回删除成功信息
@ -251,14 +251,14 @@ async def delete_sensitive(
except MySQLError as e:
if conn:
conn.rollback()
raise Exception(f"删除敏感信息记录失败{str(e)}") from e
raise Exception(f"删除敏感信息记录失败: {str(e)}") from e
finally:
db.close_connection(conn, cursor)
def get_all_sensitive_words() -> list[str]:
"""
获取所有敏感词返回字符串数组
获取所有敏感词返回字符串数组
返回:
list[str]: 包含所有敏感词的数组
@ -273,7 +273,7 @@ def get_all_sensitive_words() -> list[str]:
conn = db.get_connection()
cursor = conn.cursor(dictionary=True)
# 执行查询只获取敏感词字段
# 执行查询只获取敏感词字段
query = "SELECT name FROM sensitives ORDER BY id"
cursor.execute(query)
sensitive_records = cursor.fetchall()
@ -283,7 +283,7 @@ def get_all_sensitive_words() -> list[str]:
except MySQLError as e:
# 数据库错误处理
raise MySQLError(f"查询敏感词失败{str(e)}") from e
raise MySQLError(f"查询敏感词失败: {str(e)}") from e
finally:
# 确保资源正确释放
db.close_connection(conn, cursor)