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.

151 lines
5.9 KiB

8 months ago
8 months ago
7 months ago
8 months ago
7 months ago
8 months ago
8 months ago
8 months ago
7 months ago
8 months ago
7 months ago
8 months ago
7 months ago
8 months ago
7 months ago
8 months ago
8 months ago
7 months ago
7 months ago
8 months ago
8 months ago
8 months ago
7 months ago
8 months ago
7 months ago
8 months ago
8 months ago
7 months ago
8 months ago
8 months ago
  1. from fastapi import Depends, APIRouter, Query, Path,Request
  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. from limiter_config import limiter
  13. import json
  14. router = APIRouter(prefix="/diarys", tags=["日记管理"])
  15. # 获取列表
  16. @router.get("/list")
  17. @limiter.limit("10/minute")
  18. async def diary_list(request: Request,page: int = Query(None), page_size: int = Query(None)):
  19. limit_clause = ""
  20. if page is not None and page_size is not None:
  21. offset = (page - 1) * page_size
  22. limit_clause = f"LIMIT {page_size} OFFSET {offset}"
  23. # 列表参数:日记名称、日记内容、创建时间、日记图片、日记查看时间、日记阅读次数、日记字数、类型名称、标签名列表
  24. select_query = f"""
  25. SELECT diarys.id,diarys.diarytitle, diarys.diarycontent,diarys.readnum, diarys.create_at, diarys.imglink,
  26. diarys.wordcount, diarytypes.typename FROM diarys
  27. LEFT JOIN `diarytypes` ON diarys.typeid = diarytypes.id
  28. ORDER BY create_at DESC
  29. {limit_clause};
  30. """
  31. diary_list = fetch_all(select_query)
  32. count_query = "SELECT COUNT(*) AS total FROM diarys;"
  33. total_records = fetch_one(count_query)["total"]
  34. return response_success({
  35. "diarys": diary_list,
  36. "total": total_records,
  37. }, "diary get list success")
  38. @router.get("/list/{id}")
  39. @limiter.limit("10/minute")
  40. async def diary_one(request: Request,id: int):
  41. # 列表参数:日记名称、日记内容、创建时间、日记图片、日记查看时间、日记阅读次数、日记字数、类型名称、标签名列表
  42. select_query = """
  43. SELECT id, diarytitle, diarycontent FROM diarys
  44. WHERE id = %s
  45. ORDER BY create_at DESC;
  46. """
  47. diary_one = fetch_one(select_query, (id,))
  48. return response_success(diary_one, "diary get diary_one success")
  49. # 日记新增
  50. @router.post("/add")
  51. @limiter.limit("10/minute")
  52. async def diary_add(request: Request,diary: Diary, _: User = Depends(get_current_active_user)):
  53. select_query = "SELECT * FROM diarys WHERE diarytitle = %s"
  54. existing_diary = fetch_one(select_query, (diary.diarytitle,))
  55. raise_if_exists(existing_diary, "diary already exists")
  56. insert_query = (
  57. "INSERT INTO diarys (diarytitle, diarycontent,imglink, typeid, descr) VALUES (%s, %s, %s, %s,%s)"
  58. )
  59. insert_value=(diary.diarytitle,diary.diarycontent,diary.imglink,diary.typeid,diary.descr)
  60. execute_query(insert_query,insert_value)
  61. return {"message": "diary created successfully"}
  62. # 日记删除
  63. @router.delete("/delete/{id}")
  64. @limiter.limit("10/minute")
  65. async def diary_delete(request: Request,id: str = Path(description="日记id"),_: User = Depends(get_current_active_user)):
  66. select_query = "SELECT * FROM diarys WHERE id = %s"
  67. existing_diary = fetch_one(select_query, (id,))
  68. raise_if_not_found(existing_diary, "diary not found")
  69. delete_query = "DELETE FROM diarys WHERE id = %s"
  70. execute_query(delete_query, (id,))
  71. return response_success(message="diary delete success")
  72. @router.put("/update/{id}")
  73. async def diary_update(request: Request,id: int, diary: Diary, _: User = Depends(get_current_active_user)):
  74. # 检查要编辑的日记是否存在
  75. select_query = "SELECT * FROM diarys WHERE id = %s"
  76. existing_diary = fetch_one(select_query, (id,))
  77. raise_if_not_found(existing_diary, "diary not found")
  78. # 更新日记信息
  79. update_query = (
  80. "UPDATE diarys SET diarytitle = %s, diarycontent = %s, imglink = %s, typeid = %s, descr = %s WHERE id = %s"
  81. )
  82. update_data = (diary.diarytitle, diary.diarycontent,
  83. diary.imglink, diary.typeid, diary.descr, id)
  84. execute_query(update_query, update_data)
  85. return response_success("diary update sucess")
  86. @router.put("/update/{id}/readnum")
  87. @limiter.limit("10/minute")
  88. async def diary_update_num(request: Request, id: int):
  89. update_query ="UPDATE diarys SET readnum = readnum + 1 WHERE id = %s"
  90. execute_query(update_query,(id,))
  91. return response_success("diary update sucess")
  92. # 日记模糊查询
  93. @router.get("/search")
  94. @limiter.limit("10/minute")
  95. async def diary_list_search(
  96. request: Request,
  97. diarytitle: str = Query(None, description="日记标题"),
  98. typename: str = Query(None, description="日记类型"),
  99. start_date: str = Query(None, description="开始时间"),
  100. end_date: str = Query(None, description="结束时间"),
  101. ):
  102. select_query = """
  103. SELECT diarys.id, diarytitle, diarycontent,wordcount, typename, create_at, update_at, diarys.descr
  104. FROM diarys
  105. LEFT JOIN `diarytypes` ON diarys.typeid = diarytypes.id
  106. WHERE 1=1
  107. """
  108. params = []
  109. if diarytitle:
  110. select_query += " AND diarytitle LIKE %s"
  111. params.append(f"%{diarytitle}%")
  112. if typename:
  113. select_query += " AND typename LIKE %s"
  114. params.append(f"%{typename}%")
  115. if start_date:
  116. select_query += " AND create_at >= %s"
  117. params.append(start_date)
  118. if end_date:
  119. select_query += " AND create_at <= %s"
  120. params.append(end_date)
  121. select_query += "ORDER BY create_at DESC"
  122. diary_list = fetch_all(select_query, params=params, fetchall=True)
  123. return response_success(data=diary_list, message="diary serach succuessfully!")
  124. # 根据id查询日记
  125. @router.get("/search/{id}")
  126. @limiter.limit("10/minute")
  127. async def get_id_diary(request: Request,id: str = Path(description="日记id")):
  128. select_query = """SELECT diarys.id, diarytitle, diarycontent,wordcount, diarys.typeid, diarys.descr,imglink FROM diarys
  129. LEFT JOIN `diarytypes` ON diarys.typeid = diarytypes.id
  130. WHERE diarys.id = %s
  131. """
  132. diary_list = execute_query(select_query, (id,))
  133. return response_success(data=diary_list, message="diary search success")