2023-11-07 16:03:50 +03:00
|
|
|
from typing import Optional
|
|
|
|
from uuid import UUID
|
|
|
|
|
2023-12-06 10:40:18 +03:00
|
|
|
from models.settings import get_supabase_client
|
|
|
|
from modules.brain.dto.inputs import CreateApiBrainDefinition
|
|
|
|
from modules.brain.entity.api_brain_definition_entity import ApiBrainDefinitionEntity
|
|
|
|
from modules.brain.repository.interfaces import ApiBrainDefinitionsInterface
|
2023-11-07 16:03:50 +03:00
|
|
|
|
|
|
|
|
2023-12-06 10:40:18 +03:00
|
|
|
class ApiBrainDefinitions(ApiBrainDefinitionsInterface):
|
|
|
|
def __init__(self):
|
|
|
|
self.db = get_supabase_client()
|
2023-11-07 16:03:50 +03:00
|
|
|
|
2023-12-06 10:40:18 +03:00
|
|
|
def get_api_brain_definition(
|
|
|
|
self, brain_id: UUID
|
|
|
|
) -> Optional[ApiBrainDefinitionEntity]:
|
2023-11-07 16:03:50 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_brain_definition")
|
|
|
|
.select("*")
|
|
|
|
.filter("brain_id", "eq", brain_id)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
if len(response.data) == 0:
|
|
|
|
return None
|
|
|
|
|
2023-12-06 10:40:18 +03:00
|
|
|
return ApiBrainDefinitionEntity(**response.data[0])
|
2023-11-07 16:03:50 +03:00
|
|
|
|
|
|
|
def add_api_brain_definition(
|
2023-11-09 18:58:51 +03:00
|
|
|
self, brain_id: UUID, api_brain_definition: CreateApiBrainDefinition
|
2023-12-06 10:40:18 +03:00
|
|
|
) -> Optional[ApiBrainDefinitionEntity]:
|
2023-11-09 18:58:51 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_brain_definition")
|
|
|
|
.insert([{"brain_id": str(brain_id), **api_brain_definition.dict()}])
|
|
|
|
.execute()
|
2023-11-07 16:03:50 +03:00
|
|
|
)
|
|
|
|
if len(response.data) == 0:
|
|
|
|
return None
|
2023-12-06 10:40:18 +03:00
|
|
|
return ApiBrainDefinitionEntity(**response.data[0])
|
2023-11-07 16:03:50 +03:00
|
|
|
|
2023-11-22 13:15:14 +03:00
|
|
|
def update_api_brain_definition(
|
2023-12-06 10:40:18 +03:00
|
|
|
self, brain_id: UUID, api_brain_definition: ApiBrainDefinitionEntity
|
|
|
|
) -> Optional[ApiBrainDefinitionEntity]:
|
2023-11-22 13:15:14 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_brain_definition")
|
|
|
|
.update(api_brain_definition.dict(exclude={"brain_id"}))
|
|
|
|
.filter("brain_id", "eq", str(brain_id))
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
if len(response.data) == 0:
|
|
|
|
return None
|
2023-12-06 10:40:18 +03:00
|
|
|
return ApiBrainDefinitionEntity(**response.data[0])
|
2023-11-22 13:15:14 +03:00
|
|
|
|
2023-11-07 16:03:50 +03:00
|
|
|
def delete_api_brain_definition(self, brain_id: UUID) -> None:
|
|
|
|
self.db.table("api_brain_definition").delete().filter(
|
|
|
|
"brain_id", "eq", str(brain_id)
|
|
|
|
).execute()
|