blog.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from sql import db
  2. from typing import Optional, List
  3. import object.archive
  4. def create_blog(auth_id: int, title: str, subtitle: str, context: str,
  5. archive_list: List[object.archive.Archive]) -> bool:
  6. """ 写入新的blog """
  7. cur = db.insert(table="blog", columns=["Auth", "Title", "SubTitle", "Context"],
  8. values=f"{auth_id}, '{title}', '{subtitle}', '{context}'")
  9. if cur is None or cur.rowcount == 0:
  10. return False
  11. blog_id = cur.lastrowid
  12. for archive in archive_list:
  13. cur = db.insert(table="blog_archive", columns=["BlogID", "ArchiveID"],
  14. values=f"{blog_id}, {archive.archive_id}")
  15. if cur is None or cur.rowcount == 0:
  16. return False
  17. return True
  18. def update_blog(blog_id: int, context: str) -> bool:
  19. """ 更新博客文章 """
  20. cur = db.update(table="blog",
  21. kw={"UpdateTime": "CURRENT_TIMESTAMP()", "Context": f"'{context}'"},
  22. where=f"ID={blog_id}")
  23. if cur is None or cur.rowcount != 1:
  24. return False
  25. return True
  26. def read_blog(blog_id: int) -> list:
  27. """ 读取blog内容 """
  28. cur = db.search(columns=["Auth", "Title", "SubTitle", "Context", "UpdateTime", "Top"],
  29. table="blog",
  30. where=f"ID={blog_id}")
  31. if cur is None or cur.rowcount == 0:
  32. return []
  33. return cur.fetchone()
  34. def delete_blog(blog_id: int):
  35. cur = db.delete(table="blog_archive", where=f"BlogID={blog_id}")
  36. if cur is None:
  37. return False
  38. cur = db.delete(table="comment", where=f"BlogID={blog_id}")
  39. if cur is None:
  40. return False
  41. cur = db.delete(table="blog", where=f"ID={blog_id}")
  42. if cur is None or cur.rowcount == 0:
  43. return False
  44. return True
  45. def get_blog_list(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  46. """ 获得 blog 列表 """
  47. cur = db.search(columns=["ID", "Title", "SubTitle", "UpdateTime", "Top"], table="blog_with_top",
  48. limit=limit,
  49. offset=offset)
  50. if cur is None or cur.rowcount == 0:
  51. return []
  52. return cur.fetchall()
  53. def get_blog_list_not_top(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  54. """ 获得blog列表 忽略置顶 """
  55. cur = db.search(columns=["ID", "Title", "SubTitle", "UpdateTime"], table="blog",
  56. order_by=[("UpdateTime", "DESC")],
  57. limit=limit,
  58. offset=offset)
  59. if cur is None or cur.rowcount == 0:
  60. return []
  61. return cur.fetchall()
  62. def get_blog_count() -> int:
  63. """ 统计 blog 个数 """
  64. cur = db.search(columns=["count(ID)"], table="blog")
  65. if cur is None or cur.rowcount == 0:
  66. return 0
  67. return cur.fetchone()[0]
  68. def get_archive_blog_list(archive_id, limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  69. """ 获得指定归档的 blog 列表 """
  70. cur = db.search(columns=["BlogID", "Title", "SubTitle", "UpdateTime", "Top"], table="blog_with_archive",
  71. where=f"ArchiveID={archive_id}",
  72. limit=limit,
  73. offset=offset)
  74. if cur is None or cur.rowcount == 0:
  75. return []
  76. return cur.fetchall()
  77. def get_archive_blog_count(archive_id) -> int:
  78. """ 统计指定归档的 blog 个数 """
  79. cur = db.search(columns=["count(ID)"], table="blog_with_archive",
  80. where=f"ArchiveID={archive_id}")
  81. if cur is None or cur.rowcount == 0:
  82. return 0
  83. return cur.fetchone()[0]
  84. def get_user_user_count(user_id: int) -> int:
  85. """ 获得指定用户的 blog 个数 """
  86. cur = db.search(columns=["count(ID)"], table="blog",
  87. where=f"Auth={user_id}")
  88. if cur is None or cur.rowcount == 0:
  89. return 0
  90. return cur.fetchone()[0]