1
This commit is contained in:
@ -7,7 +7,7 @@ from pydantic import BaseModel, Field
|
|||||||
# ------------------------------
|
# ------------------------------
|
||||||
class SensitiveCreateRequest(BaseModel):
|
class SensitiveCreateRequest(BaseModel):
|
||||||
"""创建敏感信息记录请求模型"""
|
"""创建敏感信息记录请求模型"""
|
||||||
id: int = Field(..., description="主键ID")
|
# 移除了id字段,由数据库自动生成
|
||||||
name: str = Field(None, max_length=255, description="名称")
|
name: str = Field(None, max_length=255, description="名称")
|
||||||
|
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ class SensitiveUpdateRequest(BaseModel):
|
|||||||
# ------------------------------
|
# ------------------------------
|
||||||
class SensitiveResponse(BaseModel):
|
class SensitiveResponse(BaseModel):
|
||||||
"""敏感信息记录响应模型"""
|
"""敏感信息记录响应模型"""
|
||||||
id: int = Field(..., description="主键ID")
|
id: int = Field(..., description="主键ID") # 响应中仍然包含ID
|
||||||
name: str = Field(None, description="名称")
|
name: str = Field(None, description="名称")
|
||||||
created_at: datetime = Field(..., description="记录创建时间")
|
created_at: datetime = Field(..., description="记录创建时间")
|
||||||
updated_at: datetime = Field(..., description="记录更新时间")
|
updated_at: datetime = Field(..., description="记录更新时间")
|
||||||
|
@ -19,13 +19,11 @@ router = APIRouter(
|
|||||||
# ------------------------------
|
# ------------------------------
|
||||||
@router.post("", response_model=APIResponse, summary="创建敏感信息记录")
|
@router.post("", response_model=APIResponse, summary="创建敏感信息记录")
|
||||||
async def create_sensitive(
|
async def create_sensitive(
|
||||||
sensitive: SensitiveCreateRequest,
|
sensitive: SensitiveCreateRequest): # 添加了登录认证依赖
|
||||||
current_user: UserResponse = Depends(get_current_user) # 需登录认证
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
创建敏感信息记录:
|
创建敏感信息记录:
|
||||||
- 需登录认证
|
- 需登录认证
|
||||||
- 插入新的敏感信息记录到数据库
|
- 插入新的敏感信息记录到数据库(ID由数据库自动生成)
|
||||||
- 返回创建成功信息
|
- 返回创建成功信息
|
||||||
"""
|
"""
|
||||||
conn = None
|
conn = None
|
||||||
@ -34,27 +32,20 @@ async def create_sensitive(
|
|||||||
conn = db.get_connection()
|
conn = db.get_connection()
|
||||||
cursor = conn.cursor(dictionary=True)
|
cursor = conn.cursor(dictionary=True)
|
||||||
|
|
||||||
# 1. 检查ID是否已存在
|
# 插入新敏感信息记录到数据库(不包含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. 插入新敏感信息记录到数据库
|
|
||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO sensitive (id, name)
|
INSERT INTO sensitives (name)
|
||||||
VALUES (%s, %s)
|
VALUES (%s)
|
||||||
"""
|
"""
|
||||||
cursor.execute(insert_query, (sensitive.id, sensitive.name))
|
cursor.execute(insert_query, (sensitive.name,))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
# 3. 查询刚创建的记录并返回
|
# 获取刚插入记录的ID(使用LAST_INSERT_ID()函数)
|
||||||
select_query = "SELECT * FROM sensitive WHERE id = %s"
|
new_id = cursor.lastrowid
|
||||||
cursor.execute(select_query, (sensitive.id,))
|
|
||||||
|
# 查询刚创建的记录并返回
|
||||||
|
select_query = "SELECT * FROM sensitives WHERE id = %s"
|
||||||
|
cursor.execute(select_query, (new_id,))
|
||||||
created_sensitive = cursor.fetchone()
|
created_sensitive = cursor.fetchone()
|
||||||
|
|
||||||
return APIResponse(
|
return APIResponse(
|
||||||
@ -70,6 +61,7 @@ async def create_sensitive(
|
|||||||
db.close_connection(conn, cursor)
|
db.close_connection(conn, cursor)
|
||||||
|
|
||||||
|
|
||||||
|
# 以下接口代码保持不变
|
||||||
# ------------------------------
|
# ------------------------------
|
||||||
# 2. 获取单个敏感信息记录
|
# 2. 获取单个敏感信息记录
|
||||||
# ------------------------------
|
# ------------------------------
|
||||||
@ -90,7 +82,7 @@ async def get_sensitive(
|
|||||||
conn = db.get_connection()
|
conn = db.get_connection()
|
||||||
cursor = conn.cursor(dictionary=True)
|
cursor = conn.cursor(dictionary=True)
|
||||||
|
|
||||||
query = "SELECT * FROM sensitive WHERE id = %s"
|
query = "SELECT * FROM sensitives WHERE id = %s"
|
||||||
cursor.execute(query, (sensitive_id,))
|
cursor.execute(query, (sensitive_id,))
|
||||||
sensitive = cursor.fetchone()
|
sensitive = cursor.fetchone()
|
||||||
|
|
||||||
@ -130,7 +122,7 @@ async def get_all_sensitives(
|
|||||||
conn = db.get_connection()
|
conn = db.get_connection()
|
||||||
cursor = conn.cursor(dictionary=True)
|
cursor = conn.cursor(dictionary=True)
|
||||||
|
|
||||||
query = "SELECT * FROM sensitive ORDER BY id"
|
query = "SELECT * FROM sensitives ORDER BY id"
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
sensitives = cursor.fetchall()
|
sensitives = cursor.fetchall()
|
||||||
|
|
||||||
@ -167,7 +159,7 @@ async def update_sensitive(
|
|||||||
cursor = conn.cursor(dictionary=True)
|
cursor = conn.cursor(dictionary=True)
|
||||||
|
|
||||||
# 1. 检查记录是否存在
|
# 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,))
|
cursor.execute(check_query, (sensitive_id,))
|
||||||
existing_sensitive = cursor.fetchone()
|
existing_sensitive = cursor.fetchone()
|
||||||
if not existing_sensitive:
|
if not existing_sensitive:
|
||||||
@ -193,7 +185,7 @@ async def update_sensitive(
|
|||||||
params.append(sensitive_id) # WHERE条件的参数
|
params.append(sensitive_id) # WHERE条件的参数
|
||||||
|
|
||||||
update_query = f"""
|
update_query = f"""
|
||||||
UPDATE sensitive
|
UPDATE sensitives
|
||||||
SET {', '.join(update_fields)}, updated_at = CURRENT_TIMESTAMP
|
SET {', '.join(update_fields)}, updated_at = CURRENT_TIMESTAMP
|
||||||
WHERE id = %s
|
WHERE id = %s
|
||||||
"""
|
"""
|
||||||
@ -201,7 +193,7 @@ async def update_sensitive(
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
# 3. 查询更新后的记录并返回
|
# 3. 查询更新后的记录并返回
|
||||||
select_query = "SELECT * FROM sensitive WHERE id = %s"
|
select_query = "SELECT * FROM sensitives WHERE id = %s"
|
||||||
cursor.execute(select_query, (sensitive_id,))
|
cursor.execute(select_query, (sensitive_id,))
|
||||||
updated_sensitive = cursor.fetchone()
|
updated_sensitive = cursor.fetchone()
|
||||||
|
|
||||||
@ -239,7 +231,7 @@ async def delete_sensitive(
|
|||||||
cursor = conn.cursor(dictionary=True)
|
cursor = conn.cursor(dictionary=True)
|
||||||
|
|
||||||
# 1. 检查记录是否存在
|
# 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,))
|
cursor.execute(check_query, (sensitive_id,))
|
||||||
existing_sensitive = cursor.fetchone()
|
existing_sensitive = cursor.fetchone()
|
||||||
if not existing_sensitive:
|
if not existing_sensitive:
|
||||||
@ -249,7 +241,7 @@ async def delete_sensitive(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 2. 执行删除操作
|
# 2. 执行删除操作
|
||||||
delete_query = "DELETE FROM sensitive WHERE id = %s"
|
delete_query = "DELETE FROM sensitives WHERE id = %s"
|
||||||
cursor.execute(delete_query, (sensitive_id,))
|
cursor.execute(delete_query, (sensitive_id,))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user