This commit is contained in:
msj
2026-09-21 18:04:38 +08:00
parent 9b4e3cfe06
commit e5c2284ad1
3 changed files with 74 additions and 52 deletions
+25 -22
View File
@@ -1,28 +1,41 @@
from fastapi import APIRouter,Depends,HTTPException
from dao.student_dao import delete_student_dao,add_student_dao,update_student_dao,get_student_dao
from schema.student_schema import StudentRequest,StudentResponse,StugetResponse
from dao.student_dao import *
from schema.student_schema import *
from util.database import get_db
StudentAPI = APIRouter()
@StudentAPI.get('/',description='查询学生信息')
@StudentAPI.get('/',response_model=list[StudentPageResponse],description='查询学生信息')
def get_students(stu_id:int|None=None
,stu_name:str|None=None
,class_id:int|None=None
,db=Depends(get_db)
,page:int=1
,page_size:int=10):
r=get_student_dao(stu_id,stu_name,class_id,db,page,page_size)
if r:
return r
raise HTTPException(status_code=404,detail='学生不存在')
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='学生不存在')
return {'code': 200
, 'detail': 'ok'
, 'page': page
, 'page_size': page_size
, 'total': total
, 'data': r}
@StudentAPI.put('/{stu_id}')
def update_students(s:StudentRequest
,stu_id:int
,db=Depends(get_db)):
d = s.model_dump(exclude_unset=True)
if not d:
raise HTTPException(status_code=400, detail='更新内容不能为空!')
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
if not r:
raise HTTPException(status_code=500, detail='没有更新!')
@@ -36,23 +49,13 @@ def del_students(stu_id:int
raise HTTPException(status_code=500,detail='没有删除')
return {'code':200,'totals':rows,'detail':'删除成功'}
@StudentAPI.post('/',response_model=list[StugetResponse])
@StudentAPI.post('/',response_model=StugetResponse,description='新增学生')
def add_students(s:StudentRequest
,db=Depends(get_db)):
d=s.model_dump(exclude_unset=True)
r=add_student_dao(o=d,db=db)
if not r:
d=s.model_dump(exclude_unset=False)
stu_new=add_student_dao(o=d,db=db)
if not stu_new:
raise HTTPException(status_code=500,detail='添加失败')
return stu_new
@StudentAPI.put('/')
def get_students(stu_id:int|None = None
,stu_name:str|None = None
,class_id:int|None=None
,db=Depends(get_db)
,page:int=1
,page_size:int=10):
r = get_student_dao(stu_id,stu_name,class_id,db,page,page_size )
if r:
return r
raise HTTPException(status_code=404,detail='学生不存在!')