|
@ -15,8 +15,10 @@ from limiter_config import limiter |
|
|
router = APIRouter(prefix="/blogs", tags=["博客管理"]) |
|
|
router = APIRouter(prefix="/blogs", tags=["博客管理"]) |
|
|
|
|
|
|
|
|
# 获取列表 |
|
|
# 获取列表 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/list") |
|
|
@router.get("/list") |
|
|
@limiter.limit("5/minute") |
|
|
|
|
|
|
|
|
# @limiter.limit("5/minute") |
|
|
async def blog_list(request: Request, page: int = Query(None), page_size: int = Query(None)): |
|
|
async def blog_list(request: Request, page: int = Query(None), page_size: int = Query(None)): |
|
|
limit_clause = "" |
|
|
limit_clause = "" |
|
|
if page is not None and page_size is not None: |
|
|
if page is not None and page_size is not None: |
|
@ -24,7 +26,7 @@ async def blog_list(request: Request,page: int = Query(None), page_size: int = Q |
|
|
limit_clause = f"LIMIT {page_size} OFFSET {offset}" |
|
|
limit_clause = f"LIMIT {page_size} OFFSET {offset}" |
|
|
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 |
|
|
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 |
|
|
select_query = f""" |
|
|
select_query = f""" |
|
|
SELECT blogs.id,blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink, |
|
|
|
|
|
|
|
|
SELECT blogs.id,blogs.blogtitle, blogs.blogcontent,blogs.readnum, blogs.create_at, blogs.imglink, |
|
|
blogs.wordcount, blogtypes.typename,JSON_ARRAYAGG(labels.labelname) AS labelnames |
|
|
blogs.wordcount, blogtypes.typename,JSON_ARRAYAGG(labels.labelname) AS labelnames |
|
|
FROM blogs |
|
|
FROM blogs |
|
|
LEFT JOIN `blogtypes` ON blogs.typeid = blogtypes.id |
|
|
LEFT JOIN `blogtypes` ON blogs.typeid = blogtypes.id |
|
@ -60,7 +62,7 @@ async def blog_one(request:Request,id: int): |
|
|
# 博客新增 |
|
|
# 博客新增 |
|
|
@router.post("/add") |
|
|
@router.post("/add") |
|
|
@limiter.limit("5/minute") |
|
|
@limiter.limit("5/minute") |
|
|
async def blog_add(request:Request,blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)): |
|
|
|
|
|
|
|
|
async def blog_add(request: Request, blog: Blog, labels: list[MoreLable], _: User = Depends(get_current_active_user)): |
|
|
select_query = "SELECT * FROM blogs WHERE blogtitle = %s" |
|
|
select_query = "SELECT * FROM blogs WHERE blogtitle = %s" |
|
|
existing_blog = fetch_one(select_query, (blog.blogtitle,)) |
|
|
existing_blog = fetch_one(select_query, (blog.blogtitle,)) |
|
|
raise_if_exists(existing_blog, "Blog already exists") |
|
|
raise_if_exists(existing_blog, "Blog already exists") |
|
@ -88,10 +90,10 @@ async def blog_delete(request:Request,id: str = Path(description="博客id")): |
|
|
execute_query(insert_query, (id,)) |
|
|
execute_query(insert_query, (id,)) |
|
|
return response_success(message="blog delete success") |
|
|
return response_success(message="blog delete success") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 博客修改 |
|
|
@router.put("/update/{id}") |
|
|
@router.put("/update/{id}") |
|
|
@limiter.limit("5/minute") |
|
|
@limiter.limit("5/minute") |
|
|
async def blog_update(request:Request,id: int, blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)): |
|
|
|
|
|
|
|
|
async def blog_update(request: Request, id: int, blog: Blog, labels: list[MoreLable], _: User = Depends(get_current_active_user)): |
|
|
# 检查要编辑的博客是否存在 |
|
|
# 检查要编辑的博客是否存在 |
|
|
select_query = "SELECT * FROM blogs WHERE id = %s" |
|
|
select_query = "SELECT * FROM blogs WHERE id = %s" |
|
|
existing_blog = fetch_one(select_query, (id,)) |
|
|
existing_blog = fetch_one(select_query, (id,)) |
|
@ -115,10 +117,20 @@ async def blog_update(request:Request,id: int, blog: Blog, labels: list[Label], |
|
|
execute_query(insert_label_query, (id, label.id)) |
|
|
execute_query(insert_label_query, (id, label.id)) |
|
|
return response_success("blog update sucess") |
|
|
return response_success("blog update sucess") |
|
|
|
|
|
|
|
|
|
|
|
# 修改次数 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/update/{id}/readnum") |
|
|
|
|
|
@limiter.limit("5/minute") |
|
|
|
|
|
async def blog_update_num(request: Request, id: int): |
|
|
|
|
|
update_query ="UPDATE blogs SET readnum = readnum + 1 WHERE id = %s" |
|
|
|
|
|
execute_query(update_query,(id,)) |
|
|
|
|
|
return response_success("blog update sucess") |
|
|
# 博客模糊查询 |
|
|
# 博客模糊查询 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/search") |
|
|
@router.get("/search") |
|
|
@limiter.limit("5/minute") |
|
|
|
|
|
|
|
|
# @limiter.limit("5/minute") |
|
|
async def blog_list_search( |
|
|
async def blog_list_search( |
|
|
request: Request, |
|
|
request: Request, |
|
|
blogtitle: str = Query(None, description="博客标题"), |
|
|
blogtitle: str = Query(None, description="博客标题"), |
|
@ -173,4 +185,3 @@ async def get_id_blog(request:Request,id: str = Path(description="博客id")): |
|
|
except json.JSONDecodeError: |
|
|
except json.JSONDecodeError: |
|
|
blog_list['labelnames'] = [] |
|
|
blog_list['labelnames'] = [] |
|
|
return response_success(data=blog_list, message="blog search success") |
|
|
return response_success(data=blog_list, message="blog search success") |
|
|
|
|
|
|