crud.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 NoticeModel
  5. from .schema import NoticeCreateSchema, NoticeUpdateSchema
  6. class NoticeCRUD(CRUDBase[NoticeModel, NoticeCreateSchema, NoticeUpdateSchema]):
  7. """公告数据层"""
  8. def __init__(self, auth: AuthSchema) -> None:
  9. """
  10. 初始化公告数据层。
  11. 参数:
  12. - auth (AuthSchema): 认证信息模型(含 DB 会话等上下文)。
  13. 返回:
  14. - None
  15. """
  16. self.auth = auth
  17. super().__init__(model=NoticeModel, auth=auth)
  18. async def get_by_id_crud(self, id: int, preload: list | None = None) -> NoticeModel | None:
  19. """
  20. 根据ID获取公告详情。
  21. 参数:
  22. - id (int): 公告ID。
  23. - preload (list | None): 预加载关系,未提供时使用模型默认项
  24. 返回:
  25. - NoticeModel | None: 公告模型实例。
  26. """
  27. return await self.get(id=id, preload=preload)
  28. async def get_list_crud(
  29. self,
  30. search: dict | None = None,
  31. order_by: list[dict] | None = None,
  32. preload: list | None = None,
  33. ) -> Sequence[NoticeModel]:
  34. """
  35. 获取公告列表。
  36. 参数:
  37. - search (dict | None): 查询参数。
  38. - order_by (list[dict] | None): 排序参数。
  39. - preload (list | None): 预加载关系,未提供时使用模型默认项
  40. 返回:
  41. - Sequence[NoticeModel]: 公告模型实例列表。
  42. """
  43. return await self.list(search=search, order_by=order_by, preload=preload)
  44. async def create_crud(self, data: NoticeCreateSchema) -> NoticeModel | None:
  45. """
  46. 创建公告。
  47. 参数:
  48. - data (NoticeCreateSchema): 公告创建模型。
  49. 返回:
  50. - NoticeModel | None: 公告模型实例。
  51. """
  52. return await self.create(data=data)
  53. async def update_crud(self, id: int, data: NoticeUpdateSchema) -> NoticeModel | None:
  54. """
  55. 更新公告。
  56. 参数:
  57. - id (int): 公告ID。
  58. - data (NoticeUpdateSchema): 公告更新模型。
  59. 返回:
  60. - NoticeModel | None: 公告模型实例。
  61. """
  62. return await self.update(id=id, data=data)
  63. async def delete_crud(self, ids: list[int]) -> None:
  64. """
  65. 删除公告。
  66. 参数:
  67. - ids (list[int]): 公告ID列表。
  68. 返回:
  69. - None
  70. """
  71. return await self.delete(ids=ids)
  72. async def set_available_crud(self, ids: list[int], status: str) -> None:
  73. """
  74. 设置公告的可用状态。
  75. 参数:
  76. - ids (list[int]): 公告ID列表。
  77. - status (str): 可用状态。
  78. 返回:
  79. - None
  80. """
  81. return await self.set(ids=ids, status=status)