model consultant.py已更新完毕
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
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=['顾问管理模块']
|
||||
)
|
||||
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=['顾问管理模块'])
|
||||
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=['顾问管理模块'])
|
||||
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=['顾问管理模块']
|
||||
)
|
||||
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=['顾问管理模块'])
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user