Files
FastapiAdmin/backend/app/plugin/module_example/demo01/crud.py
T
zhangtao b552b29ac5 feat: 重构代码生成模块并优化相关功能
- 重构代码生成模板,统一路由前缀和业务路径规范
- 优化数据库表分页查询性能,支持方言特定实现
- 新增同步数据库差异预览功能
- 调整字段配置界面,支持拖拽排序和批量设置
- 清理旧版示例代码和冗余配置
- 修复权限和路由嵌套路径处理问题
- 优化前端样式和交互细节
2026-04-03 00:43:50 +08:00

136 lines
3.6 KiB
Python

from collections.abc import Sequence
from app.api.v1.module_system.auth.schema import AuthSchema
from app.core.base_crud import CRUDBase
from .model import Demo01Model
from .schema import Demo01CreateSchema, Demo01OutSchema, Demo01UpdateSchema
class Demo01CRUD(CRUDBase[Demo01Model, Demo01CreateSchema, Demo01UpdateSchema]):
"""示例数据层"""
def __init__(self, auth: AuthSchema) -> None:
"""
初始化CRUD数据层
参数:
- auth (AuthSchema): 认证信息模型
"""
super().__init__(model=Demo01Model, auth=auth)
async def get_by_id_crud(self, id: int, preload: list[str] | None = None) -> Demo01Model | None:
"""
详情
参数:
- id (int): 示例ID
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
返回:
- Demo01Model | None: 示例模型实例或None
"""
return await self.get(id=id, preload=preload)
async def list_crud(
self,
search: dict | None = None,
order_by: list[dict] | None = None,
preload: list[str] | None = None,
) -> Sequence[Demo01Model]:
"""
列表查询
参数:
- search (dict | None): 查询参数
- order_by (list[dict] | None): 排序参数
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
返回:
- Sequence[Demo01Model]: 示例模型实例序列
"""
return await self.list(search=search, order_by=order_by, preload=preload)
async def create_crud(self, data: Demo01CreateSchema) -> Demo01Model | None:
"""
创建
参数:
- data (Demo01CreateSchema): 示例创建模型
返回:
- Demo01Model | None: 示例模型实例或None
"""
return await self.create(data=data)
async def update_crud(self, id: int, data: Demo01UpdateSchema) -> Demo01Model | None:
"""
更新
参数:
- id (int): 示例ID
- data (Demo01UpdateSchema): 示例更新模型
返回:
- Demo01Model | None: 示例模型实例或None
"""
return await self.update(id=id, data=data)
async def delete_crud(self, ids: list[int]) -> None:
"""
批量删除
参数:
- ids (List[int]): 示例ID列表
返回:
- None
"""
return await self.delete(ids=ids)
async def set_available_crud(self, ids: list[int], status: str) -> None:
"""
批量设置可用状态
参数:
- ids (list[int]): 示例ID列表
- status (str): 可用状态
返回:
- None
"""
return await self.set(ids=ids, status=status)
async def page_crud(
self,
offset: int,
limit: int,
order_by: list[dict] | None = None,
search: dict | None = None,
preload: list | None = None,
) -> dict:
"""
分页查询
参数:
- offset (int): 偏移量
- limit (int): 每页数量
- order_by (list[dict] | None): 排序参数
- search (dict | None): 查询参数
- preload (list | None): 预加载关系,未提供时使用模型默认项
返回:
- dict: 分页数据
"""
order_by_list = order_by or [{"id": "asc"}]
search_dict = search or {}
return await self.page(
offset=offset,
limit=limit,
order_by=order_by_list,
search=search_dict,
out_schema=Demo01OutSchema,
preload=preload,
)