comment.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from sql import db
  2. def read_comment_list(blog_id: int):
  3. """ 读取文章的 comment """
  4. cur = db.search(columns=["CommentID"],
  5. table="comment_user",
  6. where=f"BlogID={blog_id}",
  7. order_by=[("UpdateTime", "DESC")])
  8. if cur is None or cur.rowcount == 0:
  9. return []
  10. return [i[0] for i in cur.fetchall()]
  11. def create_comment(blog_id: int, user_id: int, content: str):
  12. """ 新建 comment """
  13. content = content.replace("'", "''")
  14. cur = db.insert(table="comment",
  15. columns=["BlogID", "Auth", "Content"],
  16. values=f"{blog_id}, {user_id}, '{content}'")
  17. if cur is None or cur.rowcount == 0:
  18. return False
  19. return True
  20. def read_comment(comment_id: int):
  21. """ 读取 comment """
  22. cur = db.search(columns=["BlogID", "Email", "Content", "UpdateTime"],
  23. table="comment_user",
  24. where=f"CommentID={comment_id}")
  25. if cur is None or cur.rowcount == 0:
  26. return [-1, "", "", 0]
  27. return cur.fetchone()
  28. def delete_comment(comment_id):
  29. """ 删除评论 """
  30. cur = db.delete(table="comment", where=f"ID={comment_id}")
  31. if cur is None or cur.rowcount == 0:
  32. return False
  33. return True
  34. def get_user_comment_count(user_id: int):
  35. """ 读取指定用户的 comment 个数 """
  36. cur = db.search(columns=["count(ID)"], table="comment",
  37. where=f"Auth={user_id}")
  38. if cur is None or cur.rowcount == 0:
  39. return 0
  40. return cur.fetchone()[0]