grade.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Path, Depends, Query, Body
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from starlette.background import BackgroundTasks
  6. from bgtask.tasks import bgtask_delete_related_object
  7. from crud.school import crud_grade, crud_school
  8. from models.user import Admin
  9. from schemas.base import ReturnField
  10. from schemas.school.school import GradeDetail, GradeItemList, NewGrade, UpdateGrade
  11. from utils.depends import get_async_db, get_current_user
  12. router = APIRouter()
  13. # 学校年级列表
  14. @router.get("/grades",
  15. response_model=GradeItemList,
  16. response_model_exclude_none=True,
  17. summary="年级列表")
  18. async def get_grades(sid: int = Query(..., description="学校ID"),
  19. db: AsyncSession = Depends(get_async_db),
  20. res: ReturnField = Query("", description="返回字段,取值:id,name"),
  21. current_user: Admin = Depends(get_current_user)):
  22. total, items = await crud_grade.find_all(db, filters={"school_id": sid}, return_fields=res)
  23. return {"total": total, "data": items}
  24. @router.post("/grades",
  25. response_model=GradeDetail,
  26. response_model_exclude_none=True,
  27. summary="创建年级")
  28. async def create_grade(info: NewGrade,
  29. db: AsyncSession = Depends(get_async_db),
  30. current_user: Admin = Depends(get_current_user)):
  31. # 判断学校是否存在
  32. existed = await crud_school.count(db, filters={"id": info.school_id})
  33. if not existed:
  34. return {"errcode": 404, "mess": "学校不存在!"}
  35. # 判断年级是否重名
  36. existed = await crud_grade.count(db, filters={"school_id": info.school_id, "name": info.name})
  37. if existed:
  38. return {"errcode": 400, "mess": "存在同名年级!"}
  39. else:
  40. grade = await crud_grade.insert_one(db, info)
  41. return {"data": grade}
  42. @router.put("/grades/{gid}",
  43. response_model=GradeDetail,
  44. response_model_exclude_none=True,
  45. summary="更新年级")
  46. async def update_grade(gid: int = Path(..., description="年级ID"),
  47. info: UpdateGrade = Body(..., description="年级信息"),
  48. db: AsyncSession = Depends(get_async_db),
  49. current_user: Admin = Depends(get_current_user)):
  50. # 判断提交参数
  51. info_dict = info.dict(exclude_none=True)
  52. if not info_dict:
  53. return {"errcode": 400, "mess": "提交参数为空!"}
  54. # 判断年级是否存在
  55. db_obj = await crud_grade.find_one(db, filters={"id": gid})
  56. if not db_obj:
  57. return {"errcode": 404, "mess": "年级不存在!"}
  58. grade = await crud_grade.update(db, db_obj, info)
  59. return {"data": grade}
  60. # 删除年级
  61. @router.delete("/grades/{gid}",
  62. response_model=GradeDetail,
  63. response_model_exclude_none=True,
  64. summary="删除年级")
  65. async def delete_grade(bgtask: BackgroundTasks,
  66. sid: int = Query(..., description="学校ID"),
  67. gid: int = Path(..., description="年级ID"),
  68. db: AsyncSession = Depends(get_async_db),
  69. current_user: Admin = Depends(get_current_user)):
  70. existed = await crud_grade.count(db, filters={"school_id": sid, "id": gid})
  71. if not existed:
  72. return {"errcode": 404, "mess": "年级不存在!"}
  73. else:
  74. await crud_grade.delete(db, obj_id=id)
  75. bgtask.add_task(bgtask_delete_related_object, gid=gid)
  76. return {"data": None}