crud.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from collections.abc import Sequence
  2. from datetime import datetime
  3. from typing import Any
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.plugin.module_payment.apikey.model import TenantApiKeyModel, TenantApiLogModel
  6. from app.plugin.module_payment.apikey.schema import (
  7. TenantApiKeyCreate,
  8. TenantApiKeyUpdate,
  9. TenantApiLogCreate,
  10. )
  11. from app.core.base_crud import CRUDBase
  12. class TenantApiKeyCRUD(CRUDBase[TenantApiKeyModel, TenantApiKeyCreate, TenantApiKeyUpdate]):
  13. """租户API Key数据层"""
  14. def __init__(self, auth: AuthSchema) -> None:
  15. self.auth = auth
  16. super().__init__(model=TenantApiKeyModel, auth=auth)
  17. async def get_by_id_crud(
  18. self, id: int, preload: list[str | Any] | None = None
  19. ) -> TenantApiKeyModel | None:
  20. return await self.get(id=id, preload=preload)
  21. async def get_by_api_key(self, api_key: str) -> TenantApiKeyModel | None:
  22. return await self.get(
  23. preload=[],
  24. api_key=api_key,
  25. status="0",
  26. # expired_at=("None", None),
  27. )
  28. async def create_crud(
  29. self,
  30. api_key: str,
  31. api_secret: str,
  32. tenant_id: int,
  33. expired_at: datetime,
  34. description: str | None = None,
  35. ) -> TenantApiKeyModel:
  36. data = {
  37. "tenant_id": tenant_id,
  38. "description": description,
  39. "api_key": api_key,
  40. "api_secret": api_secret,
  41. "expired_at": expired_at,
  42. }
  43. obj = await self.create(data=data, skip_tenant_id=True)
  44. return obj
  45. async def update_status_crud(self, api_key_id: int, status: str) -> TenantApiKeyModel | None:
  46. return await self.update(id=api_key_id, data={"status": status})
  47. async def update_last_used_crud(self, api_key_id: int) -> None:
  48. api_key_obj = await self.get_by_id_crud(api_key_id)
  49. if api_key_obj:
  50. api_key_obj.last_used_at = datetime.now()
  51. await self.auth.db.flush()
  52. async def delete_crud(self, api_key_id: int) -> None:
  53. await self.delete(ids=[api_key_id])
  54. class TenantApiLogCRUD(CRUDBase[TenantApiLogModel, TenantApiLogCreate, TenantApiLogCreate]):
  55. """租户API调用日志数据层"""
  56. def __init__(self, auth: AuthSchema) -> None:
  57. self.auth = auth
  58. super().__init__(model=TenantApiLogModel, auth=auth)
  59. async def create_crud(
  60. self,
  61. api_key_id: int | None,
  62. tenant_id: int,
  63. endpoint: str,
  64. method: str,
  65. request_ip: str,
  66. request_data: str | None,
  67. response_code: int,
  68. response_time: float,
  69. ) -> TenantApiLogModel:
  70. data = TenantApiLogCreate(
  71. api_key_id=api_key_id,
  72. tenant_id=tenant_id,
  73. endpoint=endpoint,
  74. method=method,
  75. request_ip=request_ip,
  76. request_data=request_data,
  77. response_code=response_code,
  78. response_time=response_time,
  79. )
  80. return await self.create(data=data, skip_tenant_id=True)