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.

156 lines
6.3 KiB

9 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
9 months ago
8 months ago
9 months ago
9 months ago
8 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
9 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
8 months ago
10 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
10 months ago
8 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
8 months ago
9 months ago
  1. from fastapi import Depends, APIRouter, Query, Path
  2. from internal.models import *
  3. from internal.database import (
  4. fetch_one,
  5. fetch_all,
  6. execute_query,
  7. response_success,
  8. raise_if_exists,
  9. raise_if_not_found,
  10. )
  11. from dependencies import get_current_active_user
  12. router = APIRouter(prefix="/blogs", tags=["博客管理"])
  13. # 获取列表
  14. @router.get("/list")
  15. async def blog_list():
  16. # 列表参数:博客名称、博客内容、创建时间、博客图片、博客查看时间、博客阅读次数、博客字数、类型名称、标签名列表
  17. select_query = """
  18. SELECT blogs.id,blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink,
  19. blogs.readminite, blogs.readnum, blogs.wordcount, types.typename,
  20. JSON_ARRAYAGG(labels.labelname) AS labelnames
  21. FROM blogs
  22. LEFT JOIN `types` ON blogs.typeid = types.id
  23. LEFT JOIN blog_label ON blog_label.blogid = blogs.id
  24. LEFT JOIN labels ON blog_label.labelid = labels.id
  25. GROUP BY blogs.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink,
  26. blogs.readminite, blogs.readnum, blogs.wordcount, types.typename ORDER BY create_at DESC;
  27. """
  28. blog_list = fetch_all(select_query)
  29. return response_success(blog_list, "blog get list success")
  30. # 博客新增
  31. @router.post("/add")
  32. async def blog_add(blog: Blog,labels: list[Label], _: User = Depends(get_current_active_user)):
  33. select_query = "SELECT * FROM blogs WHERE blogtitle = %s"
  34. existing_blog = fetch_one(select_query, (blog.blogtitle,))
  35. raise_if_exists(existing_blog, "Blog already exists")
  36. insert_query = (
  37. "INSERT INTO blogs (blogtitle, blogcontent,imglink, typeid, descr) VALUES (%s, %s, %s, %s,%s)"
  38. )
  39. insert_data = (blog.blogtitle, blog.blogcontent,blog.imglink,blog.typeid, blog.descr)
  40. blog_id = execute_query(insert_query, insert_data, lastrowid=True)
  41. for label in labels:
  42. insert_label_query="INSERT INTO blog_label (blogid, labelid) VALUES (%s, %s)"
  43. execute_query(insert_label_query,(blog_id,label.id))
  44. return {"message": "Blog created successfully", "blog_id": blog_id}
  45. # 博客删除
  46. @router.delete("/delete/{id}")
  47. async def blog_delete(id: str = Path(description="博客id")):
  48. select_query = "SELECT * FROM blogs WHERE id = %s"
  49. existing_blog = fetch_one(select_query, (id,))
  50. raise_if_not_found(existing_blog, "blog not found")
  51. insert_query = "DELETE FROM blogs WHERE id = %s"
  52. execute_query(insert_query, (id,))
  53. return response_success(message="blog delete success")
  54. # 博客修改
  55. # @router.put("/update/{id}")
  56. # async def blog_put(blog: Blog, id: str = Path(description="博客id")):
  57. # select_query = "SELECT * FROM blogs WHERE id=%s"
  58. # existing_blog = fetch_one(select_query, (id,))
  59. # raise_if_not_found(existing_blog, "blog not found")
  60. # update_query = (
  61. # "UPDATE blogs SET blogtitle=%s,blogcontent=%s,typeid=%s,descr=%s WHERE id=%s;"
  62. # )
  63. # update_data = (blog.blogtitle, blog.blogcontent,
  64. # blog.typeid, blog.descr, id)
  65. # execute_query(update_query, update_data)
  66. # return response_success("blog update sucess")
  67. @router.put("/update/{id}")
  68. async def blog_update(id: int, blog: Blog, labels: list[Label], _: User = Depends(get_current_active_user)):
  69. # 检查要编辑的博客是否存在
  70. select_query = "SELECT * FROM blogs WHERE id = %s"
  71. existing_blog = fetch_one(select_query, (id,))
  72. raise_if_not_found(existing_blog, "blog not found")
  73. # 更新博客信息
  74. update_query = (
  75. "UPDATE blogs SET blogtitle = %s, blogcontent = %s, imglink = %s, typeid = %s, descr = %s WHERE id = %s"
  76. )
  77. update_data = (blog.blogtitle, blog.blogcontent, blog.imglink, blog.typeid, blog.descr, id)
  78. execute_query(update_query, update_data)
  79. # 首先删除原有的关联标签
  80. delete_query = "DELETE FROM blog_label WHERE blogid = %s"
  81. execute_query(delete_query, (id,))
  82. # 然后插入新的关联标签
  83. for label in labels:
  84. insert_label_query = "INSERT INTO blog_label (blogid, labelid) VALUES (%s, %s)"
  85. execute_query(insert_label_query, (id, label.id))
  86. return response_success("blog update sucess")
  87. # 博客模糊查询
  88. @router.get("/list/search")
  89. async def blog_list_search(
  90. blogtitle: str = Query(None, description="博客标题"),
  91. typename: str = Query(None, description="博客类型"),
  92. start_date: str = Query(None, description="开始时间"),
  93. end_date: str = Query(None, description="结束时间"),
  94. ):
  95. select_query = """
  96. SELECT blogs.id, blogtitle, blogcontent,wordcount, typename, create_at, update_at, blogs.descr,JSON_ARRAYAGG(labels.labelname) AS labelnames
  97. FROM blogs
  98. LEFT JOIN `types` ON blogs.typeid = types.id
  99. LEFT JOIN blog_label ON blogs.id = blog_label.blogid
  100. LEFT JOIN labels ON blog_label.labelid = labels.id
  101. WHERE 1=1
  102. """
  103. params = []
  104. if blogtitle:
  105. select_query += " AND blogtitle LIKE %s"
  106. params.append(f"%{blogtitle}%")
  107. if typename:
  108. select_query += " AND typename LIKE %s"
  109. params.append(f"%{typename}%")
  110. if start_date:
  111. select_query += " AND create_at >= %s"
  112. params.append(start_date)
  113. if end_date:
  114. select_query += " AND create_at <= %s"
  115. params.append(end_date)
  116. select_query += "GROUP BY blogs.id, blogs.blogtitle, blogs.blogcontent,blogs.wordcount, types.typename, blogs.create_at, blogs.update_at, blogs.descr ORDER BY create_at DESC"
  117. blog_list = fetch_all(select_query, params=params, fetchall=True)
  118. return response_success(data=blog_list, message="blog serach succuessfully!")
  119. # 根据id查询博客
  120. @router.get("/list/search/{id}")
  121. async def get_id_blog(id: str = Path(description="博客id")):
  122. select_query = """SELECT blogs.id, blogtitle, blogcontent,wordcount, blogs.typeid, blogs.descr,JSON_ARRAYAGG(labels.labelname) AS labelnames,imglink FROM blogs
  123. LEFT JOIN `types` ON blogs.typeid = types.id
  124. LEFT JOIN blog_label ON blogs.id = blog_label.blogid
  125. LEFT JOIN labels ON blog_label.labelid = labels.id
  126. WHERE blogs.id = %s
  127. GROUP BY blogs.id;
  128. """
  129. blog_list = execute_query(select_query, (id,))
  130. return response_success(data=blog_list, message="blog search success")
  131. # 我就测试一下