crud.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from collections.abc import Sequence
  2. from typing import Any
  3. from app.api.v1.module_system.auth.schema import AuthSchema
  4. from app.core.base_crud import CRUDBase
  5. from .model import NodeModel
  6. from .schema import (
  7. NodeCreateSchema,
  8. NodeUpdateSchema,
  9. )
  10. class NodeCRUD(CRUDBase[NodeModel, NodeCreateSchema, NodeUpdateSchema]):
  11. """节点数据层"""
  12. def __init__(self, auth: AuthSchema) -> None:
  13. """
  14. 初始化节点CRUD
  15. 参数:
  16. - auth (AuthSchema): 认证信息模型
  17. """
  18. self.auth = auth
  19. super().__init__(model=NodeModel, auth=auth)
  20. async def get_obj_by_id_crud(
  21. self, id: int, preload: list[str | Any] | None = None
  22. ) -> NodeModel | None:
  23. """
  24. 获取节点详情
  25. 参数:
  26. - id (int): 节点ID
  27. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  28. 返回:
  29. - NodeModel | None: 节点模型,如果不存在则为None
  30. """
  31. return await self.get(id=id, preload=preload)
  32. async def get_obj_list_crud(
  33. self,
  34. search: dict | None = None,
  35. order_by: list[dict[str, str]] | None = None,
  36. preload: list[str | Any] | None = None,
  37. ) -> Sequence[NodeModel]:
  38. """
  39. 获取节点列表
  40. 参数:
  41. - search (dict | None): 查询参数字典
  42. - order_by (list[dict[str, str]] | None): 排序参数列表
  43. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  44. 返回:
  45. - Sequence[NodeModel]: 节点模型序列
  46. """
  47. return await self.list(search=search, order_by=order_by, preload=preload)
  48. async def create_obj_crud(self, data: NodeCreateSchema) -> NodeModel | None:
  49. """
  50. 创建节点
  51. 参数:
  52. - data (NodeCreateSchema): 创建节点模型
  53. 返回:
  54. - NodeModel | None: 创建的节点模型,如果创建失败则为None
  55. """
  56. return await self.create(data=data)
  57. async def update_obj_crud(self, id: int, data: NodeUpdateSchema) -> NodeModel | None:
  58. """
  59. 更新节点
  60. 参数:
  61. - id (int): 节点ID
  62. - data (NodeUpdateSchema): 更新节点模型
  63. 返回:
  64. - NodeModel | None: 更新后的节点模型,如果更新失败则为None
  65. """
  66. return await self.update(id=id, data=data)
  67. async def delete_obj_crud(self, ids: list[int]) -> None:
  68. """
  69. 删除节点
  70. 参数:
  71. - ids (list[int]): 节点ID列表
  72. 返回:
  73. - None
  74. """
  75. return await self.delete(ids=ids)
  76. async def set_obj_field_crud(self, ids: list[int], **kwargs) -> None:
  77. """
  78. 设置节点的可用状态
  79. 参数:
  80. - ids (list[int]): 节点ID列表
  81. - kwargs: 其他要设置的字段,例如 available=True 或 available=False
  82. 返回:
  83. - None
  84. """
  85. return await self.set(ids=ids, **kwargs)
  86. async def clear_obj_crud(self) -> None:
  87. """
  88. 清除节点日志
  89. 注意:
  90. - 此操作会删除所有节点日志,请谨慎操作
  91. 返回:
  92. - None
  93. """
  94. return await self.clear()