student.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from fastapi import Query, Depends
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from crud.user import crud_student
  6. from models.user import Teacher
  7. from utils.depends import get_async_db, get_current_user
  8. # 学生列表
  9. async def get_students(page: int = 1,
  10. size: int = 10,
  11. cid: int = Query(..., description="班级ID"),
  12. sno: str = Query("", description="学号"),
  13. name: str = Query("", description="学生姓名"),
  14. db: AsyncSession = Depends(get_async_db),
  15. current_user: Teacher = Depends(get_current_user)):
  16. # 过滤条件
  17. filters = {"class_id": cid}
  18. if sno:
  19. filters["sno"] = sno
  20. if name:
  21. filters["name"] = name
  22. offset = (page - 1) * size
  23. return_fields = ["id", "name", "sno", "class_id", "class_name"]
  24. total, items = await crud_student.find_all(db,
  25. filters=filters,
  26. offset=offset,
  27. limit=size,
  28. return_fields=return_fields)
  29. return {"total": total, "data": items}