60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
# api/employ_api.py
|
|
# 学生就业管理模块:接口层只负责 HTTP 出入参,查库/校验/事务交给 dao.employ_dao
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from dao import employ_dao
|
|
from database import get_db
|
|
from schemas.employ_schema import EmployCreate, EmployUpdate, EmployOut
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", summary="分页查询就业记录")
|
|
def list_employ(
|
|
page: int = Query(1, ge=1, description="页码"),
|
|
size: int = Query(10, ge=1, le=100, description="每页数量"),
|
|
emp_company: Optional[str] = Query(None, description="按公司名称模糊查询"),
|
|
employed: Optional[bool] = Query(None, description="是否已就业(true/false)"),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
total, items = employ_dao.list_employ(
|
|
db, page=page, size=size, emp_company=emp_company, employed=employed
|
|
)
|
|
return {
|
|
"total": total,
|
|
"page": page,
|
|
"size": size,
|
|
"items": [EmployOut.model_validate(x) for x in items],
|
|
}
|
|
|
|
|
|
@router.get("/{stu_id}", summary="查询单个学生的就业信息")
|
|
def get_employ(stu_id: str, db: Session = Depends(get_db)):
|
|
"""查询就业详情,附带学生姓名。"""
|
|
emp = employ_dao.assert_employ_alive(db, stu_id)
|
|
data = EmployOut.model_validate(emp).model_dump()
|
|
data["student_name"] = emp.stu.name if emp.stu else None
|
|
return data
|
|
|
|
|
|
@router.post("", response_model=EmployOut, status_code=201, summary="新增就业记录")
|
|
def create_employ(payload: EmployCreate, db: Session = Depends(get_db)):
|
|
"""新增就业记录(学生必须存在,且一对一不可重复,由 DAO 校验)。"""
|
|
return employ_dao.create_employ(db, payload.model_dump())
|
|
|
|
|
|
@router.put("/{stu_id}", response_model=EmployOut, summary="更新就业信息")
|
|
def update_employ(stu_id: str, payload: EmployUpdate, db: Session = Depends(get_db)):
|
|
"""按传入字段局部更新就业信息。"""
|
|
return employ_dao.update_employ(db, stu_id, payload.model_dump(exclude_unset=True))
|
|
|
|
|
|
@router.delete("/{stu_id}", summary="删除就业记录(软删除)")
|
|
def delete_employ(stu_id: str, db: Session = Depends(get_db)):
|
|
"""软删除就业记录。"""
|
|
employ_dao.soft_delete_employ(db, stu_id)
|
|
return {"message": "删除成功", "stu_id": stu_id} |