Files

64 lines
2.1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from core.database import get_db
from schemas.consultant import ConsultantCreate, ConsultantUpdate, ConsultantResponse
from dao import consultant as dao_consultant
router = APIRouter()
@router.get(
path="/consultants",
tags=['顾问管理模块'],summary='顾问模糊分页查询')
def read_consultants(
db: Session = Depends(get_db),
page:int=1,
page_size:int=20,
name:str|None=None,
phone:str|None=None
):
return dao_consultant.get_consultant_list(db, page, page_size, name, phone)
@router.post("/consultants", response_model=ConsultantResponse,tags=['顾问管理模块'],summary='顾问增加')
def create_consultant(
obj_in: ConsultantCreate,
db: Session = Depends(get_db)
):
return dao_consultant.create_consultant(db, obj_in)
@router.get("/consultants/{id}", response_model=ConsultantResponse,tags=['顾问管理模块'],summary='顾问查询')
def read_consultant(id: int, db: Session = Depends(get_db)):
res = dao_consultant.get_consultant_by_id(db, cid=id)
if res is None:
raise HTTPException(status_code=404, detail="顾问不存在或已删除")
return res
@router.put(
path="/consultants/{id}",
response_model=ConsultantResponse,
tags=['顾问管理模块'],summary='顾问更新')
def update_consultant(
id: int,
obj_in: ConsultantUpdate,
db: Session = Depends(get_db)
):
# model_dump 排除为None的字段,只更新前端传过来的字段
update_dict = obj_in.model_dump(exclude_none=True)
res = dao_consultant.update_consultant(db, cid=id, update_data=update_dict)
if res is None:
raise HTTPException(status_code=404, detail="顾问不存在或已删除")
return res
@router.delete("/consultants/{id}", response_model=ConsultantResponse,tags=['顾问管理模块'],summary='顾问删除')
def delete_consultant(id: int, db: Session = Depends(get_db)):
res = dao_consultant.delete_consultant(db, cid=id)
if res is None:
raise HTTPException(status_code=404, detail="顾问不存在或已删除")
return res