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.

84 lines
3.4 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. from fastapi import Depends, APIRouter, status, Query, Path, HTTPException,Request
  2. from internal.models import *
  3. from internal.database import fetch_one, fetch_all, execute_query, response_success, raise_if_exists,raise_if_not_found
  4. from dependencies import get_current_active_user
  5. from limiter_config import limiter
  6. router = APIRouter(
  7. prefix="/comments",
  8. tags=['评论管理']
  9. )
  10. # 新增
  11. @router.post("/add")
  12. @limiter.limit("1/minute")
  13. async def comment_add(request: Request,comment:Comment):
  14. insert_query="""INSERT INTO comments (parent_id,blog_id,commentname,email,commenturl,commentcontent) VALUES(%s,%s,%s,%s,%s,%s)"""
  15. insert_value=(comment.parent_id,comment.blog_id,comment.commentname,comment.email,comment.commenturl,comment.commentcontent)
  16. execute_query(insert_query,insert_value)
  17. return response_success(data=comment,message="comment create success")
  18. def build_comment_tree(comments: list[dict[str, any]], parent_id: int = None) -> list[dict[str, any]]:
  19. tree = []
  20. for comment in comments:
  21. if comment['parent_id'] == parent_id:
  22. children = build_comment_tree(comments, comment['id'])
  23. if children:
  24. comment['children'] = children
  25. tree.append(comment)
  26. return tree
  27. @router.get("/list")
  28. @limiter.limit("10/minute")
  29. async def comment_list(request: Request, blog_id: int = Query(..., description="ID of the blog to filter comments")):
  30. select_query = """
  31. SELECT id, parent_id, blog_id, commentname, email, commenturl, commentcontent, create_at
  32. FROM comments
  33. WHERE blog_id = %s;
  34. """
  35. comments = fetch_all(select_query, (blog_id,))
  36. comment_tree = build_comment_tree(comments)
  37. return response_success(comment_tree, "comment get list success")
  38. # # 单条数据查询
  39. # @router.get("/list/search")
  40. # @limiter.limit("10/minute")
  41. # async def comment_search(request: Request,header:str=Query(description="评论标题")):
  42. # select_query="SELECT id,header,text,descr FROM comments WHERE 1=1 "
  43. # params=[]
  44. # if header:
  45. # select_query+="AND header LIKE %s"
  46. # params.append(f"%{header}%")
  47. # comment_query=fetch_all(select_query,params=params,fetchall=True)
  48. # return response_success(data=comment_query,message="comment search success")
  49. # # 评论修改
  50. # @router.put("/update/{id}")
  51. # @limiter.limit("10/minute")
  52. # async def comment_put(request: Request,comment:comment,id: str = Path(description="评论id"),_: User = Depends(get_current_active_user)):
  53. # update_query = (
  54. # "UPDATE comments SET header=%s,text=%s,descr=%s WHERE id=%s;"
  55. # )
  56. # update_data = (comment.header, comment.text,
  57. # comment.descr,id)
  58. # execute_query(update_query, update_data)
  59. # return response_success("comment update sucess")
  60. # 评论删除
  61. @router.delete("/delete/{id}")
  62. @limiter.limit("10/minute")
  63. async def comment_del(request: Request,id: str = Path(description="评论id")):
  64. update_query = (
  65. "DELETE FROM comments WHERE id=%s;"
  66. )
  67. update_data = (id)
  68. execute_query(update_query, update_data)
  69. return response_success()
  70. # # 根据id查询评论
  71. # @router.get("/list/search/{id}")
  72. # @limiter.limit("10/minute")
  73. # async def comment_search_id(request: Request,id:str=Path(description="评论id")):
  74. # select_query="SELECT * FROM comments WHERE id=%s"
  75. # comment_query=fetch_one(select_query,(id,))
  76. # return response_success(data=comment_query,message="comment search success")