From cfcfc95040b658088673bd536a37ae3346d483fb Mon Sep 17 00:00:00 2001 From: panda <7934952@qq.com> Date: Fri, 5 Jul 2024 17:04:17 +0800 Subject: [PATCH] add news --- routers/blogmanage.py | 47 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/routers/blogmanage.py b/routers/blogmanage.py index b26e54d..7ddfb57 100644 --- a/routers/blogmanage.py +++ b/routers/blogmanage.py @@ -19,7 +19,7 @@ router = APIRouter(prefix="/blogs", tags=["博客管理"]) async def blog_list(): # 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 select_query = """ - SELECT blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, + SELECT blogs.id,blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, blogs.readminite, blogs.readnum, blogs.wordcount, types.typename, JSON_ARRAYAGG(labels.labelname) AS labelnames FROM blogs @@ -27,7 +27,7 @@ LEFT JOIN `types` ON blogs.typeid = types.id LEFT JOIN blog_label ON blog_label.blogid = blogs.id LEFT JOIN labels ON blog_label.labelid = labels.id -GROUP BY blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, +GROUP BY blogs.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, blogs.readminite, blogs.readnum, blogs.wordcount, types.typename ORDER BY create_at DESC; @@ -66,17 +66,41 @@ async def blog_delete(id: str = Path(description="博客id")): # 博客修改 +# @router.put("/update/{id}") +# async def blog_put(blog: Blog, id: str = Path(description="博客id")): +# select_query = "SELECT * FROM blogs WHERE id=%s" +# existing_blog = fetch_one(select_query, (id,)) +# raise_if_not_found(existing_blog, "blog not found") +# update_query = ( +# "UPDATE blogs SET blogtitle=%s,blogcontent=%s,typeid=%s,descr=%s WHERE id=%s;" +# ) +# update_data = (blog.blogtitle, blog.blogcontent, +# blog.typeid, blog.descr, id) +# execute_query(update_query, update_data) +# return response_success("blog update sucess") + @router.put("/update/{id}") -async def blog_put(blog: Blog, id: str = Path(description="博客id")): - select_query = "SELECT * FROM blogs WHERE id=%s" +async def blog_update(id: int, blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)): + # 检查要编辑的博客是否存在 + select_query = "SELECT * FROM blogs WHERE id = %s" existing_blog = fetch_one(select_query, (id,)) raise_if_not_found(existing_blog, "blog not found") + + # 更新博客信息 update_query = ( - "UPDATE blogs SET blogtitle=%s,blogcontent=%s,typeid=%s,descr=%s WHERE id=%s;" + "UPDATE blogs SET blogtitle = %s, blogcontent = %s, imglink = %s, typeid = %s, descr = %s WHERE id = %s" ) - update_data = (blog.blogtitle, blog.blogcontent, - blog.typeid, blog.descr, id) + update_data = (blog.blogtitle, blog.blogcontent, blog.imglink, blog.typeid, blog.descr, id) execute_query(update_query, update_data) + + # 首先删除原有的关联标签 + delete_query = "DELETE FROM blog_label WHERE blogid = %s" + execute_query(delete_query, (id,)) + + # 然后插入新的关联标签 + for label in labels: + insert_label_query = "INSERT INTO blog_label (blogid, labelid) VALUES (%s, %s)" + execute_query(insert_label_query, (id, label.id)) return response_success("blog update sucess") @@ -117,7 +141,14 @@ async def blog_list_search( # 根据id查询博客 @router.get("/list/search/{id}") async def get_id_blog(id: str = Path(description="博客id")): - select_query = "SELECT * FROM blogs WHERE id=%s" + select_query = """SELECT blogs.id, blogtitle, blogcontent,wordcount, blogs.typeid, blogs.descr,JSON_ARRAYAGG(labels.labelname) AS labelnames,imglink FROM blogs + LEFT JOIN `types` ON blogs.typeid = types.id + LEFT JOIN blog_label ON blogs.id = blog_label.blogid + LEFT JOIN labels ON blog_label.labelid = labels.id + WHERE blogs.id = %s + GROUP BY blogs.id; + +""" blog_list = execute_query(select_query, (id,)) return response_success(data=blog_list, message="blog search success")