Browse Source

add news

master
panda 8 months ago
parent
commit
cfcfc95040
  1. 47
      routers/blogmanage.py

47
routers/blogmanage.py

@ -19,7 +19,7 @@ router = APIRouter(prefix="/blogs", tags=["博客管理"])
async def blog_list(): async def blog_list():
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 # 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表
select_query = """ 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, blogs.readminite, blogs.readnum, blogs.wordcount, types.typename,
JSON_ARRAYAGG(labels.labelname) AS labelnames JSON_ARRAYAGG(labels.labelname) AS labelnames
FROM blogs 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 blog_label ON blog_label.blogid = blogs.id
LEFT JOIN labels ON blog_label.labelid = labels.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; 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}") @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,)) existing_blog = fetch_one(select_query, (id,))
raise_if_not_found(existing_blog, "blog not found") raise_if_not_found(existing_blog, "blog not found")
# 更新博客信息
update_query = ( 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) 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") return response_success("blog update sucess")
@ -117,7 +141,14 @@ async def blog_list_search(
# 根据id查询博客 # 根据id查询博客
@router.get("/list/search/{id}") @router.get("/list/search/{id}")
async def get_id_blog(id: str = Path(description="博客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,)) blog_list = execute_query(select_query, (id,))
return response_success(data=blog_list, message="blog search success") return response_success(data=blog_list, message="blog search success")

Loading…
Cancel
Save