|
|
@ -1,4 +1,4 @@ |
|
|
|
from fastapi import Depends, APIRouter, Query, Path,Request |
|
|
|
from fastapi import Depends, APIRouter, Query, Path, Request |
|
|
|
from internal.models import * |
|
|
|
from datetime import date |
|
|
|
from internal.database import ( |
|
|
@ -15,16 +15,18 @@ from limiter_config import limiter |
|
|
|
router = APIRouter(prefix="/blogs", tags=["博客管理"]) |
|
|
|
|
|
|
|
# 获取列表 |
|
|
|
|
|
|
|
|
|
|
|
@router.get("/list") |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def blog_list(request: Request,page: int = Query(None), page_size: int = Query(None)): |
|
|
|
# @limiter.limit("5/minute") |
|
|
|
async def blog_list(request: Request, page: int = Query(None), page_size: int = Query(None)): |
|
|
|
limit_clause = "" |
|
|
|
if page is not None and page_size is not None: |
|
|
|
offset = (page - 1) * page_size |
|
|
|
limit_clause = f"LIMIT {page_size} OFFSET {offset}" |
|
|
|
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 |
|
|
|
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 |
|
|
|
FROM blogs |
|
|
|
LEFT JOIN `blogtypes` ON blogs.typeid = blogtypes.id |
|
|
@ -45,7 +47,7 @@ async def blog_list(request: Request,page: int = Query(None), page_size: int = Q |
|
|
|
|
|
|
|
@router.get("/list/{id}") |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def blog_one(request:Request,id: int): |
|
|
|
async def blog_one(request: Request, id: int): |
|
|
|
|
|
|
|
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 |
|
|
|
select_query = """ |
|
|
@ -60,7 +62,7 @@ async def blog_one(request:Request,id: int): |
|
|
|
# 博客新增 |
|
|
|
@router.post("/add") |
|
|
|
@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" |
|
|
|
existing_blog = fetch_one(select_query, (blog.blogtitle,)) |
|
|
|
raise_if_exists(existing_blog, "Blog already exists") |
|
|
@ -80,7 +82,7 @@ async def blog_add(request:Request,blog: Blog, labels: list[Label], _: User = De |
|
|
|
# 博客删除 |
|
|
|
@router.delete("/delete/{id}") |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def blog_delete(request:Request,id: str = Path(description="博客id")): |
|
|
|
async def blog_delete(request: Request, 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") |
|
|
@ -88,10 +90,10 @@ async def blog_delete(request:Request,id: str = Path(description="博客id")): |
|
|
|
execute_query(insert_query, (id,)) |
|
|
|
return response_success(message="blog delete success") |
|
|
|
|
|
|
|
|
|
|
|
# 博客修改 |
|
|
|
@router.put("/update/{id}") |
|
|
|
@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" |
|
|
|
existing_blog = fetch_one(select_query, (id,)) |
|
|
@ -115,12 +117,22 @@ async def blog_update(request:Request,id: int, blog: Blog, labels: list[Label], |
|
|
|
execute_query(insert_label_query, (id, label.id)) |
|
|
|
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") |
|
|
|
@limiter.limit("5/minute") |
|
|
|
# @limiter.limit("5/minute") |
|
|
|
async def blog_list_search( |
|
|
|
request:Request, |
|
|
|
request: Request, |
|
|
|
blogtitle: str = Query(None, description="博客标题"), |
|
|
|
typename: str = Query(None, description="博客类型"), |
|
|
|
start_date: str = Query(None, description="开始时间"), |
|
|
@ -155,7 +167,7 @@ async def blog_list_search( |
|
|
|
# 根据id查询博客 |
|
|
|
@router.get("/search/{id}") |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def get_id_blog(request:Request,id: str = Path(description="博客id")): |
|
|
|
async def get_id_blog(request: Request, id: str = Path(description="博客id")): |
|
|
|
select_query = """SELECT blogs.id, blogtitle, blogcontent,wordcount, blogs.typeid, blogs.descr,JSON_ARRAYAGG(labels.id) AS labelnames,imglink FROM blogs |
|
|
|
LEFT JOIN `blogtypes` ON blogs.typeid = blogtypes.id |
|
|
|
LEFT JOIN blog_label ON blogs.id = blog_label.blogid |
|
|
@ -173,4 +185,3 @@ async def get_id_blog(request:Request,id: str = Path(description="博客id")): |
|
|
|
except json.JSONDecodeError: |
|
|
|
blog_list['labelnames'] = [] |
|
|
|
return response_success(data=blog_list, message="blog search success") |
|
|
|
|