forked from wolin_ck666_999/StuManagerSys
上传文件至「api」
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
from fastapi import APIRouter,Depends,HTTPException,Query
|
||||
from dao.class_dao import add_class_dao, update_class_dao, delete_class_dao, hard_delete_class_dao,class_no_exist,get_class_list_dao,get_class_by_id_dao
|
||||
from schemas.class_request import ClassCreate,ClassResponse,ClassUpdate
|
||||
from database import get_db
|
||||
from typing import Optional
|
||||
from sqlalchemy.orm import Session
|
||||
class_app = APIRouter()
|
||||
@class_app.post('',response_model=ClassResponse)
|
||||
def add_class(c:ClassCreate,db=Depends(get_db)):
|
||||
if class_no_exist(c.class_id,db):
|
||||
raise HTTPException(status_code=404,detail='该编号已经存在')
|
||||
ok,result = add_class_dao(c.model_dump(),db)
|
||||
if not ok:
|
||||
raise HTTPException(status_code=500,detail='服务器繁忙或指定的老师不存在')
|
||||
return ClassResponse(totals=1,data=result)
|
||||
@class_app.put('{id}')
|
||||
def update_class(id:int,c:ClassUpdate,db=Depends(get_db)):
|
||||
d = c.model_dump()
|
||||
r = update_class_dao(id=id ,update_data=d,db=db)
|
||||
if not r:
|
||||
raise HTTPException(status_code=500,detail='更新失败')
|
||||
return{'code':200,'total':1,'detail':'更新成功'}
|
||||
'''
|
||||
@class_app.get('')
|
||||
def get_class(id:int,db=Depends(get_db),page:int=1,page_size:int=10):
|
||||
rows = get_class_dao(id=id,db=db,page=page,page_size=page_size)
|
||||
if rows:
|
||||
return rows
|
||||
raise HTTPException(status_code=404,detail='该班级不存在')
|
||||
'''
|
||||
@class_app.get('')
|
||||
def get_classes(
|
||||
id: Optional[int] = Query(None, description="班级ID"),
|
||||
class_id: Optional[str] = Query(None, description="班级编号"),
|
||||
class_name: Optional[str] = Query(None, description="班级名称(支持模糊搜索)"),
|
||||
ht_id: Optional[int] = Query(None, description="班主任ID"),
|
||||
t_id: Optional[int] = Query(None, description="教师ID"),
|
||||
page: int = Query(1, ge=1, description="页码"),
|
||||
page_size: int = Query(10, ge=1, le=100, description="每页条数"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
# 如果明确传了 id,当作单条查询处理
|
||||
if id is not None:
|
||||
single_class = get_class_by_id_dao(id=id, db=db)
|
||||
if not single_class:
|
||||
raise HTTPException(status_code=404, detail="该班级不存在")
|
||||
return single_class
|
||||
|
||||
# 多条件列表查询
|
||||
total, items = get_class_list_dao(
|
||||
db=db,
|
||||
class_no=class_id,
|
||||
class_name=class_name,
|
||||
ht_id=ht_id,
|
||||
t_id=t_id,
|
||||
page=page,
|
||||
page_size=page_size
|
||||
)
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"items": items
|
||||
}
|
||||
@class_app.delete('/{id}') #软删除
|
||||
def delete_class(id:int,db=Depends(get_db)):
|
||||
rows = delete_class_dao(id=id,db=db)
|
||||
if not rows:
|
||||
raise HTTPException(status_code=404,detail='该班级不存在或已删除')
|
||||
return{'code':200,'total':rows,'detail':'删除成功'}
|
||||
|
||||
@class_app.delete('{id}/hard')
|
||||
def hard_delete_class(id:int,db=Depends(get_db)):
|
||||
rows = hard_delete_class_dao(id=id,db=db)
|
||||
if not rows:
|
||||
raise HTTPException(status_code=404,detail='该班级不存在')
|
||||
return{'code':200,'total':rows,'detail':'物理删除成功'}
|
||||
Reference in New Issue
Block a user