1
0

comment.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from sql import db
  2. from sql.cache import (get_comment_from_cache, write_comment_to_cache, delete_comment_from_cache,
  3. get_user_comment_count_from_cache, write_user_comment_count_to_cache,
  4. delete_all_user_comment_count_from_cache, delete_user_comment_count_from_cache)
  5. def read_comment_list(blog_id: int):
  6. """ 读取文章的 comment """
  7. cur = db.search("SELECT CommentID "
  8. "FROM comment_user "
  9. "WHERE BlogID=%s "
  10. "ORDER BY UpdateTime DESC", blog_id)
  11. if cur is None or cur.rowcount == 0:
  12. return []
  13. return [i[0] for i in cur.fetchall()]
  14. def create_comment(blog_id: int, user_id: int, content: str):
  15. """ 新建 comment """
  16. delete_user_comment_count_from_cache(user_id)
  17. cur = db.insert("INSERT INTO comment(BlogID, Auth, Content) "
  18. "VALUES (%s, %s, %s)", blog_id, user_id, content)
  19. if cur is None or cur.rowcount == 0:
  20. return False
  21. return True
  22. def read_comment(comment_id: int):
  23. """ 读取 comment """
  24. res = get_comment_from_cache(comment_id)
  25. if res is not None:
  26. return res
  27. cur = db.search("SELECT BlogID, Email, Content, UpdateTime FROM comment_user WHERE CommentID=%s", comment_id)
  28. if cur is None or cur.rowcount == 0:
  29. return [-1, "", "", 0]
  30. res = cur.fetchone()
  31. write_comment_to_cache(comment_id, *res)
  32. return res
  33. def delete_comment(comment_id):
  34. """ 删除评论 """
  35. delete_comment_from_cache(comment_id)
  36. delete_all_user_comment_count_from_cache()
  37. cur = db.delete("DELETE FROM comment WHERE ID=%s", comment_id)
  38. if cur is None or cur.rowcount == 0:
  39. return False
  40. return True
  41. def get_user_comment_count(user_id: int):
  42. """ 读取指定用户的 comment 个数 """
  43. cur = db.search("SELECT COUNT(*) FROM comment WHERE Auth=%s", user_id)
  44. if cur is None or cur.rowcount == 0:
  45. return 0
  46. return cur.fetchone()[0]