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.

67 lines
2.8 KiB

7 months ago
9 months ago
7 months ago
9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months ago
7 months ago
9 months ago
  1. from fastapi import Depends, APIRouter, status, Query, Path, HTTPException,Request
  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. from limiter_config import limiter
  6. router = APIRouter(
  7. prefix="/classtics",
  8. tags=['语录管理']
  9. )
  10. # 获取列表
  11. @router.get("/list")
  12. @limiter.limit("10/minute")
  13. async def classtic_list(request: Request,):
  14. select_query = "SELECT id,header,`text`,descr FROM classtics;"
  15. classtic_list = fetch_all(select_query)
  16. return response_success(classtic_list, "classtic get list success")
  17. # 新增
  18. @router.post("/add")
  19. @limiter.limit("10/minute")
  20. async def classtic_add(request: Request,classtic:Classtic,_: User = Depends(get_current_active_user)):
  21. insert_query="""INSERT INTO classtics (header,`text`,descr) VALUES(%s,%s,%s)"""
  22. insert_value=(classtic.header,classtic.text,classtic.descr)
  23. execute_query(insert_query,insert_value)
  24. return response_success(data=classtic,message="classtic create success")
  25. # 单条数据查询
  26. @router.get("/list/search")
  27. @limiter.limit("10/minute")
  28. async def classtic_search(request: Request,header:str=Query(description="语录标题")):
  29. select_query="SELECT id,header,text,descr FROM classtics WHERE 1=1 "
  30. params=[]
  31. if header:
  32. select_query+="AND header LIKE %s"
  33. params.append(f"%{header}%")
  34. classtic_query=fetch_all(select_query,params=params,fetchall=True)
  35. return response_success(data=classtic_query,message="classtic search success")
  36. # 语录修改
  37. @router.put("/update/{id}")
  38. @limiter.limit("10/minute")
  39. async def classtic_put(request: Request,classtic:Classtic,id: str = Path(description="语录id"),_: User = Depends(get_current_active_user)):
  40. update_query = (
  41. "UPDATE classtics SET header=%s,text=%s,descr=%s WHERE id=%s;"
  42. )
  43. update_data = (classtic.header, classtic.text,
  44. classtic.descr,id)
  45. execute_query(update_query, update_data)
  46. return response_success("classtic update sucess")
  47. # 语录删除
  48. @router.delete("/delete/{id}")
  49. @limiter.limit("10/minute")
  50. async def classtic_del(request: Request,id: str = Path(description="语录id"),_: User = Depends(get_current_active_user)):
  51. update_query = (
  52. "DELETE FROM classtics WHERE id=%s;"
  53. )
  54. update_data = (id)
  55. execute_query(update_query, update_data)
  56. return response_success()
  57. # 根据id查询语录
  58. @router.get("/list/search/{id}")
  59. @limiter.limit("10/minute")
  60. async def classtic_search_id(request: Request,id:str=Path(description="语录id")):
  61. select_query="SELECT * FROM classtics WHERE id=%s"
  62. classtic_query=fetch_one(select_query,(id,))
  63. return response_success(data=classtic_query,message="classtic search success")