diff --git a/routers/blogmanage.py b/routers/blogmanage.py index 7ddfb57..3f189f6 100644 --- a/routers/blogmanage.py +++ b/routers/blogmanage.py @@ -9,7 +9,7 @@ from internal.database import ( raise_if_not_found, ) from dependencies import get_current_active_user - +import json router = APIRouter(prefix="/blogs", tags=["博客管理"]) # 获取列表 @@ -17,20 +17,17 @@ router = APIRouter(prefix="/blogs", tags=["博客管理"]) @router.get("/list") async def blog_list(): + # 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 select_query = """ - 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 -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.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, - blogs.readminite, blogs.readnum, blogs.wordcount, types.typename ORDER BY create_at DESC; - - + SELECT blogs.id,blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, + blogs.wordcount, types.typename,JSON_ARRAYAGG(labels.labelname) AS labelnames + FROM blogs + 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.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, + blogs.wordcount, types.typename ORDER BY create_at DESC; """ blog_list = fetch_all(select_query) return response_success(blog_list, "blog get list success") @@ -38,18 +35,19 @@ GROUP BY blogs.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.im # 博客新增 @router.post("/add") -async def blog_add(blog: Blog,labels: list[Label], _: User = Depends(get_current_active_user)): +async def blog_add(blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)): select_query = "SELECT * FROM blogs WHERE blogtitle = %s" existing_blog = fetch_one(select_query, (blog.blogtitle,)) raise_if_exists(existing_blog, "Blog already exists") insert_query = ( "INSERT INTO blogs (blogtitle, blogcontent,imglink, typeid, descr) VALUES (%s, %s, %s, %s,%s)" ) - insert_data = (blog.blogtitle, blog.blogcontent,blog.imglink,blog.typeid, blog.descr) + insert_data = (blog.blogtitle, blog.blogcontent, + blog.imglink, blog.typeid, blog.descr) blog_id = execute_query(insert_query, insert_data, lastrowid=True) for label in labels: - insert_label_query="INSERT INTO blog_label (blogid, labelid) VALUES (%s, %s)" - execute_query(insert_label_query,(blog_id,label.id)) + insert_label_query = "INSERT INTO blog_label (blogid, labelid) VALUES (%s, %s)" + execute_query(insert_label_query, (blog_id, label.id)) return {"message": "Blog created successfully", "blog_id": blog_id} @@ -90,13 +88,14 @@ async def blog_update(id: int, blog: Blog, labels: list[Label], _: User = Depend update_query = ( "UPDATE blogs SET blogtitle = %s, blogcontent = %s, imglink = %s, typeid = %s, descr = %s WHERE id = %s" ) - update_data = (blog.blogtitle, blog.blogcontent, blog.imglink, 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)" @@ -141,7 +140,7 @@ async def blog_list_search( # 根据id查询博客 @router.get("/list/search/{id}") async def get_id_blog(id: str = Path(description="博客id")): - select_query = """SELECT blogs.id, blogtitle, blogcontent,wordcount, blogs.typeid, blogs.descr,JSON_ARRAYAGG(labels.labelname) AS labelnames,imglink FROM blogs + select_query = """SELECT blogs.id, blogtitle, blogcontent,wordcount, blogs.typeid, blogs.descr,JSON_ARRAYAGG(labels.id) 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 @@ -150,6 +149,13 @@ async def get_id_blog(id: str = Path(description="博客id")): """ blog_list = execute_query(select_query, (id,)) + + if blog_list and isinstance(blog_list, dict): + if 'labelnames' in blog_list and isinstance(blog_list['labelnames'], str): + try: + blog_list['labelnames'] = json.loads(blog_list['labelnames']) + except json.JSONDecodeError: + blog_list['labelnames'] = [] return response_success(data=blog_list, message="blog search success")