第二版_完整
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from dao.cls_mgmt_dao import ClsMgmtDAO
|
||||
from database import get_db
|
||||
from scheme.cls_mgmt_scheme import ClsMgmtResponse, ClsMgmtCreate
|
||||
from scheme.users import UserCreate, UserUpdate, UserResponse
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/", response_model=List[ClsMgmtResponse])
|
||||
# @router.get("/",response_model=ClsMgmtResponse)
|
||||
async def get_classes(
|
||||
skip: int = Query(0, ge=0, description="跳过的记录数"),
|
||||
limit: int = Query(100, ge=1, le=200, description="返回的最大记录数"),
|
||||
db: Session = Depends(get_db) # 依赖注入获得数据库会话
|
||||
):
|
||||
"""
|
||||
获取班级列表,支持分页
|
||||
"""
|
||||
classes = ClsMgmtDAO.get_all(db, skip=skip, limit=limit)
|
||||
return classes # FastAPI 自动根据 response_model 转换为 JSON
|
||||
|
||||
# @router.get("/{class_id}", response_model=ClsMgmtResponse)
|
||||
# @router.get("/{class_id}")
|
||||
# async def get_class(
|
||||
# class_id: str = Path(description="班级id:260814"),
|
||||
# db: Session = Depends(get_db)
|
||||
# ):
|
||||
# """
|
||||
# 根据班级 ID 获取详细信息
|
||||
# """
|
||||
# user = ClsMgmtDAO.get_by_id(db, class_id)
|
||||
# if not user:
|
||||
# raise HTTPException(status_code=404, detail="班级不存在")
|
||||
# return user
|
||||
|
||||
@router.get("/{class_id}",response_model=ClsMgmtResponse) # ← 加这一行
|
||||
async def get_class(
|
||||
class_id: str = Path(description="班级id:260814"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
根据班级 ID 获取详细信息
|
||||
"""
|
||||
cls = ClsMgmtDAO.get_by_id(db, class_id)
|
||||
if not cls:
|
||||
raise HTTPException(status_code=404, detail="班级不存在")
|
||||
return cls # 直接返回 ORM 对象,FastAPI 自动转成 ClsMgmtResponse
|
||||
# @router.get(
|
||||
# "/{class_id}",
|
||||
# response_model="ClsMgmtResponse",
|
||||
# summary="根据班级id获取详细信息"
|
||||
# )
|
||||
# async def get_class(
|
||||
# class_id: str = Path(description="班级id,例:260814"),
|
||||
# db: Session = Depends(get_db)
|
||||
# ):
|
||||
# cls = ClsMgmtDAO.get_by_id(db, class_id)
|
||||
# if not cls:
|
||||
# raise HTTPException(status_code=404, detail="班级不存在")
|
||||
# return cls
|
||||
|
||||
# @router.post("/{class_id}") # ← 加这一行
|
||||
# async def add_class(
|
||||
# class_id: str = Path(description="班级id:260814"),
|
||||
# db: Session = Depends(get_db)
|
||||
# ):
|
||||
# """
|
||||
# 根据班级 ID 获取详细信息
|
||||
# """
|
||||
# cls = ClsMgmtDAO.get_by_id(db, class_id)
|
||||
# if not cls:
|
||||
# raise HTTPException(status_code=404, detail="班级不存在")
|
||||
# return cls
|
||||
|
||||
@router.post(
|
||||
"/add_class",
|
||||
response_model=ClsMgmtResponse
|
||||
# status_code=201,
|
||||
# summary="新增班级"
|
||||
)
|
||||
async def create_class(
|
||||
cls_data: ClsMgmtCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
新增班级
|
||||
- 班级id自动生成:根据开课时间取后6位(如 2026-08-14 → 260814)
|
||||
- 先根据班级id验证是否已存在
|
||||
- 已存在 → 返回 400「班级已存在」
|
||||
- 不存在 → 新增班级
|
||||
"""
|
||||
# 第一步:根据开课时间生成班级id
|
||||
class_id = ClsMgmtDAO.generate_class_id(cls_data.cls_start_date)
|
||||
# 第二步:根据班级id验证是否已存在
|
||||
if ClsMgmtDAO.exists(db, class_id):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"班级已存在(班级id:{class_id})"
|
||||
)
|
||||
# 第三步:不存在则新增
|
||||
new_cls = ClsMgmtDAO.create(db, cls_data)
|
||||
return new_cls
|
||||
|
||||
@router.delete(
|
||||
"/del_class/{class_id}",
|
||||
# response_model=ClsMgmtResponse
|
||||
# status_code=201,
|
||||
# summary="新增班级"
|
||||
)
|
||||
async def del_class(
|
||||
class_id: str = Path(description="班级id:260814"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
删除班级
|
||||
"""
|
||||
print(class_id)
|
||||
del_cls = ClsMgmtDAO.delete_class(db, class_id)
|
||||
return del_cls
|
||||
|
||||
@router.put(
|
||||
"/update_class/{class_id}",
|
||||
response_model=ClsMgmtResponse
|
||||
# status_code=201,
|
||||
# summary="新增班级"
|
||||
)
|
||||
async def update_class(
|
||||
cls_data: ClsMgmtCreate,
|
||||
class_id: str = Path(description="班级id:260814"),
|
||||
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
根据班级ID修改班主任和授课老师信息
|
||||
"""
|
||||
update_cls = ClsMgmtDAO.update_cls(db, cls_data,class_id)
|
||||
return update_cls
|
||||
|
||||
|
||||
Reference in New Issue
Block a user