You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
3.4 KiB
85 lines
3.4 KiB
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.post("/add")
|
|
@limiter.limit("1/minute")
|
|
async def comment_add(request: Request,comment:Comment):
|
|
insert_query="""INSERT INTO comments (parent_id,blog_id,commentname,email,commenturl,commentcontent) VALUES(%s,%s,%s,%s,%s,%s)"""
|
|
insert_value=(comment.parent_id,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")
|
|
|
|
|
|
def build_comment_tree(comments: list[dict[str, any]], parent_id: int = None) -> list[dict[str, any]]:
|
|
tree = []
|
|
for comment in comments:
|
|
if comment['parent_id'] == parent_id:
|
|
children = build_comment_tree(comments, comment['id'])
|
|
if children:
|
|
comment['children'] = children
|
|
tree.append(comment)
|
|
return tree
|
|
|
|
@router.get("/list")
|
|
@limiter.limit("10/minute")
|
|
async def comment_list(request: Request, blog_id: int = Query(..., description="ID of the blog to filter comments")):
|
|
select_query = """
|
|
SELECT id, parent_id, blog_id, commentname, email, commenturl, commentcontent, create_at
|
|
FROM comments
|
|
WHERE blog_id = %s;
|
|
"""
|
|
comments = fetch_all(select_query, (blog_id,))
|
|
comment_tree = build_comment_tree(comments)
|
|
return response_success(comment_tree, "comment get list 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")
|