149 lines
5.8 KiB
Python
149 lines
5.8 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from database import get_db
|
|
from dao.stu_dao import StudentDao
|
|
from scheme.stu_scheme import StudentCreate, StudentUpdate, StudentResponse
|
|
|
|
|
|
router = APIRouter()
|
|
# ============= 查询 =============
|
|
#-----------分页查询所有学生-----------此处限制,dao层只是默认
|
|
@router.get("/",response_model=List[StudentResponse],summary="查询所有学生")
|
|
async def get_students(
|
|
skip: int = Query(0, ge=0, description="跳过的记录数"),
|
|
limit: int = Query(50, ge=1, le=80, description="每页数量最大80"),
|
|
db: Session = Depends(get_db) # 依赖注入获得数据库会话
|
|
):
|
|
students = StudentDao.get_all(db, skip=skip, limit=limit)
|
|
return students
|
|
|
|
#-----------根据id查询学生-----------
|
|
@router.get("/{stu_id}", response_model=StudentResponse,summary="根据ID查询学生")
|
|
async def get_student_by_id(
|
|
stu_id: str,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
id_student = StudentDao.get_by_id(db,stu_id)
|
|
if not id_student:
|
|
raise HTTPException(status_code=404, detail="学生不存在")
|
|
return id_student
|
|
|
|
#-----------根据name查询学生-----------
|
|
@router.get("/name/{stu_name}", response_model=List[StudentResponse],summary="根据姓名查询学生")
|
|
async def get_student_by_name(
|
|
stu_name: str,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
name_student = StudentDao.get_by_name(db,stu_name)
|
|
if not name_student:
|
|
raise HTTPException(status_code=404, detail="学生不存在")
|
|
return name_student
|
|
|
|
#-----------根据班级ID查询学生-----------
|
|
@router.get("/class/{cls_id}", response_model=List[StudentResponse],summary="班级ID查询学生")
|
|
async def get_student_by_class(
|
|
cls_id: str,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
class_student = StudentDao.get_by_class(db,cls_id)
|
|
if not class_student:
|
|
raise HTTPException(status_code=404, detail="学生不存在")
|
|
return class_student
|
|
|
|
#===========创建学生信息===========
|
|
@router.post("/", response_model=StudentResponse, status_code=201,summary="新建学生")
|
|
async def create_student(
|
|
student_data: StudentCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
创建新学生:
|
|
1. 校验班级是否存在
|
|
2. 生成学号(s + 班级ID + 两位序号)
|
|
3. 组装数据,存入数据库
|
|
"""
|
|
#----------- 校验班级是否存在 -----------
|
|
cls_exists = StudentDao.get_class_by_id(db, student_data.cls_id)
|
|
if not cls_exists:
|
|
raise HTTPException(status_code=400,detail=f"班级 {student_data.cls_id} 不存在,请先创建班级")
|
|
#----------- 生成学号 -----------
|
|
count = StudentDao.count_by_class(db, student_data.cls_id)
|
|
next_seq = count + 1
|
|
student_id = f"s{student_data.cls_id}{next_seq:02d}"
|
|
|
|
#----------- 组装字典(补上后端生成的字段) -----------
|
|
student_dict = {
|
|
"id": student_id, # 后端生成
|
|
"cls_id": student_data.cls_id, # 来自请求体
|
|
"name": student_data.name,
|
|
"gender": student_data.gender,
|
|
"age": student_data.age,
|
|
"hometown": student_data.hometown,
|
|
"grad_school": student_data.grad_school,
|
|
"major": student_data.major,
|
|
"education": student_data.education,
|
|
"enr_date": student_data.enr_date,
|
|
"grad_date": student_data.grad_date,
|
|
"advisor_id": student_data.advisor_id,
|
|
"state": "在读", # 表模型默认值
|
|
"is_deleted": 0} # 表模型默认值
|
|
|
|
#----------- 调用 DAO 创建 -----------
|
|
try:
|
|
new_student = StudentDao.create(db, student_dict)
|
|
except Exception:
|
|
db.rollback()
|
|
raise HTTPException(status_code=400, detail="数据异常,请重试")
|
|
return new_student
|
|
#============= 修改 ============
|
|
@router.put("/{stu_id}", response_model=StudentResponse,summary="通过ID查找修改学生信息")
|
|
async def update_student(
|
|
stu_id: str,
|
|
stu_data: StudentUpdate,
|
|
db: Session = Depends(get_db)):
|
|
"""
|
|
修改学生信息:
|
|
1. 校验学生是否存在
|
|
2. 如果修改了班级,校验新班级是否存在
|
|
3. 调用 DAO 更新
|
|
"""
|
|
#----------- 校验学生是否存在 -----------
|
|
db_student = StudentDao.get_by_id(db, stu_id)
|
|
if not db_student:
|
|
raise HTTPException(status_code=404, detail="学生不存在")
|
|
#----------- 如果修改了班级,校验新班级是否存在 -----------
|
|
if stu_data.cls_id and stu_data.cls_id != db_student.cls_id:
|
|
cls_exists = StudentDao.get_class_by_id(db, stu_data.cls_id)
|
|
if not cls_exists:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"班级 {stu_data.cls_id} 不存在")
|
|
# 请求体只校验了两个日期都传的情况,这里补上“只传一个”的情况
|
|
enr = stu_data.enr_date or db_student.enr_date
|
|
grad = stu_data.grad_date or db_student.grad_date
|
|
if grad <= enr:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="毕业日期必须晚于入学日期")
|
|
#----------- 调用 DAO 更新 -----------
|
|
updated_student = StudentDao.update(db, stu_id, stu_data)
|
|
return updated_student
|
|
|
|
#=============删除学生(软删除)===============
|
|
@router.delete("/{stu_id}", response_model=StudentResponse,summary="通过ID查找删除学生信息")
|
|
async def delete_student(
|
|
stu_id: str,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
删除学生信息(逻辑删除):
|
|
1. 校验学生是否存在
|
|
2. 把 is_deleted 标记为 1
|
|
"""
|
|
# ------------- 调用 DAO 删除 -------------
|
|
deleted_student = StudentDao.delete(db, stu_id)
|
|
if not deleted_student:
|
|
raise HTTPException(status_code=404, detail="学生不存在")
|
|
return deleted_student |