6 changed files with 246 additions and 17 deletions
-
28internal/models.py
-
6main.py
-
12routers/blogmanage.py
-
16routers/blogtype.py
-
140routers/diarymanage.py
-
61routers/diarytype.py
@ -0,0 +1,140 @@ |
|||
from fastapi import Depends, APIRouter, Query, Path |
|||
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 |
|||
import json |
|||
router = APIRouter(prefix="/diarys", tags=["日记管理"]) |
|||
|
|||
# 获取列表 |
|||
|
|||
|
|||
@router.get("/list") |
|||
async def diary_list(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.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}") |
|||
async def diary_one(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") |
|||
async def diary_add(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}") |
|||
async def diary_delete(id: str = Path(description="日记id")): |
|||
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(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.get("/list/search") |
|||
async def diary_list_search( |
|||
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("/list/search/{id}") |
|||
async def get_id_diary(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") |
|||
|
|||
|
|||
# 我就测试一下 |
@ -0,0 +1,61 @@ |
|||
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="/diarytypes", |
|||
tags=['日记分类'] |
|||
) |
|||
# 获取列表 |
|||
@router.get("/list") |
|||
async def type_list(): |
|||
select_query = "SELECT id,typename,descr FROM diarytypes;" |
|||
type_list = fetch_all(select_query) |
|||
return response_success(type_list, "type get list success") |
|||
|
|||
# 新增 |
|||
@router.post("/add") |
|||
async def type_add(type:Type): |
|||
insert_query="""INSERT INTO diarytypes (typename,descr) VALUES(%s,%s)""" |
|||
insert_value=(type.typename,type.descr) |
|||
execute_query(insert_query,insert_value) |
|||
return response_success(data=type,message="type create success") |
|||
|
|||
# 单条数据查询 |
|||
@router.get("/list/search") |
|||
async def type_search(typename:str=Query(description="类型名称")): |
|||
select_query="SELECT id,typename,descr FROM diarytypes WHERE 1=1 " |
|||
params=[] |
|||
if typename: |
|||
select_query+="AND typename LIKE %s" |
|||
params.append(f"%{typename}%") |
|||
type_query=fetch_all(select_query,params=params,fetchall=True) |
|||
return response_success(data=type_query,message="type search success") |
|||
|
|||
# 语录修改 |
|||
@router.put("/update/{id}") |
|||
async def type_put(type:Type,id: str = Path(description="类型id")): |
|||
update_query = ( |
|||
"UPDATE diarytypes SET typename=%s,descr=%s WHERE id=%s;" |
|||
) |
|||
update_data = (type.typename,type.descr,id) |
|||
execute_query(update_query, update_data) |
|||
return response_success("type update sucess") |
|||
|
|||
# 语录删除 |
|||
@router.delete("/delete/{id}") |
|||
async def type_del(id: str = Path(description="类型id")): |
|||
update_query = ( |
|||
"DELETE FROM diarytypes WHERE id=%s;" |
|||
) |
|||
update_data = (id) |
|||
execute_query(update_query, update_data) |
|||
return response_success() |
|||
|
|||
# 根据id查询语录 |
|||
@router.get("/list/search/{id}") |
|||
async def type_search_id(id:str=Path(description="类型id")): |
|||
select_query="SELECT * FROM diarytypes WHERE id=%s" |
|||
type_query=fetch_one(select_query,(id,)) |
|||
return response_success(data=type_query,message="type search success") |
Write
Preview
Loading…
Cancel
Save
Reference in new issue