feat: add_model_routes,简单的通用接口生成
This commit is contained in:
@@ -19,90 +19,3 @@ class LlmDemoModel(BasicModel, table=True):
|
|||||||
datetime_end: FormattedDatetime | None = Field(default=None, description="会员截止到期时间")
|
datetime_end: FormattedDatetime | None = Field(default=None, description="会员截止到期时间")
|
||||||
birthday: FormattedDate | None = Field(default=None, description="生日")
|
birthday: FormattedDate | None = Field(default=None, description="生日")
|
||||||
amount: FormattedDecimal | None = Field(default=Decimal(0), description="金额")
|
amount: FormattedDecimal | None = Field(default=Decimal(0), description="金额")
|
||||||
|
|
||||||
|
|
||||||
def add_llm_demo_route(app: FastAPI):
|
|
||||||
route_prefix = "/llm_demo"
|
|
||||||
router = APIRouter(prefix=route_prefix, tags=[route_prefix])
|
|
||||||
|
|
||||||
@router.post('/list')
|
|
||||||
async def _list(
|
|
||||||
session: AsyncSessionDep,
|
|
||||||
body: ModelQuerySchema = ModelQuerySchema(),
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
获取所有用户列表
|
|
||||||
"""
|
|
||||||
|
|
||||||
# 构建SQL查询执行对象
|
|
||||||
query = select(LlmDemoModel).order_by(
|
|
||||||
LlmDemoModel.created_at.desc()
|
|
||||||
)
|
|
||||||
# 计算偏移量(跳过前N条),并查询比一页多1条的记录(用于判断是否有下一页)
|
|
||||||
query = query.offset(body.page * body.page_size).limit(body.page_size + 1)
|
|
||||||
result = await session.execute(query)
|
|
||||||
query_cls_list: List[LlmDemoModel] = result.scalars().all()
|
|
||||||
has_next = len(query_cls_list) == body.page_size + 1
|
|
||||||
# 若有下一页,移除多查询的那一条记录
|
|
||||||
if has_next:
|
|
||||||
query_cls_list.pop()
|
|
||||||
return {
|
|
||||||
"list": query_cls_list,
|
|
||||||
"has_next": has_next
|
|
||||||
}
|
|
||||||
|
|
||||||
@router.post('/insert')
|
|
||||||
async def _insert(body: LlmDemoModel, session: AsyncSessionDep):
|
|
||||||
"""
|
|
||||||
插入用户
|
|
||||||
"""
|
|
||||||
if not body.id:
|
|
||||||
body.id = str(uuid.uuid4())
|
|
||||||
session.add(body)
|
|
||||||
await session.commit()
|
|
||||||
return body
|
|
||||||
|
|
||||||
@router.post('/update')
|
|
||||||
async def _update(body: LlmDemoModel, session: AsyncSessionDep):
|
|
||||||
"""
|
|
||||||
更新用户
|
|
||||||
"""
|
|
||||||
query = select(LlmDemoModel).where(LlmDemoModel.id == body.id)
|
|
||||||
result = await session.execute(query)
|
|
||||||
query_cls = result.scalars().first()
|
|
||||||
if not query_cls:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Update row with id:{body.id} not found")
|
|
||||||
|
|
||||||
update_data_exclude_true = body.model_dump(exclude_unset=True, exclude={'id'})
|
|
||||||
update_data_exclude_false = body.model_dump(exclude={'id'})
|
|
||||||
|
|
||||||
print(f"update_data_exclude_true: {update_data_exclude_true}")
|
|
||||||
print(f"update_data_exclude_false: {update_data_exclude_false}")
|
|
||||||
|
|
||||||
update_data = body.model_dump(exclude_unset=True, exclude={'id'})
|
|
||||||
for field, value in update_data.items():
|
|
||||||
setattr(query_cls, field, value)
|
|
||||||
session.add(query_cls)
|
|
||||||
await session.commit()
|
|
||||||
return query_cls
|
|
||||||
|
|
||||||
@router.post('/delete')
|
|
||||||
async def _delete(body: LlmDemoModel, session: AsyncSessionDep):
|
|
||||||
"""
|
|
||||||
更新用户
|
|
||||||
"""
|
|
||||||
query = select(LlmDemoModel).where(LlmDemoModel.id == body.id)
|
|
||||||
result = await session.execute(query)
|
|
||||||
query_cls = result.scalars().first()
|
|
||||||
if not query_cls:
|
|
||||||
return {"affect_row_count": 0}
|
|
||||||
await session.delete(query_cls)
|
|
||||||
await session.commit()
|
|
||||||
return {"affect_row_count": 1}
|
|
||||||
|
|
||||||
app.include_router(router)
|
|
||||||
|
|
||||||
|
|
||||||
class ModelQuerySchema(BaseModel):
|
|
||||||
page: int = Field(default=0, description="页码")
|
|
||||||
page_size: int = Field(default=5, description="每页数量")
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
from sqlmodel import Field
|
||||||
|
|
||||||
|
from app.model.BasicModel import BasicModel
|
||||||
|
from app.utils.model_utils import FormattedDatetime, FormattedDate, FormattedDecimal
|
||||||
|
|
||||||
|
|
||||||
|
class ResumeTemplateModel(BasicModel, table=True):
|
||||||
|
__tablename__ = "llm_resume_template"
|
||||||
|
|
||||||
|
source_code: str | None = Field(default=None, description="简历模板TSX代码")
|
||||||
|
thumb_image: str | None = Field(default=None, description="简历模板缩略图")
|
||||||
|
default_primary: str | None = Field(default=None, description="默认主题色")
|
||||||
|
default_secondary: str | None = Field(default=None, description="默认次级色")
|
||||||
+6
-2
@@ -4,7 +4,9 @@ from app.controller.add_docs_route import add_docs_route
|
|||||||
from app.controller.add_file_route import add_file_route
|
from app.controller.add_file_route import add_file_route
|
||||||
from app.controller.add_graph_proxy_route import add_graph_proxy_route
|
from app.controller.add_graph_proxy_route import add_graph_proxy_route
|
||||||
from app.controller.add_test_route import add_test_route
|
from app.controller.add_test_route import add_test_route
|
||||||
from app.model.LlmDemoMdel import add_llm_demo_route
|
from app.model.LlmDemoMdel import LlmDemoModel
|
||||||
|
from app.model.ResumeTemplateModel import ResumeTemplateModel
|
||||||
|
from app.utils.add_model_routes import add_model_routes
|
||||||
from app.utils.next_id import add_next_id_route
|
from app.utils.next_id import add_next_id_route
|
||||||
|
|
||||||
# /*@formatter:off*/
|
# /*@formatter:off*/
|
||||||
@@ -13,9 +15,11 @@ routes = [
|
|||||||
add_test_route, # 测试接口
|
add_test_route, # 测试接口
|
||||||
add_graph_proxy_route, # 代理自定义工作流
|
add_graph_proxy_route, # 代理自定义工作流
|
||||||
add_custom_stream_route, # 自定义流式接口
|
add_custom_stream_route, # 自定义流式接口
|
||||||
add_llm_demo_route, # LlmDemo 测试用户模块
|
|
||||||
add_complex_search_route, # 多条件组合查询案例
|
add_complex_search_route, # 多条件组合查询案例
|
||||||
add_next_id_route, # 生成ID接口
|
add_next_id_route, # 生成ID接口
|
||||||
add_file_route, # 文件上传接口
|
add_file_route, # 文件上传接口
|
||||||
|
|
||||||
|
lambda app: add_model_routes(app,LlmDemoModel,'/llm_demo'), # LlmDemo 测试用户模块
|
||||||
|
lambda app: add_model_routes(app,ResumeTemplateModel,'/llm_resume_template'), # 简历模板
|
||||||
]
|
]
|
||||||
# /*@formatter:on*/
|
# /*@formatter:on*/
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import uuid
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from fastapi import FastAPI, APIRouter, HTTPException
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from sqlmodel import select
|
||||||
|
|
||||||
|
from app.model.BasicModel import BasicModel
|
||||||
|
from app.utils.mysql_utils import AsyncSessionDep
|
||||||
|
from app.utils.path_join import path_join
|
||||||
|
|
||||||
|
|
||||||
|
def add_model_routes(app: FastAPI, clazz: type[BasicModel], route_prefix: str):
|
||||||
|
route_prefix = '/general' + route_prefix
|
||||||
|
router = APIRouter(prefix=route_prefix, tags=[route_prefix])
|
||||||
|
|
||||||
|
# 分页查询参数
|
||||||
|
class ListQuerySchema(BasicModel):
|
||||||
|
page: int | None = Field(default=0, description="页码")
|
||||||
|
page_size: int | None = Field(default=5, description="每页数量")
|
||||||
|
|
||||||
|
# 单条记录查询参数
|
||||||
|
class ItemQuerySchema(BaseModel):
|
||||||
|
id: str = Field(description="数据的ID")
|
||||||
|
|
||||||
|
# 新建接口参数类型
|
||||||
|
class InsertBodySchema(BaseModel):
|
||||||
|
row: clazz = Field(description="要新建的数据")
|
||||||
|
|
||||||
|
# 更新接口参数类型
|
||||||
|
class UpdateBodySchema(BaseModel):
|
||||||
|
row: clazz = Field(description="要更新的数据")
|
||||||
|
|
||||||
|
@router.post('/list')
|
||||||
|
async def _list(
|
||||||
|
session: AsyncSessionDep,
|
||||||
|
body: ListQuerySchema = ListQuerySchema(),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
获取所有用户列表
|
||||||
|
"""
|
||||||
|
|
||||||
|
# 构建SQL查询执行对象
|
||||||
|
query = select(clazz).order_by(clazz.created_at.desc())
|
||||||
|
# 计算偏移量(跳过前N条),并查询比一页多1条的记录(用于判断是否有下一页)
|
||||||
|
query = query.offset(body.page * body.page_size).limit(body.page_size + 1)
|
||||||
|
result = await session.execute(query)
|
||||||
|
query_cls_list: List[clazz] = result.scalars().all()
|
||||||
|
has_next = len(query_cls_list) == body.page_size + 1
|
||||||
|
# 若有下一页,移除多查询的那一条记录
|
||||||
|
if has_next:
|
||||||
|
query_cls_list.pop()
|
||||||
|
return {
|
||||||
|
"list": query_cls_list,
|
||||||
|
"has_next": has_next
|
||||||
|
}
|
||||||
|
|
||||||
|
@router.post('/item')
|
||||||
|
async def _item(body: ItemQuerySchema, session: AsyncSessionDep):
|
||||||
|
"""
|
||||||
|
获取用户详情
|
||||||
|
"""
|
||||||
|
query = select(clazz).where(clazz.id == body.id)
|
||||||
|
result = await session.execute(query)
|
||||||
|
query_cls = result.scalars().first()
|
||||||
|
return {"result": query_cls}
|
||||||
|
|
||||||
|
@router.post('/insert')
|
||||||
|
async def _insert(body: InsertBodySchema, session: AsyncSessionDep):
|
||||||
|
"""
|
||||||
|
插入用户
|
||||||
|
"""
|
||||||
|
new_obj = body.row
|
||||||
|
if not new_obj.id:
|
||||||
|
new_obj.id = str(uuid.uuid4())
|
||||||
|
session.add(new_obj)
|
||||||
|
await session.commit()
|
||||||
|
return new_obj
|
||||||
|
|
||||||
|
@router.post('/update')
|
||||||
|
async def _update(body: UpdateBodySchema, session: AsyncSessionDep):
|
||||||
|
"""
|
||||||
|
更新用户
|
||||||
|
"""
|
||||||
|
edit_obj = body.row
|
||||||
|
query = select(clazz).where(clazz.id == edit_obj.id)
|
||||||
|
result = await session.execute(query)
|
||||||
|
query_cls = result.scalars().first()
|
||||||
|
if not query_cls:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Update row with id:{edit_obj.id} not found")
|
||||||
|
|
||||||
|
update_data_exclude_true = edit_obj.model_dump(exclude_unset=True, exclude={'id'})
|
||||||
|
update_data_exclude_false = edit_obj.model_dump(exclude={'id'})
|
||||||
|
|
||||||
|
print(f"update_data_exclude_true: {update_data_exclude_true}")
|
||||||
|
print(f"update_data_exclude_false: {update_data_exclude_false}")
|
||||||
|
|
||||||
|
update_data = edit_obj.model_dump(exclude_unset=True, exclude={'id'})
|
||||||
|
for field, value in update_data.items():
|
||||||
|
setattr(query_cls, field, value)
|
||||||
|
session.add(query_cls)
|
||||||
|
await session.commit()
|
||||||
|
return query_cls
|
||||||
|
|
||||||
|
@router.post('/delete')
|
||||||
|
async def _delete(body: clazz, session: AsyncSessionDep):
|
||||||
|
"""
|
||||||
|
更新用户
|
||||||
|
"""
|
||||||
|
query = select(clazz).where(clazz.id == body.id)
|
||||||
|
result = await session.execute(query)
|
||||||
|
query_cls = result.scalars().first()
|
||||||
|
if not query_cls:
|
||||||
|
return {"affect_row_count": 0}
|
||||||
|
await session.delete(query_cls)
|
||||||
|
await session.commit()
|
||||||
|
return {"affect_row_count": 1}
|
||||||
|
|
||||||
|
app.include_router(router)
|
||||||
Reference in New Issue
Block a user