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.
62 lines
2.3 KiB
62 lines
2.3 KiB
from fastapi import Depends, APIRouter, status, Query, Path, HTTPException
|
|
from internal.models import *
|
|
from internal.database import fetch_one, fetch_all, execute_query, response_success, raise_if_exists,raise_if_not_found
|
|
from dependencies import get_current_active_user
|
|
|
|
router = APIRouter(
|
|
prefix="/classtics",
|
|
tags=['语录管理']
|
|
)
|
|
# 获取列表
|
|
@router.get("/list")
|
|
async def classtic_list():
|
|
select_query = "SELECT id,header,`text`,descr FROM classtics;"
|
|
classtic_list = fetch_all(select_query)
|
|
return response_success(classtic_list, "classtic get list success")
|
|
|
|
# 新增
|
|
@router.post("/add")
|
|
async def classtic_add(classtic:Classtic):
|
|
insert_query="""INSERT INTO classtics (header,`text`,descr) VALUES(%s,%s,%s)"""
|
|
insert_value=(classtic.header,classtic.text,classtic.descr)
|
|
execute_query(insert_query,insert_value)
|
|
return response_success(data=classtic,message="classtic create success")
|
|
|
|
# 单条数据查询
|
|
@router.get("/list/search")
|
|
async def classtic_search(header:str=Query(description="语录标题")):
|
|
select_query="SELECT id,header,text,descr FROM classtics WHERE 1=1 "
|
|
params=[]
|
|
if header:
|
|
select_query+="AND header LIKE %s"
|
|
params.append(f"%{header}%")
|
|
classtic_query=fetch_all(select_query,params=params,fetchall=True)
|
|
return response_success(data=classtic_query,message="classtic search success")
|
|
|
|
# 语录修改
|
|
@router.put("/update/{id}")
|
|
async def classtic_put(classtic:Classtic,id: str = Path(description="语录id")):
|
|
update_query = (
|
|
"UPDATE classtics SET header=%s,text=%s,descr=%s WHERE id=%s;"
|
|
)
|
|
update_data = (classtic.header, classtic.text,
|
|
classtic.descr,id)
|
|
execute_query(update_query, update_data)
|
|
return response_success("classtic update sucess")
|
|
|
|
# 语录删除
|
|
@router.delete("/delete/{id}")
|
|
async def classtic_del(id: str = Path(description="语录id")):
|
|
update_query = (
|
|
"DELETE FROM classtics WHERE id=%s;"
|
|
)
|
|
update_data = (id)
|
|
execute_query(update_query, update_data)
|
|
return response_success()
|
|
|
|
# 根据id查询语录
|
|
@router.get("/list/search/{id}")
|
|
async def classtic_search_id(id:str=Path(description="语录id")):
|
|
select_query="SELECT * FROM classtics WHERE id=%s"
|
|
classtic_query=fetch_one(select_query,(id,))
|
|
return response_success(data=classtic_query,message="classtic search success")
|