第一次提交
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
from fastapi import APIRouter,Depends, HTTPException,Query
|
||||
from sqlalchemy.orm import Session
|
||||
from database import get_db
|
||||
from dao.employments import get_student_id,get_class_id,add_update_employment,get_student_multi_condition
|
||||
from dao.student import get_student_by_id
|
||||
from schemas.employments import Emp_Class,Emp_add_update,Emp_multi_Conditions
|
||||
from typing import List,Optional
|
||||
from dao.employments import delete_employment
|
||||
router = APIRouter(
|
||||
prefix="/employment", tags=["学生就业管理"] # 该路由下所有路径的前缀
|
||||
)
|
||||
|
||||
# 1. GET /employment/students/{student_id} 获取单个学生就业信息
|
||||
@router.get("/students/{student_id}", response_model=Emp_Class, summary="获取单个学生就业信息")
|
||||
def get_student_employment(student_id: str, db: Session = Depends(get_db)):
|
||||
stu = get_student_by_id(db, student_id)
|
||||
if not stu:
|
||||
raise HTTPException(status_code=404, detail="学生不存在")
|
||||
isbool = get_student_id(db, student_id)
|
||||
if not isbool:
|
||||
raise HTTPException(status_code=404, detail="学生不存在")
|
||||
return Emp_Class(
|
||||
id=stu.id,
|
||||
name=stu.name,
|
||||
class_name=stu.class_name,
|
||||
offer_open=isbool.offer_open if isbool else None,
|
||||
offer_review=isbool.offer_review if isbool else None,
|
||||
emp=isbool.emp if isbool else None,
|
||||
sal=isbool.sal if isbool else None,
|
||||
is_deleted=isbool.is_deleted
|
||||
)
|
||||
|
||||
|
||||
# 2. GET /employment/class/{class_id} 获取班级全部学生就业信息
|
||||
@router.get("/class/{class_id}", response_model=List[Emp_Class], summary="获取班级下所有学生就业信息")
|
||||
def get_class_employment(class_id: str, db: Session = Depends(get_db)):
|
||||
rows =get_class_id(db, class_id)
|
||||
result_list = []
|
||||
for stu, isbool in rows:
|
||||
if not stu:
|
||||
raise HTTPException(status_code=404, detail="学生不存在")
|
||||
if not isbool:
|
||||
raise HTTPException(status_code=404, detail="学生不存在")
|
||||
result_list.append(Emp_Class(
|
||||
id=stu.id,
|
||||
name=stu.name,
|
||||
class_name=stu.class_name,
|
||||
offer_open=isbool.offer_open if isbool else None,
|
||||
offer_review=isbool.offer_review if isbool else None,
|
||||
emp=isbool.emp if isbool else None,
|
||||
sal=isbool.sal if isbool else None,
|
||||
is_deleted=isbool.is_deleted
|
||||
))
|
||||
return result_list
|
||||
|
||||
# 3. POST /employment/students/{student_id} 添加/更新就业信息
|
||||
@router.post("/students/{student_id}", response_model=Emp_Class, summary="新增或更新学生就业信息")
|
||||
def upsert_student_employment(
|
||||
student_id: str,
|
||||
emp_data: Emp_add_update,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
stu = get_student_by_id(db, student_id)
|
||||
if not stu:
|
||||
raise HTTPException(status_code=404, detail="学生不存在,请先创建学生基础信息")
|
||||
emp = add_update_employment(db, student_id, emp_data, stu)
|
||||
|
||||
return Emp_Class(
|
||||
id=stu.id,
|
||||
name=stu.name,
|
||||
class_name=stu.class_name,
|
||||
offer_open=emp.offer_open,
|
||||
offer_review=emp.offer_review,
|
||||
emp=emp.emp,
|
||||
sal=emp.sal,
|
||||
is_deleted=emp.is_deleted
|
||||
)
|
||||
#多条件查询
|
||||
@router.get("/query", response_model=List[Emp_Class], summary="多条件查询就业信息")
|
||||
def multi_query_employment(
|
||||
mid: Optional[str] = Query(default=None, title="学生id", description="主键"),#title是前端可以看到的输入框内容
|
||||
name: Optional[str] = Query(default=None, title="学生姓名"),
|
||||
min_sal: Optional[float] = Query(default=None, ge=0, title="最低薪资"),
|
||||
max_sal: Optional[float] = Query(default=None, ge=0, title="最高薪资"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
rows = get_student_multi_condition(db, mid, name, min_sal, max_sal)
|
||||
result_list = []
|
||||
|
||||
for stu, isbool in rows:
|
||||
result_list.append(Emp_Class(
|
||||
id=stu.id,
|
||||
name=stu.name,
|
||||
class_name=stu.class_name,
|
||||
offer_open=isbool.offer_open if isbool else None,
|
||||
offer_review=isbool.offer_review if isbool else None,
|
||||
emp=isbool.emp if isbool else None,
|
||||
sal=isbool.sal if isbool else None,
|
||||
is_deleted=isbool.is_deleted if isbool else False # ← 修复了 NoneType 报错
|
||||
))
|
||||
|
||||
return result_list
|
||||
|
||||
|
||||
@router.post('/deleted',summary="逻辑删除学生就业信息")
|
||||
def delete_emp( student_id:str,db: Session = Depends(get_db)):
|
||||
delete_employment(db,student_id)
|
||||
if delete_employment(db,student_id):
|
||||
return f"学生id:{student_id}删除成功"
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="学生不存在,请先创建学生基础信息")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user