"백엔드_핵심_로직_구현_프론트엔드_연동_및_도커_배포_최적화_완료"
This commit is contained in:
29
backend/app/api/endpoints/news.py
Normal file
29
backend/app/api/endpoints/news.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from typing import List
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.db.database import get_db
|
||||
from app.db.models import NewsCache
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class NewsItemSchema(BaseModel):
|
||||
news_id: str
|
||||
title: str
|
||||
description: str
|
||||
pubDate: str
|
||||
sentiment: str | None
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@router.get("/", response_model=List[NewsItemSchema])
|
||||
async def get_news(query: str = None, limit: int = 50, db: AsyncSession = Depends(get_db)):
|
||||
stmt = select(NewsCache).order_by(NewsCache.pubDate.desc()).limit(limit)
|
||||
if query:
|
||||
stmt = stmt.where(NewsCache.title.like(f"%{query}%"))
|
||||
|
||||
result = await db.execute(stmt)
|
||||
news = result.scalars().all()
|
||||
return news
|
||||
Reference in New Issue
Block a user