123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from fastapi import APIRouter, Path, Depends, Query, Body
- from sqlalchemy.ext.asyncio import AsyncSession
- from starlette.background import BackgroundTasks
- from bgtask.tasks import delete_related_object
- from crud.school import crud_grade, crud_school
- from models.user import SysUser
- from schemas.base import ReturnField
- from schemas.school.school import GradeDetail, GradeItemList, NewGrade, UpdateGrade
- from utils.depends import get_async_db, get_current_user
- router = APIRouter()
- # 学校年级列表
- @router.get("/grades",
- response_model=GradeItemList,
- response_model_exclude_none=True,
- summary="年级列表")
- async def get_grades(sid: int = Query(..., description="学校ID"),
- db: AsyncSession = Depends(get_async_db),
- res: ReturnField = Query("",
- description="返回字段,取值:id,name"),
- current_user: SysUser = Depends(get_current_user)):
- filters = {"school_id": sid}
- total, items = await crud_grade.find_all(db,
- filters=filters,
- return_fields=res)
- return {"total": total, "data": items}
- @router.post("/grades",
- response_model=GradeDetail,
- response_model_exclude_none=True,
- summary="创建年级")
- async def create_grade(info: NewGrade,
- db: AsyncSession = Depends(get_async_db),
- current_user: SysUser = Depends(get_current_user)):
- # 判断学校是否存在
- existed = await crud_school.count(db, filters={"id": info.school_id})
- if not existed:
- return {"errcode": 404, "mess": "学校不存在!"}
- # 判断年级是否重名
- existed = await crud_grade.count(db,
- filters={
- "school_id": info.school_id,
- "name": info.name
- })
- if existed:
- return {"errcode": 400, "mess": "存在同名年级!"}
- else:
- grade = await crud_grade.insert_one(db, info)
- return {"data": grade}
- @router.put("/grades/{gid}",
- response_model=GradeDetail,
- response_model_exclude_none=True,
- summary="更新年级")
- async def update_grade(gid: int = Path(..., description="年级ID"),
- info: UpdateGrade = Body(..., description="年级信息"),
- db: AsyncSession = Depends(get_async_db),
- current_user: SysUser = Depends(get_current_user)):
- # 判断提交参数
- info_dict = info.dict(exclude_none=True)
- if not info_dict:
- return {"errcode": 400, "mess": "提交参数为空!"}
- # 判断年级是否存在
- db_obj = await crud_grade.find_one(db, filters={"id": gid})
- if not db_obj:
- return {"errcode": 404, "mess": "年级不存在!"}
- grade = await crud_grade.update(db, db_obj, info)
- return {"data": grade}
- # 删除年级
- @router.delete("/grades/{gid}",
- response_model=GradeDetail,
- response_model_exclude_none=True,
- summary="删除年级")
- async def delete_school(bg_task: BackgroundTasks,
- sid: int = Query(..., description="学校ID"),
- gid: int = Path(..., description="年级ID"),
- db: AsyncSession = Depends(get_async_db),
- current_user: SysUser = Depends(get_current_user)):
- existed = await crud_grade.count(db, {"school_id": sid, "id": gid})
- if not existed:
- return {"errcode": 404, "mess": "年级不存在!"}
- else:
- await crud_grade.delete(db, obj_id=id)
- bg_task.add_task(delete_related_object, db, gid=gid)
- return {"data": None}
|