2026-09-21 11:43:34 +08:00
|
|
|
from fastapi import APIRouter,Depends,HTTPException
|
2026-09-20 15:32:28 +08:00
|
|
|
from schema.student_schema import StudentResponse
|
2026-09-21 11:43:34 +08:00
|
|
|
from dao.student_dao import delete_student_dao,add_student_dao,update_student_dao,get_student_dao,select_age
|
2026-09-21 15:10:45 +08:00
|
|
|
from schema.student_schema import StudentRequest,StudentResponse,StugetResponse
|
2026-09-21 11:43:34 +08:00
|
|
|
from model.student_model import Student_Model
|
|
|
|
|
from util.database import get_db
|
2026-09-20 15:32:28 +08:00
|
|
|
|
|
|
|
|
|
2026-09-21 11:43:34 +08:00
|
|
|
StudentAPI = APIRouter()
|
|
|
|
|
|
|
|
|
|
@StudentAPI.get("/age",response_model=StudentResponse,tags=['统计分析模块'],description='根据年龄查询学生信息')
|
2026-09-21 15:10:45 +08:00
|
|
|
def get_students(min_age:int|None=None
|
|
|
|
|
,max_age:int|None=None):
|
2026-09-20 15:32:28 +08:00
|
|
|
l = select_age(min_age,max_age)
|
|
|
|
|
return [i for i in l]
|
|
|
|
|
|
2026-09-21 11:43:34 +08:00
|
|
|
@StudentAPI.get('',description='查询学生信息')
|
|
|
|
|
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 11:43:34 +08:00
|
|
|
r=get_student_dao(stu_id,stu_name,class_id,db,page,page_size)
|
|
|
|
|
if r:
|
|
|
|
|
return r
|
|
|
|
|
raise HTTPException(status_code=404,detail='学生不存在')
|
|
|
|
|
|
|
|
|
|
@StudentAPI.put('/{stu_id}')
|
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)
|
|
|
|
|
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
|
|
|
|
|
if not r:
|
|
|
|
|
raise HTTPException(status_code=500, detail='没有更新!')
|
|
|
|
|
return {'code':200,'totals':r,'detail':'更新成功'}
|
|
|
|
|
|
|
|
|
|
@StudentAPI.delete('/{stu_id}')
|
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:
|
|
|
|
|
raise HTTPException(status_code=500,detail='没有删除')
|
|
|
|
|
return {'code':200,'totals':rows,'detail':'删除成功'}
|
|
|
|
|
|
2026-09-21 15:10:45 +08:00
|
|
|
@StudentAPI.post('',response_model=list[StugetResponse])
|
|
|
|
|
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:
|
|
|
|
|
raise HTTPException(status_code=500,detail='添加失败')
|
2026-09-21 11:43:34 +08:00
|
|
|
|
|
|
|
|
|
2026-09-21 15:10:45 +08:00
|
|
|
@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='学生不存在!')
|