|
|
@ -1,5 +1,6 @@ |
|
|
|
from fastapi import Depends, APIRouter, Query, Path |
|
|
|
from fastapi import Depends, APIRouter, Query, Path,Request |
|
|
|
from internal.models import * |
|
|
|
from datetime import date |
|
|
|
from internal.database import ( |
|
|
|
fetch_one, |
|
|
|
fetch_all, |
|
|
@ -10,13 +11,13 @@ from internal.database import ( |
|
|
|
) |
|
|
|
from dependencies import get_current_active_user |
|
|
|
import json |
|
|
|
from limiter_config import limiter |
|
|
|
router = APIRouter(prefix="/blogs", tags=["博客管理"]) |
|
|
|
|
|
|
|
# 获取列表 |
|
|
|
|
|
|
|
|
|
|
|
@router.get("/list") |
|
|
|
async def blog_list(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 |
|
|
@ -43,7 +44,8 @@ async def blog_list(page: int = Query(None), page_size: int = Query(None)): |
|
|
|
|
|
|
|
|
|
|
|
@router.get("/list/{id}") |
|
|
|
async def blog_one(id: int): |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def blog_one(request:Request,id: int): |
|
|
|
|
|
|
|
# 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表 |
|
|
|
select_query = """ |
|
|
@ -57,7 +59,8 @@ async def blog_one(id: int): |
|
|
|
|
|
|
|
# 博客新增 |
|
|
|
@router.post("/add") |
|
|
|
async def blog_add(blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)): |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def blog_add(request:Request,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 already exists") |
|
|
@ -76,7 +79,8 @@ async def blog_add(blog: Blog, labels: list[Label], _: User = Depends(get_curren |
|
|
|
|
|
|
|
# 博客删除 |
|
|
|
@router.delete("/delete/{id}") |
|
|
|
async def blog_delete(id: str = Path(description="博客id")): |
|
|
|
@limiter.limit("5/minute") |
|
|
|
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") |
|
|
@ -86,7 +90,8 @@ async def blog_delete(id: str = Path(description="博客id")): |
|
|
|
|
|
|
|
|
|
|
|
@router.put("/update/{id}") |
|
|
|
async def blog_update(id: int, blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)): |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def blog_update(request:Request,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,)) |
|
|
@ -112,8 +117,10 @@ async def blog_update(id: int, blog: Blog, labels: list[Label], _: User = Depend |
|
|
|
|
|
|
|
|
|
|
|
# 博客模糊查询 |
|
|
|
@router.get("/list/search") |
|
|
|
@router.get("/search") |
|
|
|
@limiter.limit("5/minute") |
|
|
|
async def blog_list_search( |
|
|
|
request:Request, |
|
|
|
blogtitle: str = Query(None, description="博客标题"), |
|
|
|
typename: str = Query(None, description="博客类型"), |
|
|
|
start_date: str = Query(None, description="开始时间"), |
|
|
@ -146,8 +153,9 @@ async def blog_list_search( |
|
|
|
|
|
|
|
|
|
|
|
# 根据id查询博客 |
|
|
|
@router.get("/list/search/{id}") |
|
|
|
async def get_id_blog(id: str = Path(description="博客id")): |
|
|
|
@router.get("/search/{id}") |
|
|
|
@limiter.limit("5/minute") |
|
|
|
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 |
|
|
@ -166,5 +174,3 @@ async def get_id_blog(id: str = Path(description="博客id")): |
|
|
|
blog_list['labelnames'] = [] |
|
|
|
return response_success(data=blog_list, message="blog search success") |
|
|
|
|
|
|
|
|
|
|
|
# 我就测试一下 |