AI掘金头条新闻系统 (Toutiao News)-相关推荐

1. crud/news.py

python 复制代码
async def get_related_news(db: AsyncSession, news_id: int, category_id: int, limit: int = 5):
    """
    新闻详情-相关推荐
    """
    stmt = (
        select(News)
        .where(
            News.id != news_id,
            News.category_id == category_id
        )
        .order_by(
            News.views.desc(),
            News.publish_time.desc()
        )
        .limit(limit)
    )

    result = await db.execute(stmt)
    related_news = result.scalars().all()

    return [
        {
            "id": item.id,
            "title": item.title,
            "content": item.content,
            "image": item.image,
            "author": item.author,
            "publishTime": item.publish_time,
            "categoryId": item.category_id,
            "views": item.views
        }
        for item in related_news
    ]

2. routers/news.py

python 复制代码
    # 新闻详情-相关推荐
    related_news = await news.get_related_news(
        db,
        news_detail.id,
        news_detail.category_id
    )

    data = {
        "id": news_detail.id,
        "title": news_detail.title,
        "description": news_detail.description,
        "content": news_detail.content,
        "image": news_detail.image,
        "author": news_detail.author,
        "publishTime": news_detail.publish_time,
        "categoryId": news_detail.category_id,
        "views": news_detail.views + 1,
        "relatedNews": related_news
    }

    return Result.success(data)

完整代码

python 复制代码
@router.get("/detail", response_model=Result)
async def get_news_detail(
        news_id: int = Query(..., alias="id"),
        db: AsyncSession = Depends(get_db)
):
    """
    获取新闻详情、新闻浏览量 + 1、相关推荐
    """

    # 获取新闻详情
    news_detail = await news.get_news_detail(db, news_id)

    if news_detail is None:
        return Result.error(code=404, data="该新闻不存在")

    # 新闻浏览量 + 1
    news_res = await news.increase_new_views(db, news_id)

    if not news_res:
        return Result.error(code=404, data="更新浏览量失败")

    # 新闻详情-相关推荐
    related_news = await news.get_related_news(
        db,
        news_detail.id,
        news_detail.category_id
    )

    data = {
        "id": news_detail.id,
        "title": news_detail.title,
        "description": news_detail.description,
        "content": news_detail.content,
        "image": news_detail.image,
        "author": news_detail.author,
        "publishTime": news_detail.publish_time,
        "categoryId": news_detail.category_id,
        "views": news_detail.views + 1,
        "relatedNews": related_news
    }

    return Result.success(data)
相关推荐
GreenTea5 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
风流 少年5 小时前
Spring AI 2.0:Memory
java·后端·spring
2603_965148115 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
万少6 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端
云和数据.ChenGuang6 小时前
fastapi的参数剖析
人工智能·深度学习·机器学习·语言模型·状态模式·fastapi
jufeng13077 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 8 篇】
python·ai agent·配置系统
circuitsosk7 小时前
跨境电商智能化实战:AI如何赋能客服自动回复、广告智能投放与供应链预测
大数据·人工智能·python·langchain·智能客服
J_bean8 小时前
MySQL 事务是否必须手动开启?
数据库·mysql·数据库事务·自动提交·手动提交
J_bean8 小时前
MySQL InnoDB 如何检测死锁、判定死锁、处理死锁
数据库·mysql·死锁·死锁检测·处理死锁·死锁判定
2601_956319888 小时前
2026年零基础学量化:从看懂示例到写清条件和动作
人工智能·python