Files
SST/PythonProject/api/student_api.py
T

67 lines
2.8 KiB
Python
Raw Normal View History

2026-09-22 14:36:43 +08:00
from fastapi import APIRouter,HTTPException,Depends
2026-09-21 18:04:38 +08:00
from dao.student_dao import *
from schema.student_schema import *
2026-09-21 11:43:34 +08:00
from util.database import get_db
2026-09-22 12:16:28 +08:00
StudentAPI = APIRouter(tags=['学生基本信息管理模块'])
2026-09-21 11:43:34 +08:00
2026-09-22 12:00:00 +08:00
@StudentAPI.get('/students',response_model=StudentPageResponse,summary='学生信息查询接口',description='查询学生信息')
2026-09-21 11:43:34 +08:00
def get_students(stu_id:int|None=None
2026-09-21 15:10:45 +08:00
,stu_name:str|None=None
,class_id:int|None=None
,db=Depends(get_db)
,page:int=1
,page_size:int=10):
2026-09-21 18:04:38 +08:00
r, total = get_student_dao(stu_id=stu_id
,stu_name=stu_name
,class_id=class_id
,page=page
,page_size=page_size
,db=db)
if not r:
raise HTTPException(status_code=404,detail='学生不存在')
2026-09-21 22:25:16 +08:00
return StudentPageResponse(page=page,
page_size=page_size,
2026-09-22 09:56:31 +08:00
totals=total,
2026-09-21 22:25:16 +08:00
data=r)
2026-09-21 11:43:34 +08:00
2026-09-22 12:00:00 +08:00
@StudentAPI.put('/{stu_id}',summary='学生信息更新接口',description='更新学生信息')
2026-09-21 15:10:45 +08:00
def update_students(s:StudentRequest
,stu_id:int
,db=Depends(get_db)):
2026-09-21 11:43:34 +08:00
d = s.model_dump(exclude_unset=True)
2026-09-22 14:58:51 +08:00
d.pop('stu_id', None)
2026-09-21 18:04:38 +08:00
if not d:
2026-09-22 14:34:22 +08:00
raise HTTPException(status_code=400, detail='更新内容不能为空')
2026-09-21 11:43:34 +08:00
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
2026-09-22 14:58:51 +08:00
if r == 'conflict':
raise HTTPException(status_code=409, detail = '身份证号已被其他学生占用')
if r == 'error':
raise HTTPException(status_code=500, detail='更新失败,请稍后重试')
2026-09-21 11:43:34 +08:00
if not r:
2026-09-22 14:58:51 +08:00
raise HTTPException(status_code=404, detail='没有更新')
2026-09-21 11:43:34 +08:00
return {'code':200,'totals':r,'detail':'更新成功'}
2026-09-22 12:00:00 +08:00
@StudentAPI.delete('/{stu_id}',summary='学生信息删除接口',description='删除学生信息')
2026-09-21 15:10:45 +08:00
def del_students(stu_id:int
,db=Depends(get_db)):
2026-09-21 11:43:34 +08:00
rows=delete_student_dao( stu_id=stu_id,db=db )
if not rows:
2026-09-22 10:39:41 +08:00
raise HTTPException(status_code=404,detail='对象已被删除')
2026-09-21 11:43:34 +08:00
return {'code':200,'totals':rows,'detail':'删除成功'}
2026-09-22 12:00:00 +08:00
@StudentAPI.post('/students',response_model=StugetResponse,summary='学生信息新增接口',description='新增学生信息')
2026-09-21 15:10:45 +08:00
def add_students(s:StudentRequest
,db=Depends(get_db)):
2026-09-22 14:34:22 +08:00
d = s.model_dump(exclude_unset=True)
if not d:
raise HTTPException(status_code=400, detail='添加内容不能为空')
r = add_student_dao( o=d, db=db)
if r == 'conflict':
raise HTTPException(status_code=409, detail='身份证号已存在,请勿重复添加')
if r == 'error':
raise HTTPException(status_code=500, detail='更添加失败,请稍后重试')
return r