cm-wisdom-cube/backend/app/routers/developer.py
2025-03-14 16:57:26 +08:00

103 lines
3.6 KiB
Python

from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from pydantic import BaseModel
from app.core.database import get_db
from app.models.developer import Developer
from app.utils.common_util import get_total_page, get_offset, SuccessListPage
from app.core.security import get_current_user
router = APIRouter(prefix="/developer", tags=["developer"])
class DeveloperSave(BaseModel):
id: int = 0
name: str
profile: str
post: str
@router.post("/save")
async def save_developer(developer: DeveloperSave,
db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)):
params = Developer(
name=developer.name,
profile=developer.profile,
post=developer.post,
gmt_create=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
)
try:
db.add(params)
db.commit()
except Exception as e:
db.rollback()
raise HTTPException(status_code=400, detail=str(e))
return {"status": "success"}
@router.delete("/delete")
async def delete_developer(ids: str = Query(..., description="删除ID列表", min_length=1),
db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)):
try:
id_list = ids.split(",")
db.query(Developer).filter(Developer.id.in_(id_list)).delete()
db.commit()
except Exception as e:
db.rollback()
raise HTTPException(status_code=400, detail=str(e))
return {"status": "success"}
@router.put("/update/{developer_id}")
async def update_developer(developer_id: int,
developer: DeveloperSave,
db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)):
params = db.query(Developer).filter(Developer.id == developer_id).first()
if not params:
raise HTTPException(status_code=404, detail="数据不存在")
params.name = developer.name
params.profile = developer.profile
params.post = developer.post
try:
db.commit()
except Exception as e:
db.rollback()
raise HTTPException(status_code=400, detail=str(e))
return {"status": "success"}
@router.get("/get/{developer_id}")
async def get_developer(developer_id: int,
db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)):
developer = db.query(Developer).filter(Developer.id == developer_id).first()
if not developer:
raise HTTPException(status_code=404, detail="数据不存在")
return developer
@router.get("/list")
async def list_developer(db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)):
return db.query(Developer).all()
@router.get("/listpage")
async def get_developer_listpage(db: Session = Depends(get_db),
current_user: str = Depends(get_current_user),
page: int = Query(1, gt=0, description="当前页面"),
page_size: int = Query(10, gt=0, le=100, description="每页记录数"),
name: str = Query(None, description="姓名")):
query = db.query(Developer)
if name:
query = query.filter(Developer.name.ilike(f"%{name}%"))
total = query.count()
total_page = get_total_page(total, page_size)
offset = get_offset(page, page_size)
data_list = query.offset(offset).limit(page_size).all()
return SuccessListPage(page, total, total_page, data_list)