56 lines
2.4 KiB
Python
56 lines
2.4 KiB
Python
from typing import List
|
|||
|
|
from fastapi import APIRouter, Depends, HTTPException
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
from dao import advisor_dao
|
||
|
|
from database import get_db
|
||
|
|
from schema import advisor_schema
|
||
|
|
|
||
|
|
#子路由
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
#查所有
|
||
|
|
@router.get("/getAllAdvisors", response_model=List[advisor_schema.AdvisorOut],summary="查所有顾问")
|
||
|
|
def get_all_advisors(db: Session = Depends(get_db)):
|
||
|
|
return advisor_dao.get_all(db)
|
||
|
|
|
||
|
|
#查单个
|
||
|
|
@router.get("/getAdvisor/{advisor_id}",response_model=advisor_schema.AdvisorOut,summary="查单个顾问")
|
||
|
|
def get_advisor(advisor_id:int,db:Session =Depends(get_db)):
|
||
|
|
advisor=advisor_dao.get_by_id(db,advisor_id)
|
||
|
|
if advisor:
|
||
|
|
return advisor
|
||
|
|
else:
|
||
|
|
raise HTTPException(status_code=404,detail="顾问不存在")
|
||
|
|
|
||
|
|
|
||
|
|
#查指定顾问下的学生信息
|
||
|
|
@router.get("/advisors/{advisor_id}/students", response_model=List[advisor_schema.StudentOut],summary="查指定顾问下的学生信息")
|
||
|
|
def get_advisor_students(advisor_id: int, db: Session = Depends(get_db)):
|
||
|
|
students = advisor_dao.adv_all_stu(db, advisor_id)
|
||
|
|
if students is None:
|
||
|
|
raise HTTPException(status_code=404, detail="顾问不存在")
|
||
|
|
return students # 空列表 [] 会正常返回 200
|
||
|
|
#增加
|
||
|
|
@router.post("/createAdvisor", response_model=advisor_schema.AdvisorOut,summary="增加顾问")
|
||
|
|
def create_advisor(advisor: advisor_schema.AdvisorCreate, db: Session = Depends(get_db)):
|
||
|
|
return advisor_dao.create_advisor(db, advisor.advisor_name, advisor.phone, advisor.gender)
|
||
|
|
|
||
|
|
#删除/恢复
|
||
|
|
@router.put("/changeAdvisorStatus/{advisor_id}/status",response_model=advisor_schema.AdvisorOut,summary="删除顾问")
|
||
|
|
def change_advisor_status(advisor_id: int,
|
||
|
|
body: advisor_schema.AdvisorStatus,
|
||
|
|
db: Session = Depends(get_db)):
|
||
|
|
advisor = advisor_dao.change_advisor(db, advisor_id, body.flag)
|
||
|
|
if advisor is None:
|
||
|
|
raise HTTPException(status_code=404, detail="顾问不存在")
|
||
|
|
return advisor
|
||
|
|
|
||
|
|
#修改
|
||
|
|
@router.put("/updateAdvisor/{advisor_id}", response_model=advisor_schema.AdvisorOut,summary="修改顾问")
|
||
|
|
def update_advisor(advisor_id: int,
|
||
|
|
advisor: advisor_schema.AdvisorCreate,
|
||
|
|
db: Session = Depends(get_db)):
|
||
|
|
advisors = advisor_dao.update_advisor(db, advisor_id, advisor.advisor_name, advisor.phone, advisor.gender)
|
||
|
|
if advisors is None:
|
||
|
|
raise HTTPException(status_code=404, detail="顾问不存在")
|
||
|
|
return advisors
|