This commit is contained in:
2025-09-03 22:58:16 +08:00
parent 14c70e3c76
commit 9a3e5be99f
2 changed files with 22 additions and 30 deletions

View File

@ -19,13 +19,11 @@ router = APIRouter(
# ------------------------------
@router.post("", response_model=APIResponse, summary="创建敏感信息记录")
async def create_sensitive(
sensitive: SensitiveCreateRequest,
current_user: UserResponse = Depends(get_current_user) # 需登录认证
):
sensitive: SensitiveCreateRequest): # 添加了登录认证依赖
"""
创建敏感信息记录:
- 需登录认证
- 插入新的敏感信息记录到数据库
- 插入新的敏感信息记录到数据库ID由数据库自动生成
- 返回创建成功信息
"""
conn = None
@ -34,27 +32,20 @@ async def create_sensitive(
conn = db.get_connection()
cursor = conn.cursor(dictionary=True)
# 1. 检查ID是否已存在
check_query = "SELECT id FROM sensitive WHERE id = %s"
cursor.execute(check_query, (sensitive.id,))
existing_sensitive = cursor.fetchone()
if existing_sensitive:
raise HTTPException(
status_code=400,
detail=f"ID为 {sensitive.id} 的敏感信息记录已存在"
)
# 2. 插入新敏感信息记录到数据库
# 插入新敏感信息记录到数据库不包含ID由数据库自动生成
insert_query = """
INSERT INTO sensitive (id, name)
VALUES (%s, %s)
INSERT INTO sensitives (name)
VALUES (%s)
"""
cursor.execute(insert_query, (sensitive.id, sensitive.name))
cursor.execute(insert_query, (sensitive.name,))
conn.commit()
# 3. 查询刚创建的记录并返回
select_query = "SELECT * FROM sensitive WHERE id = %s"
cursor.execute(select_query, (sensitive.id,))
# 获取刚插入记录的ID使用LAST_INSERT_ID()函数)
new_id = cursor.lastrowid
# 查询刚创建的记录并返回
select_query = "SELECT * FROM sensitives WHERE id = %s"
cursor.execute(select_query, (new_id,))
created_sensitive = cursor.fetchone()
return APIResponse(
@ -70,6 +61,7 @@ async def create_sensitive(
db.close_connection(conn, cursor)
# 以下接口代码保持不变
# ------------------------------
# 2. 获取单个敏感信息记录
# ------------------------------
@ -90,7 +82,7 @@ async def get_sensitive(
conn = db.get_connection()
cursor = conn.cursor(dictionary=True)
query = "SELECT * FROM sensitive WHERE id = %s"
query = "SELECT * FROM sensitives WHERE id = %s"
cursor.execute(query, (sensitive_id,))
sensitive = cursor.fetchone()
@ -130,7 +122,7 @@ async def get_all_sensitives(
conn = db.get_connection()
cursor = conn.cursor(dictionary=True)
query = "SELECT * FROM sensitive ORDER BY id"
query = "SELECT * FROM sensitives ORDER BY id"
cursor.execute(query)
sensitives = cursor.fetchall()
@ -167,7 +159,7 @@ async def update_sensitive(
cursor = conn.cursor(dictionary=True)
# 1. 检查记录是否存在
check_query = "SELECT id FROM sensitive WHERE id = %s"
check_query = "SELECT id FROM sensitives WHERE id = %s"
cursor.execute(check_query, (sensitive_id,))
existing_sensitive = cursor.fetchone()
if not existing_sensitive:
@ -193,7 +185,7 @@ async def update_sensitive(
params.append(sensitive_id) # WHERE条件的参数
update_query = f"""
UPDATE sensitive
UPDATE sensitives
SET {', '.join(update_fields)}, updated_at = CURRENT_TIMESTAMP
WHERE id = %s
"""
@ -201,7 +193,7 @@ async def update_sensitive(
conn.commit()
# 3. 查询更新后的记录并返回
select_query = "SELECT * FROM sensitive WHERE id = %s"
select_query = "SELECT * FROM sensitives WHERE id = %s"
cursor.execute(select_query, (sensitive_id,))
updated_sensitive = cursor.fetchone()
@ -239,7 +231,7 @@ async def delete_sensitive(
cursor = conn.cursor(dictionary=True)
# 1. 检查记录是否存在
check_query = "SELECT id FROM sensitive WHERE id = %s"
check_query = "SELECT id FROM sensitives WHERE id = %s"
cursor.execute(check_query, (sensitive_id,))
existing_sensitive = cursor.fetchone()
if not existing_sensitive:
@ -249,7 +241,7 @@ async def delete_sensitive(
)
# 2. 执行删除操作
delete_query = "DELETE FROM sensitive WHERE id = %s"
delete_query = "DELETE FROM sensitives WHERE id = %s"
cursor.execute(delete_query, (sensitive_id,))
conn.commit()