Files
stu/api/student_info_api.py

101 lines
4.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 导入 FastAPI 的 APIRouter(用于组织路由)、Depends(依赖注入)、HTTPException(抛出 HTTP 异常)
from fastapi import APIRouter, Depends, HTTPException
# 从 dao 层导入数据库操作函数,并对修改和删除函数起了别名,避免与路由函数重名
from dao.student_info_sql import (
get_part_student_info,
add_student_info,
update_student_info as update_info,
delete_student_info as delete_info,
)
# 导入数据库会话依赖,用于每个请求获取独立的 Session
from database import get_db
# 导入 Pydantic 模型:请求体、响应体、通用操作响应体
from schema.stu_info_region_schema import stu_info_req, stu_info_resp, stu_info_add_resp
# 创建学生信息模块的路由实例
student_info_api = APIRouter()
# -------------------- 查询学生信息(支持条件过滤) --------------------
@student_info_api.get('/student', response_model=list[stu_info_resp], summary='查询')
def get_student_info_part(
student_id: str = None,
student_name: str = None,
gender: str = None,
gs=Depends(get_db)
):
"""
根据学号、姓名、性别查询学生信息。
- 所有查询参数均为可选,不传则返回全部未删除记录。
- 返回列表,每个元素符合 stu_info_resp 模型。
"""
try:
# 调用 dao 层查询函数,传入数据库会话和过滤条件
data = get_part_student_info(gs, student_id, student_name, gender)
return data
except Exception as err:
# 捕获异常,返回 500 错误,详情为原始异常信息
raise HTTPException(status_code=500, detail=err)
# -------------------- 添加学生信息 --------------------
@student_info_api.post('/student', response_model=stu_info_add_resp, summary='添加')
def add_student_infomation(stu_info: stu_info_req, gs=Depends(get_db)):
"""
新增一条学生记录。
- 请求体必须符合 stu_info_req 模型。
- 返回统一的 stu_info_add_resp(code + message)。
"""
try:
# 将 Pydantic 模型转为字典,便于传给 dao 层
data = stu_info.model_dump()
# 调用 dao 层添加函数
res = add_student_info(gs, data)
if not res:
# 如果 dao 返回假值(如 None、空字符串),说明添加失败
raise HTTPException(status_code=500, detail='添加失败')
return stu_info_add_resp(code=200, message='添加成功')
except Exception as e:
# 将异常信息转为字符串,避免序列化问题
raise HTTPException(status_code=500, detail=str(e))
# -------------------- 修改学生信息 --------------------
@student_info_api.put('/student', response_model=stu_info_add_resp, summary='修改')
def update_student_info(stu_info: stu_info_req, gs=Depends(get_db)):
"""
根据学号更新学生信息。
- 请求体必须包含完整的 stu_info_req(包括 student_id)。
- 返回统一的 stu_info_add_resp。
"""
try:
data = stu_info.model_dump()
# 调用 dao 层更新函数(已别名为 update_info)
res = update_info(gs, data)
if not res:
# 没有更新任何行,说明学号不存在或数据未变化
raise HTTPException(status_code=500, detail='没有更新!')
return stu_info_add_resp(code=200, message='更新成功')
except Exception as err:
raise HTTPException(status_code=500, detail=str(err))
# -------------------- 删除学生信息(逻辑删除) --------------------
@student_info_api.delete('/student', response_model=stu_info_add_resp, summary='删除')
def delete_student_info(sid: str, gs=Depends(get_db)):
"""
根据学号删除学生记录(通常为逻辑删除)。
- sid 为查询参数,返回统一的 stu_info_add_resp。
"""
try:
# 调用 dao 层删除函数(已别名为 delete_info),传入学号
res = delete_info(gs, sid)
if not res:
# 没有删除任何行,可能学号不存在
raise HTTPException(status_code=500, detail='没有删除')
return stu_info_add_resp(code=200, message='删除成功')
except Exception as err:
raise HTTPException(status_code=500, detail=str(err))