@ -9,7 +9,7 @@ from internal.database import (
raise_if_not_found ,
raise_if_not_found ,
)
)
from dependencies import get_current_active_user
from dependencies import get_current_active_user
import json
router = APIRouter ( prefix = " /blogs " , tags = [ " 博客管理 " ] )
router = APIRouter ( prefix = " /blogs " , tags = [ " 博客管理 " ] )
# 获取列表
# 获取列表
@ -17,20 +17,17 @@ router = APIRouter(prefix="/blogs", tags=["博客管理"])
@router.get ( " /list " )
@router.get ( " /list " )
async def blog_list ( ) :
async def blog_list ( ) :
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表
select_query = """
select_query = """
SELECT blogs . id , 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
blogs . wordcount , types . typename , JSON_ARRAYAGG ( labels . labelname ) AS labelnames
FROM blogs
FROM blogs
LEFT JOIN `types` ON blogs . typeid = types . id
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 . id , 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 . wordcount , types . typename ORDER BY create_at DESC ;
"""
"""
blog_list = fetch_all ( select_query )
blog_list = fetch_all ( select_query )
return response_success ( blog_list , " blog get list success " )
return response_success ( blog_list , " blog get list success " )
@ -45,7 +42,8 @@ async def blog_add(blog: Blog,labels: list[Label], _: User = Depends(get_current
insert_query = (
insert_query = (
" INSERT INTO blogs (blogtitle, blogcontent,imglink, typeid, descr) VALUES ( %s , %s , %s , %s , %s ) "
" 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 )
blog_id = execute_query ( insert_query , insert_data , lastrowid = True )
for label in labels :
for label in labels :
insert_label_query = " INSERT INTO blog_label (blogid, labelid) VALUES ( %s , %s ) "
insert_label_query = " INSERT INTO blog_label (blogid, labelid) VALUES ( %s , %s ) "
@ -90,7 +88,8 @@ async def blog_update(id: int, blog: Blog, labels: list[Label], _: User = Depend
update_query = (
update_query = (
" UPDATE blogs SET blogtitle = %s , blogcontent = %s , imglink = %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 . 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 )
execute_query ( update_query , update_data )
# 首先删除原有的关联标签
# 首先删除原有的关联标签
@ -141,7 +140,7 @@ 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 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 `types` ON blogs . typeid = types . id
LEFT JOIN blog_label ON blogs . id = blog_label . blogid
LEFT JOIN blog_label ON blogs . id = blog_label . blogid
LEFT JOIN labels ON blog_label . labelid = labels . id
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 , ) )
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 " )
return response_success ( data = blog_list , message = " blog search success " )