131 lines
3.6 KiB
Python
131 lines
3.6 KiB
Python
# path: api/employment_api.py
|
||
# time:2026年9月12日14:18
|
||
# title:就业管理 接口
|
||
# author:周兴
|
||
# info:负责调用employment的dao文件里面增删改查逻辑
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||
from sqlalchemy.orm import Session
|
||
|
||
from database import get_db # 把数据库会话获取函数导入API层,让接口可以通过依赖注入获得数据库连接
|
||
|
||
from dao.employment_dao import (
|
||
create_employment,
|
||
search_employments,
|
||
update_employment,
|
||
delete_employment,
|
||
check_student_exists,
|
||
check_employment_exists
|
||
)
|
||
|
||
from schema.employment_schema import (
|
||
EmploymentCreate,
|
||
EmploymentUpdate,
|
||
EmploymentResponse
|
||
)
|
||
|
||
# 创建就业管理子路由
|
||
router = APIRouter()
|
||
|
||
# 新增就业信息
|
||
@router.post("/addEmployment", response_model=EmploymentResponse,summary="新增就业信息")
|
||
async def add_employment(
|
||
employment_data: EmploymentCreate,
|
||
db: Session = Depends(get_db)
|
||
):
|
||
# 1、先检查学生是否存在
|
||
student = check_student_exists(
|
||
db=db,
|
||
student_id=employment_data.student_id
|
||
)
|
||
# 如果学生不存在
|
||
if student is None:
|
||
raise HTTPException(
|
||
status_code=404,
|
||
detail="该学生不存在"
|
||
)
|
||
|
||
|
||
# 2、检查该学生是否已经存在就业信息
|
||
existing_employment = check_employment_exists(
|
||
db=db,
|
||
student_id=employment_data.student_id
|
||
)
|
||
if existing_employment is not None:
|
||
raise HTTPException(
|
||
status_code=409,
|
||
detail="该学生已经存在就业信息"
|
||
)
|
||
|
||
# 3、如果学生存在,再新增就业信息
|
||
try:
|
||
employment = create_employment(db, employment_data)
|
||
except ValueError as error:
|
||
raise HTTPException(status_code=422, detail=str(error)) from None
|
||
|
||
return employment
|
||
|
||
# 查询就业信息
|
||
@router.get("/getEmployments", response_model=list[EmploymentResponse],summary="查询就业信息")
|
||
async def get_employments(
|
||
student_id: int | None = Query(default=None),
|
||
company_name: str | None = Query(default=None),
|
||
min_salary: float | None = Query(default=None, ge=0),
|
||
max_salary: float | None = Query(default=None, ge=0),
|
||
db: Session = Depends(get_db)
|
||
):
|
||
employment_list = search_employments(
|
||
db=db,
|
||
student_id=student_id,
|
||
company_name=company_name,
|
||
min_salary=min_salary,
|
||
max_salary=max_salary
|
||
)
|
||
|
||
return employment_list
|
||
|
||
# 修改就业信息
|
||
@router.put("/modifyEmployment/{student_id}", response_model=EmploymentResponse,summary="修改就业信息")
|
||
async def modify_employment(
|
||
student_id: int,
|
||
employment_data: EmploymentUpdate,
|
||
db: Session = Depends(get_db)
|
||
):
|
||
try:
|
||
employment = update_employment(db=db, student_id=student_id, employment_data=employment_data)
|
||
except ValueError as error:
|
||
raise HTTPException(status_code=422, detail=str(error)) from None
|
||
|
||
if employment is None:
|
||
raise HTTPException(
|
||
status_code=404,
|
||
detail="该学生的就业信息不存在"
|
||
)
|
||
|
||
return employment
|
||
|
||
# 逻辑删除就业信息
|
||
@router.delete("/removeEmployment/{student_id}",summary="逻辑删除就业信息")
|
||
async def remove_employment(
|
||
student_id: int,
|
||
db: Session = Depends(get_db)
|
||
):
|
||
employment = delete_employment(
|
||
db=db,
|
||
student_id=student_id
|
||
)
|
||
|
||
if employment is None:
|
||
raise HTTPException(
|
||
status_code=404,
|
||
detail="该学生的就业信息不存在"
|
||
)
|
||
|
||
return {
|
||
"message": "就业信息删除成功"
|
||
}
|
||
|
||
|
||
|
||
|