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.

75 lines
3.3 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. from fastapi import Depends, APIRouter, status, Query, Path, HTTPException
  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. router = APIRouter(
  6. prefix="/blogs",
  7. tags=['博客管理']
  8. )
  9. # 获取列表
  10. @router.get("/list")
  11. async def blog_list():
  12. select_query = "SELECT blogs.id, blogtitle, blogcontent, typename, create_at, update_at, blogs.descr FROM blogs LEFT JOIN `types` ON blogs.typeid = types.id ORDER BY create_at DESC;"
  13. blog_list = fetch_all(select_query)
  14. return response_success(blog_list, "blog get list success")
  15. # 博客新增
  16. @router.post('/add')
  17. async def blog_add(blog: Blog, _: User = Depends(get_current_active_user)):
  18. select_query = "SELECT * FROM blogs WHERE blogtitle = %s"
  19. existing_blog = fetch_one(select_query, (blog.blogtitle,))
  20. raise_if_exists(existing_blog, "blog is already exists")
  21. insert_query = "INSERT INTO blogs (blogtitle,blogcontent,typeid,descr) VALUES (%s,%s,%s,%s)"
  22. insert_data = (blog.blogtitle, blog.blogcontent, blog.typeid, blog.descr)
  23. execute_query(insert_query, insert_data)
  24. return response_success(data=blog,message="blog create success")
  25. # 博客删除
  26. @router.delete("/delete/{id}")
  27. async def blog_delete(id: str = Path(description="博客id")):
  28. select_query = "SELECT * FROM blogs WHERE id = %s"
  29. existing_blog=fetch_one(select_query,(id,))
  30. raise_if_not_found(existing_blog,"blog not found")
  31. insert_query = "DELETE FROM blogs WHERE id = %s"
  32. execute_query(insert_query, (id,))
  33. return response_success(message="blog delete success")
  34. # 博客修改
  35. @router.put("/update/{id}")
  36. async def blog_put(blog: Blog, id: str = Path(description="博客id")):
  37. select_query="SELECT * FROM blogs WHERE id=%s"
  38. existing_blog=fetch_one(select_query,(id,))
  39. raise_if_not_found(existing_blog,"blog not found")
  40. update_query = "UPDATE blogs SET blogtitle=%s,blogcontent=%s,typeid=%s,descr=%s WHERE id=%s;"
  41. update_data = (blog.blogtitle, blog.blogcontent,
  42. blog.typeid, blog.descr, id)
  43. execute_query(update_query, update_data)
  44. return response_success("blog update sucess")
  45. # 博客查询
  46. @router.get("/list/search")
  47. async def blog_list_search(
  48. blogtitle: str = Query(None, description="博客标题"),
  49. typename: str = Query(None, description="博客类型"),
  50. start_date: str = Query(None, description="开始时间"),
  51. end_date: str = Query(None, description="结束时间"),
  52. ):
  53. select_query = "SELECT blogs.id, blogtitle, blogcontent, typename, create_at, update_at, blogs.descr FROM blogs LEFT JOIN `types` ON blogs.typeid = types.id WHERE 1=1"
  54. params = []
  55. if blogtitle:
  56. select_query += " AND blogtitle LIKE %s"
  57. params.append(f"%{blogtitle}%")
  58. if typename:
  59. select_query += " AND typename LIKE %s"
  60. params.append(f"%{typename}%")
  61. if start_date:
  62. select_query += " AND create_at >= %s"
  63. params.append(start_date)
  64. if end_date:
  65. select_query += " AND create_at <= %s"
  66. params.append(end_date)
  67. select_query += " ORDER BY create_at DESC"
  68. blog_list = fetch_all(select_query, params=params, fetchall=True)
  69. return response_success(data=blog_list,message="blog serach succuessfully!")