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.

60 lines
2.2 KiB

9 months ago
10 months ago
9 months ago
10 months ago
8 months ago
11 months ago
9 months ago
10 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 months ago
8 months ago
9 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="/diarytypes",
  7. tags=['日记分类']
  8. )
  9. # 获取列表
  10. @router.get("/list")
  11. async def type_list():
  12. select_query = "SELECT id,typename,descr FROM diarytypes;"
  13. type_list = fetch_all(select_query)
  14. return response_success(type_list, "type get list success")
  15. # 新增
  16. @router.post("/add")
  17. async def type_add(type:Type):
  18. insert_query="""INSERT INTO diarytypes (typename,descr) VALUES(%s,%s)"""
  19. insert_value=(type.typename,type.descr)
  20. execute_query(insert_query,insert_value)
  21. return response_success(data=type,message="type create success")
  22. # 单条数据查询
  23. @router.get("/list/search")
  24. async def type_search(typename:str=Query(description="类型名称")):
  25. select_query="SELECT id,typename,descr FROM diarytypes WHERE 1=1 "
  26. params=[]
  27. if typename:
  28. select_query+="AND typename LIKE %s"
  29. params.append(f"%{typename}%")
  30. type_query=fetch_all(select_query,params=params,fetchall=True)
  31. return response_success(data=type_query,message="type search success")
  32. # 语录修改
  33. @router.put("/update/{id}")
  34. async def type_put(type:Type,id: str = Path(description="类型id")):
  35. update_query = (
  36. "UPDATE diarytypes SET typename=%s,descr=%s WHERE id=%s;"
  37. )
  38. update_data = (type.typename,type.descr,id)
  39. execute_query(update_query, update_data)
  40. return response_success("type update sucess")
  41. # 语录删除
  42. @router.delete("/delete/{id}")
  43. async def type_del(id: str = Path(description="类型id")):
  44. update_query = (
  45. "DELETE FROM diarytypes WHERE id=%s;"
  46. )
  47. update_data = (id)
  48. execute_query(update_query, update_data)
  49. return response_success()
  50. # 根据id查询语录
  51. @router.get("/list/search/{id}")
  52. async def type_search_id(id:str=Path(description="类型id")):
  53. select_query="SELECT * FROM diarytypes WHERE id=%s"
  54. type_query=fetch_one(select_query,(id,))
  55. return response_success(data=type_query,message="type search success")