From ef9c74a237ade3783b795580c4d2de3d7453c682 Mon Sep 17 00:00:00 2001 From: panda <7934952@qq.com> Date: Thu, 11 Jul 2024 17:11:03 +0800 Subject: [PATCH] add new --- routers/blogmanage.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/routers/blogmanage.py b/routers/blogmanage.py index 978c811..8a96c36 100644 --- a/routers/blogmanage.py +++ b/routers/blogmanage.py @@ -13,13 +13,14 @@ import json router = APIRouter(prefix="/blogs", tags=["博客管理"]) # 获取列表 - - @router.get("/list") -async def blog_list(): - +async def blog_list(page: int = Query(None), page_size: int = Query(None)): + limit_clause = "" + if page is not None and page_size is not None: + offset = (page - 1) * page_size + limit_clause = f"LIMIT {page_size} OFFSET {offset}" # 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 - select_query = """ + select_query = f""" SELECT blogs.id,blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, blogs.wordcount, types.typename,JSON_ARRAYAGG(labels.labelname) AS labelnames FROM blogs @@ -27,10 +28,17 @@ async def blog_list(): LEFT JOIN blog_label ON blog_label.blogid = blogs.id LEFT JOIN labels ON blog_label.labelid = labels.id GROUP BY blogs.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, - blogs.wordcount, types.typename ORDER BY create_at DESC; + blogs.wordcount, types.typename ORDER BY create_at DESC + {limit_clause}; """ blog_list = fetch_all(select_query) - return response_success(blog_list, "blog get list success") + count_query = "SELECT COUNT(*) AS total FROM blogs;" + total_records = fetch_one(count_query)["total"] + return response_success({ + "blogs": blog_list, + "total": total_records, + }, "blog get list success") + @router.get("/list/{id}") async def blog_one(id: int): @@ -41,7 +49,7 @@ async def blog_one(id: int): WHERE id = %s ORDER BY create_at DESC; """ - blog_one = fetch_one(select_query,(id,)) + blog_one = fetch_one(select_query, (id,)) return response_success(blog_one, "blog get blog_one success") @@ -74,6 +82,7 @@ async def blog_delete(id: str = Path(description="博客id")): execute_query(insert_query, (id,)) return response_success(message="blog delete success") + @router.put("/update/{id}") async def blog_update(id: int, blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)): # 检查要编辑的博客是否存在