Files
sss/student_system_final/api1/students.py
T
2026-09-21 22:13:04 +08:00

48 lines
2.2 KiB
Python

# api/students.py
# 本文件定义学生模块的所有 API 路由(Controller 层)
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from database import get_db
from dao.student_dao import StudentDAO
from scheme.students import StudentCreate, StudentUpdate, StudentResponse, StudentListResponse
# 创建路由器
router = APIRouter(prefix="/students", tags=["学生管理"])
# 查询学生列表(支持按编号、姓名、班级等条件筛选)
@router.get("/list/", response_model=StudentListResponse)
async def get_student_list(
sid: int = None,
name: str = Query(None,max_length=4,description='名字的模糊查询'),
cid: int =Query(None,description="根据学生的班级id查询"),
gender: str = Query(None,max_length=1,description="选择项(男\女)根据学生性别查询,查询男同学或者女同学"),
education: str = Query(None,max_length=2,description="选择项(硕士\本科)根据学生学历查询,查询该学历学生"),
page: int = Query(1,description="查看的页数"),
size: int = Query(5,description="查询每一页的数量"),
db: Session = Depends(get_db)
):
total, items = StudentDAO.get_list(db, sid=sid, name=name, cid=cid,
gender=gender, education=education,
page=page, size=size)
return {"total": total, "items": items}
# 按学号查单个学生
@router.get("/{sid}", response_model=StudentResponse)
async def get_student_by_id(sid: int, db: Session = Depends(get_db)):
return StudentDAO.get_by_id(db, sid)
# 按照用户输入新增学生
@router.post("/create", response_model=StudentResponse)
async def create_student(data: StudentCreate, db: Session = Depends(get_db)):
return StudentDAO.create(db, data)
# 更新学生信息
@router.put("/update/{sid}", response_model=StudentResponse)
async def update_student(sid: int, data: StudentUpdate, db: Session = Depends(get_db)):
return StudentDAO.update(db, sid, data)
# 删除学生
@router.post("/delete/{sid}", response_model=StudentResponse)
async def delete_student(sid: int, db: Session = Depends(get_db)):
return StudentDAO.delete(db, sid)