crud.py 2.9 KB

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