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)
相关推荐
障碍的枫子3 分钟前
我的第一个Agent
开发语言·python·langchain
打工仔折腾 AI6 分钟前
260+异构同步链路实战:如何用语义化自动翻译破解CDC DDL同步痛点
人工智能·后端·python·自然语言处理·性能优化·langchain·机器翻译
曲径海盗20 分钟前
【Python 学习第 33 天】条件判断 if/else
python·学习笔记·编程入门
松就是我9029821 分钟前
Agent工具调用实现原理
后端
学习的老炮37 分钟前
反汇编基础速记
后端
曦夜日长38 分钟前
MySQL系统篇,数据库基础(二):数据库概念、SQL 分类与存储引擎
数据库·sql·mysql
步行cgn39 分钟前
@RequestMapping 注解详解
java·后端·spring
骑着蜗牛撵大象32739 分钟前
设备监控数据存与查:Spring Boot 接入 TimescaleDB 超表的完整落地
android·spring boot·后端·springboot·时序数据库·es·设备监控
鶴哥只手遮天40 分钟前
Spring Boot 4.1.1 深度源码解析-第 15 章 · 条件注解体系:全部 `@ConditionalOnXxx` 的实现原理
java·后端·开源
我的div丢了肿么办40 分钟前
go语言安装包详细讲解
后端·go