diff --git a/internal/models.py b/internal/models.py index b13b4fa..4ddf81a 100644 --- a/internal/models.py +++ b/internal/models.py @@ -128,6 +128,29 @@ class Classtic(BaseModel): description="备注允许为空" )] +class Comment(BaseModel): + blog_id:Annotated[int,Field( + title="博客id", + default=None, + description="博客id允许为空" + )] + commentname:Annotated[str,Field( + title="评论用户", + description="评论用户不允许为空" + )] + email:Annotated[str,Field( + title="评论用户邮箱", + description="评论用户邮箱不允许为空" + )] + commenturl:Annotated[str,Field( + title="评论地址", + description="评论地址不允许为空" + )] + commentcontent:Annotated[str,Field( + title="评论内容", + description="评论内容不允许为空" + )] + class ComLink(BaseModel): linktext:Annotated[str,Field( title="链接名称", diff --git a/main.py b/main.py index 4270087..5dc42f4 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from slowapi.middleware import SlowAPIMiddleware from slowapi import _rate_limit_exceeded_handler from limiter_config import limiter -from routers import bloglabel, blogtype, usermanage,blogmanage,classticmanage,commonlinkmanage,diarymanage,diarytype,statistic,disbursemanage,photomanage +from routers import bloglabel, blogtype, usermanage,blogmanage,classticmanage,commonlinkmanage,diarymanage,diarytype,statistic,disbursemanage,photomanage,commentmanage app=FastAPI() app.state.limiter = limiter app.add_exception_handler(429, _rate_limit_exceeded_handler) @@ -25,6 +25,7 @@ app.include_router(bloglabel.router) app.include_router(statistic.router) app.include_router(disbursemanage.router) app.include_router(photomanage.router) +app.include_router(commentmanage.router) # 解决跨域 app.add_middleware( CORSMiddleware, diff --git a/routers/commentmanage.py b/routers/commentmanage.py new file mode 100644 index 0000000..5e53708 --- /dev/null +++ b/routers/commentmanage.py @@ -0,0 +1,68 @@ +from fastapi import Depends, APIRouter, status, Query, Path, HTTPException,Request +from internal.models import * +from internal.database import fetch_one, fetch_all, execute_query, response_success, raise_if_exists,raise_if_not_found +from dependencies import get_current_active_user +from limiter_config import limiter +router = APIRouter( + prefix="/comments", + tags=['评论管理'] +) +# 获取列表 +@router.get("/list") +@limiter.limit("10/minute") +async def comment_list(request: Request,): + select_query = "SELECT id,blog_id,commentname,email,commenturl,commentcontent,create_at FROM comments;" + comment_list = fetch_all(select_query) + return response_success(comment_list, "comment get list success") + +# 新增 +@router.post("/add") +@limiter.limit("10/minute") +async def comment_add(request: Request,comment:Comment): + insert_query="""INSERT INTO comments (blog_id,commentname,email,commenturl,commentcontent) VALUES(%s,%s,%s,%s)""" + insert_value=(comment.blog_id,comment.commentname,comment.email,comment.commenturl,comment.commentcontent) + execute_query(insert_query,insert_value) + return response_success(data=comment,message="comment create success") + +# # 单条数据查询 +# @router.get("/list/search") +# @limiter.limit("10/minute") +# async def comment_search(request: Request,header:str=Query(description="评论标题")): +# select_query="SELECT id,header,text,descr FROM comments WHERE 1=1 " +# params=[] +# if header: +# select_query+="AND header LIKE %s" +# params.append(f"%{header}%") +# comment_query=fetch_all(select_query,params=params,fetchall=True) +# return response_success(data=comment_query,message="comment search success") + +# # 评论修改 +# @router.put("/update/{id}") +# @limiter.limit("10/minute") +# async def comment_put(request: Request,comment:comment,id: str = Path(description="评论id"),_: User = Depends(get_current_active_user)): +# update_query = ( +# "UPDATE comments SET header=%s,text=%s,descr=%s WHERE id=%s;" +# ) +# update_data = (comment.header, comment.text, +# comment.descr,id) +# execute_query(update_query, update_data) +# return response_success("comment update sucess") + +# 评论删除 +@router.delete("/delete/{id}") +@limiter.limit("10/minute") +async def comment_del(request: Request,id: str = Path(description="评论id")): + update_query = ( + "DELETE FROM comments WHERE id=%s;" + ) + update_data = (id) + execute_query(update_query, update_data) + return response_success() + +# # 根据id查询评论 +# @router.get("/list/search/{id}") +# @limiter.limit("10/minute") +# async def comment_search_id(request: Request,id:str=Path(description="评论id")): +# select_query="SELECT * FROM comments WHERE id=%s" +# comment_query=fetch_one(select_query,(id,)) +# return response_success(data=comment_query,message="comment search success") \ No newline at end of file