|
|
@ -19,7 +19,7 @@ router = APIRouter(prefix="/blogs", tags=["博客管理"]) |
|
|
|
async def blog_list(): |
|
|
|
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 |
|
|
|
select_query = """ |
|
|
|
SELECT blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.img, |
|
|
|
SELECT 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.img, |
|
|
|
GROUP BY blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, |
|
|
|
blogs.readminite, blogs.readnum, blogs.wordcount, types.typename ORDER BY create_at DESC; |
|
|
|
|
|
|
|
|
|
|
@ -38,16 +38,20 @@ GROUP BY blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.img, |
|
|
|
|
|
|
|
# 博客新增 |
|
|
|
@router.post("/add") |
|
|
|
async def blog_add(blog: Blog, _: 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 is already exists") |
|
|
|
raise_if_exists(existing_blog, "Blog already exists") |
|
|
|
insert_query = ( |
|
|
|
"INSERT INTO blogs (blogtitle,blogcontent,typeid,descr) VALUES (%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.typeid, blog.descr) |
|
|
|
execute_query(insert_query, insert_data) |
|
|
|
return response_success(data=blog, message="blog create success") |
|
|
|
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)) |
|
|
|
|
|
|
|
return {"message": "Blog created successfully", "blog_id": blog_id} |
|
|
|
|
|
|
|
|
|
|
|
# 博客删除 |
|
|
|