crud.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from collections.abc import Sequence
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.core.base_crud import CRUDBase
  4. from .model import Demo01Model
  5. from .schema import Demo01CreateSchema, Demo01OutSchema, Demo01UpdateSchema
  6. class Demo01CRUD(CRUDBase[Demo01Model, Demo01CreateSchema, Demo01UpdateSchema]):
  7. """示例数据层"""
  8. def __init__(self, auth: AuthSchema) -> None:
  9. """
  10. 初始化CRUD数据层
  11. 参数:
  12. - auth (AuthSchema): 认证信息模型
  13. """
  14. super().__init__(model=Demo01Model, auth=auth)
  15. async def get_by_id_crud(self, id: int, preload: list[str] | None = None) -> Demo01Model | None:
  16. """
  17. 详情
  18. 参数:
  19. - id (int): 示例ID
  20. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  21. 返回:
  22. - Demo01Model | None: 示例模型实例或None
  23. """
  24. return await self.get(id=id, preload=preload)
  25. async def list_crud(
  26. self,
  27. search: dict | None = None,
  28. order_by: list[dict] | None = None,
  29. preload: list[str] | None = None,
  30. ) -> Sequence[Demo01Model]:
  31. """
  32. 列表查询
  33. 参数:
  34. - search (dict | None): 查询参数
  35. - order_by (list[dict] | None): 排序参数
  36. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  37. 返回:
  38. - Sequence[Demo01Model]: 示例模型实例序列
  39. """
  40. return await self.list(search=search, order_by=order_by, preload=preload)
  41. async def create_crud(self, data: Demo01CreateSchema) -> Demo01Model | None:
  42. """
  43. 创建
  44. 参数:
  45. - data (Demo01CreateSchema): 示例创建模型
  46. 返回:
  47. - Demo01Model | None: 示例模型实例或None
  48. """
  49. return await self.create(data=data)
  50. async def update_crud(self, id: int, data: Demo01UpdateSchema) -> Demo01Model | None:
  51. """
  52. 更新
  53. 参数:
  54. - id (int): 示例ID
  55. - data (Demo01UpdateSchema): 示例更新模型
  56. 返回:
  57. - Demo01Model | None: 示例模型实例或None
  58. """
  59. return await self.update(id=id, data=data)
  60. async def delete_crud(self, ids: list[int]) -> None:
  61. """
  62. 批量删除
  63. 参数:
  64. - ids (List[int]): 示例ID列表
  65. 返回:
  66. - None
  67. """
  68. return await self.delete(ids=ids)
  69. async def set_available_crud(self, ids: list[int], status: str) -> None:
  70. """
  71. 批量设置可用状态
  72. 参数:
  73. - ids (list[int]): 示例ID列表
  74. - status (str): 可用状态
  75. 返回:
  76. - None
  77. """
  78. return await self.set(ids=ids, status=status)
  79. async def page_crud(
  80. self,
  81. offset: int,
  82. limit: int,
  83. order_by: list[dict] | None = None,
  84. search: dict | None = None,
  85. preload: list | None = None,
  86. ) -> dict:
  87. """
  88. 分页查询
  89. 参数:
  90. - offset (int): 偏移量
  91. - limit (int): 每页数量
  92. - order_by (list[dict] | None): 排序参数
  93. - search (dict | None): 查询参数
  94. - preload (list | None): 预加载关系,未提供时使用模型默认项
  95. 返回:
  96. - dict: 分页数据
  97. """
  98. order_by_list = order_by or [{"id": "asc"}]
  99. search_dict = search or {}
  100. return await self.page(
  101. offset=offset,
  102. limit=limit,
  103. order_by=order_by_list,
  104. search=search_dict,
  105. out_schema=Demo01OutSchema,
  106. preload=preload,
  107. )