This commit is contained in:
stardrophere
2026-03-09 18:13:35 +08:00
parent 3c57dd0cce
commit 5b541bbea3
11 changed files with 464 additions and 75 deletions
+45
View File
@@ -0,0 +1,45 @@
# app/api/endpoints/sources.py
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
from app.database import get_db
from app.schemas.schemas import (
InfoSourceCreate, InfoSourceUpdate, InfoSourceResponse, PaginatedResponse
)
from app.crud import crud_source
router = APIRouter()
@router.post("/", response_model=InfoSourceResponse, status_code=status.HTTP_201_CREATED)
async def create_info_source(source_in: InfoSourceCreate, db: Session = Depends(get_db)):
"""新建一个信息源"""
return crud_source.create(db=db, obj_in=source_in)
@router.get("/", response_model=PaginatedResponse[InfoSourceResponse])
async def get_all_sources(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
"""获取所有信息源列表(加入了分页参数)"""
sources = crud_source.get_multi(db=db, skip=skip, limit=limit)
return {"total": len(sources), "data": sources}
@router.get("/{source_id}", response_model=InfoSourceResponse)
async def get_info_source(source_id: int, db: Session = Depends(get_db)):
"""获取单个信息源详情"""
source = crud_source.get(db=db, source_id=source_id)
if not source:
raise HTTPException(status_code=404, detail="该信息源不存在")
return source
@router.patch("/{source_id}", response_model=InfoSourceResponse)
async def update_info_source(source_id: int, source_in: InfoSourceUpdate, db: Session = Depends(get_db)):
"""更新信息源"""
source = crud_source.get(db=db, source_id=source_id)
if not source:
raise HTTPException(status_code=404, detail="该信息源不存在")
# 直接把查出来的数据库对象和前端传来的 Pydantic 对象丢给 CRUD 处理
return crud_source.update(db=db, db_obj=source, obj_in=source_in)