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)
相关推荐
独泪了无痕6 小时前
MyBatis魔法堂:结果集映射
后端·mybatis
大貔貅喝啤酒7 小时前
Python Requests库教程
自动化测试·python·requests库
copyer_xyf7 小时前
LangChain 调用 LLM
后端·python·agent
copyer_xyf7 小时前
Prompt 组织管理
后端·python·agent
北顾笙9807 小时前
MySQL-day2
数据库·mysql
shimly1234568 小时前
python3 uvicorn 是啥?
python
CTA量化套保9 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
摇滚侠9 小时前
SpringMVC 入门到实战 文件上传 75-77
java·后端·spring·maven·intellij-idea
程序猿乐锅9 小时前
【MySQL | 第八篇】MySQL 视图
数据库·mysql
GIS数据转换器9 小时前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机