crud.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 TenantModel
  6. from .schema import TenantCreateSchema, TenantOutSchema, TenantUpdateSchema
  7. class TenantCRUD(CRUDBase[TenantModel, TenantCreateSchema, TenantUpdateSchema]):
  8. """租户数据层"""
  9. def __init__(self, auth: AuthSchema) -> None:
  10. self.auth = auth
  11. super().__init__(model=TenantModel, auth=auth)
  12. async def get_by_id_crud(
  13. self, id: int, preload: list[str | Any] | None = None
  14. ) -> TenantModel | None:
  15. return await self.get(id=id, preload=preload)
  16. async def get_list_crud(
  17. self,
  18. search: dict | None = None,
  19. order_by: list[dict[str, str]] | None = None,
  20. preload: list[str | Any] | None = None,
  21. ) -> Sequence[TenantModel]:
  22. return await self.list(search=search, order_by=order_by, preload=preload)
  23. async def create_crud(self, data: TenantCreateSchema) -> TenantModel | None:
  24. return await self.create(data=data)
  25. async def update_crud(self, id: int, data: TenantUpdateSchema) -> TenantModel | None:
  26. return await self.update(id=id, data=data)
  27. async def delete_crud(self, ids: list[int]) -> None:
  28. await self.delete(ids=ids)
  29. async def set_available_crud(self, ids: list[int], status: str) -> None:
  30. await self.set(ids=ids, status=status)
  31. async def page_crud(
  32. self,
  33. offset: int,
  34. limit: int,
  35. order_by: list[dict[str, str]] | None,
  36. search: dict | None = None,
  37. out_schema: type[TenantOutSchema] | None = None,
  38. preload: list[str | Any] | None = None,
  39. ) -> dict:
  40. return await self.page(
  41. offset=offset,
  42. limit=limit,
  43. order_by=order_by or [{"id": "asc"}],
  44. search=search or {},
  45. out_schema=out_schema or TenantOutSchema,
  46. preload=preload or [],
  47. )