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.
152 lines
5.9 KiB
152 lines
5.9 KiB
from fastapi import Depends, APIRouter, Query, Path,Request
|
|
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
|
|
from limiter_config import limiter
|
|
import json
|
|
router = APIRouter(prefix="/diarys", tags=["日记管理"])
|
|
|
|
# 获取列表
|
|
|
|
|
|
@router.get("/list")
|
|
@limiter.limit("10/minute")
|
|
async def diary_list(request: Request,page: int = Query(None), page_size: int = Query(None)):
|
|
limit_clause = ""
|
|
if page is not None and page_size is not None:
|
|
offset = (page - 1) * page_size
|
|
limit_clause = f"LIMIT {page_size} OFFSET {offset}"
|
|
# 列表参数:日记名称、日记内容、创建时间、日记图片、日记查看时间、日记阅读次数、日记字数、类型名称、标签名列表
|
|
select_query = f"""
|
|
SELECT diarys.id,diarys.diarytitle, diarys.diarycontent,diarys.readnum, diarys.create_at, diarys.imglink,
|
|
diarys.wordcount, diarytypes.typename FROM diarys
|
|
LEFT JOIN `diarytypes` ON diarys.typeid = diarytypes.id
|
|
ORDER BY create_at DESC
|
|
{limit_clause};
|
|
"""
|
|
diary_list = fetch_all(select_query)
|
|
count_query = "SELECT COUNT(*) AS total FROM diarys;"
|
|
total_records = fetch_one(count_query)["total"]
|
|
return response_success({
|
|
"diarys": diary_list,
|
|
"total": total_records,
|
|
}, "diary get list success")
|
|
|
|
|
|
@router.get("/list/{id}")
|
|
@limiter.limit("10/minute")
|
|
async def diary_one(request: Request,id: int):
|
|
# 列表参数:日记名称、日记内容、创建时间、日记图片、日记查看时间、日记阅读次数、日记字数、类型名称、标签名列表
|
|
select_query = """
|
|
SELECT id, diarytitle, diarycontent FROM diarys
|
|
WHERE id = %s
|
|
ORDER BY create_at DESC;
|
|
"""
|
|
diary_one = fetch_one(select_query, (id,))
|
|
return response_success(diary_one, "diary get diary_one success")
|
|
|
|
|
|
# 日记新增
|
|
@router.post("/add")
|
|
@limiter.limit("10/minute")
|
|
async def diary_add(request: Request,diary: Diary, _: User = Depends(get_current_active_user)):
|
|
select_query = "SELECT * FROM diarys WHERE diarytitle = %s"
|
|
existing_diary = fetch_one(select_query, (diary.diarytitle,))
|
|
raise_if_exists(existing_diary, "diary already exists")
|
|
insert_query = (
|
|
"INSERT INTO diarys (diarytitle, diarycontent,imglink, typeid, descr) VALUES (%s, %s, %s, %s,%s)"
|
|
)
|
|
insert_value=(diary.diarytitle,diary.diarycontent,diary.imglink,diary.typeid,diary.descr)
|
|
execute_query(insert_query,insert_value)
|
|
|
|
return {"message": "diary created successfully"}
|
|
|
|
|
|
# 日记删除
|
|
@router.delete("/delete/{id}")
|
|
@limiter.limit("10/minute")
|
|
async def diary_delete(request: Request,id: str = Path(description="日记id"),_: User = Depends(get_current_active_user)):
|
|
select_query = "SELECT * FROM diarys WHERE id = %s"
|
|
existing_diary = fetch_one(select_query, (id,))
|
|
raise_if_not_found(existing_diary, "diary not found")
|
|
delete_query = "DELETE FROM diarys WHERE id = %s"
|
|
execute_query(delete_query, (id,))
|
|
return response_success(message="diary delete success")
|
|
|
|
|
|
@router.put("/update/{id}")
|
|
async def diary_update(request: Request,id: int, diary: Diary, _: User = Depends(get_current_active_user)):
|
|
# 检查要编辑的日记是否存在
|
|
select_query = "SELECT * FROM diarys WHERE id = %s"
|
|
existing_diary = fetch_one(select_query, (id,))
|
|
raise_if_not_found(existing_diary, "diary not found")
|
|
|
|
# 更新日记信息
|
|
update_query = (
|
|
"UPDATE diarys SET diarytitle = %s, diarycontent = %s, imglink = %s, typeid = %s, descr = %s WHERE id = %s"
|
|
)
|
|
update_data = (diary.diarytitle, diary.diarycontent,
|
|
diary.imglink, diary.typeid, diary.descr, id)
|
|
execute_query(update_query, update_data)
|
|
return response_success("diary update sucess")
|
|
|
|
@router.put("/update/{id}/readnum")
|
|
@limiter.limit("10/minute")
|
|
async def diary_update_num(request: Request, id: int):
|
|
update_query ="UPDATE diarys SET readnum = readnum + 1 WHERE id = %s"
|
|
execute_query(update_query,(id,))
|
|
return response_success("diary update sucess")
|
|
|
|
|
|
# 日记模糊查询
|
|
@router.get("/search")
|
|
@limiter.limit("10/minute")
|
|
async def diary_list_search(
|
|
request: Request,
|
|
diarytitle: str = Query(None, description="日记标题"),
|
|
typename: str = Query(None, description="日记类型"),
|
|
start_date: str = Query(None, description="开始时间"),
|
|
end_date: str = Query(None, description="结束时间"),
|
|
):
|
|
select_query = """
|
|
SELECT diarys.id, diarytitle, diarycontent,wordcount, typename, create_at, update_at, diarys.descr
|
|
FROM diarys
|
|
LEFT JOIN `diarytypes` ON diarys.typeid = diarytypes.id
|
|
WHERE 1=1
|
|
"""
|
|
params = []
|
|
if diarytitle:
|
|
select_query += " AND diarytitle LIKE %s"
|
|
params.append(f"%{diarytitle}%")
|
|
if typename:
|
|
select_query += " AND typename LIKE %s"
|
|
params.append(f"%{typename}%")
|
|
if start_date:
|
|
select_query += " AND create_at >= %s"
|
|
params.append(start_date)
|
|
if end_date:
|
|
select_query += " AND create_at <= %s"
|
|
params.append(end_date)
|
|
select_query += "ORDER BY create_at DESC"
|
|
diary_list = fetch_all(select_query, params=params, fetchall=True)
|
|
return response_success(data=diary_list, message="diary serach succuessfully!")
|
|
|
|
|
|
# 根据id查询日记
|
|
@router.get("/search/{id}")
|
|
@limiter.limit("10/minute")
|
|
async def get_id_diary(request: Request,id: str = Path(description="日记id")):
|
|
select_query = """SELECT diarys.id, diarytitle, diarycontent,wordcount, diarys.typeid, diarys.descr,imglink FROM diarys
|
|
LEFT JOIN `diarytypes` ON diarys.typeid = diarytypes.id
|
|
WHERE diarys.id = %s
|
|
|
|
"""
|
|
diary_list = execute_query(select_query, (id,))
|
|
return response_success(data=diary_list, message="diary search success")
|