Files
SST/PythonProject/api/student_api.py
T
2026-09-21 14:44:11 +08:00

48 lines
1.8 KiB
Python

from fastapi import APIRouter,Depends,HTTPException
from dao.student_dao import delete_student_dao,select_all,update_student_dao,get_student_dao,select_age
from schema.student_schema import StudentRequest,StudentResponse
from util.database import get_db
StudentAPI = APIRouter()
@StudentAPI.get("/age",response_model=StudentResponse,tags=['统计分析模块'],description='根据年龄查询学生信息')
def get_students(min_age:int|None=None,max_age:int|None=None):
l = select_age(min_age,max_age)
return [i for i in l]
@StudentAPI.get("/all",response_model=StudentResponse,tags=['统计分析模块'],description='统计每个班级的学员总数和男女生总数')
def get_students():
l = select_all()
return [i for i in l]
@StudentAPI.get('/',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='学生不存在')
@StudentAPI.put('/{stu_id}')
def update_orders(s:StudentRequest,stu_id:int,db=Depends(get_db)):
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}')
def del_student(stu_id:int,db=Depends(get_db)):
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':'删除成功'}