grade.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 delete_related_object
  7. from crud.school import crud_grade, crud_school
  8. from models.user import SysUser
  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("",
  21. description="返回字段,取值:id,name"),
  22. current_user: SysUser = Depends(get_current_user)):
  23. filters = {"school_id": sid}
  24. total, items = await crud_grade.find_all(db,
  25. filters=filters,
  26. return_fields=res)
  27. return {"total": total, "data": items}
  28. @router.post("/grades",
  29. response_model=GradeDetail,
  30. response_model_exclude_none=True,
  31. summary="创建年级")
  32. async def create_grade(info: NewGrade,
  33. db: AsyncSession = Depends(get_async_db),
  34. current_user: SysUser = Depends(get_current_user)):
  35. # 判断学校是否存在
  36. existed = await crud_school.count(db, filters={"id": info.school_id})
  37. if not existed:
  38. return {"errcode": 404, "mess": "学校不存在!"}
  39. # 判断年级是否重名
  40. existed = await crud_grade.count(db,
  41. filters={
  42. "school_id": info.school_id,
  43. "name": info.name
  44. })
  45. if existed:
  46. return {"errcode": 400, "mess": "存在同名年级!"}
  47. else:
  48. grade = await crud_grade.insert_one(db, info)
  49. return {"data": grade}
  50. @router.put("/grades/{gid}",
  51. response_model=GradeDetail,
  52. response_model_exclude_none=True,
  53. summary="更新年级")
  54. async def update_grade(gid: int = Path(..., description="年级ID"),
  55. info: UpdateGrade = Body(..., description="年级信息"),
  56. db: AsyncSession = Depends(get_async_db),
  57. current_user: SysUser = Depends(get_current_user)):
  58. # 判断提交参数
  59. info_dict = info.dict(exclude_none=True)
  60. if not info_dict:
  61. return {"errcode": 400, "mess": "提交参数为空!"}
  62. # 判断年级是否存在
  63. db_obj = await crud_grade.find_one(db, filters={"id": gid})
  64. if not db_obj:
  65. return {"errcode": 404, "mess": "年级不存在!"}
  66. grade = await crud_grade.update(db, db_obj, info)
  67. return {"data": grade}
  68. # 删除年级
  69. @router.delete("/grades/{gid}",
  70. response_model=GradeDetail,
  71. response_model_exclude_none=True,
  72. summary="删除年级")
  73. async def delete_school(bg_task: BackgroundTasks,
  74. sid: int = Query(..., description="学校ID"),
  75. gid: int = Path(..., description="年级ID"),
  76. db: AsyncSession = Depends(get_async_db),
  77. current_user: SysUser = Depends(get_current_user)):
  78. existed = await crud_grade.count(db, {"school_id": sid, "id": gid})
  79. if not existed:
  80. return {"errcode": 404, "mess": "年级不存在!"}
  81. else:
  82. await crud_grade.delete(db, obj_id=id)
  83. bg_task.add_task(delete_related_object, db, gid=gid)
  84. return {"data": None}