all tests passing

This commit is contained in:
phernandez
2024-12-08 00:39:51 -06:00
parent c71dd2cf0d
commit 49910d5507
10 changed files with 200 additions and 519 deletions
+19 -10
View File
@@ -3,10 +3,12 @@ from datetime import datetime, UTC
from pathlib import Path
from basic_memory.repository import EntityRepository
from basic_memory.schemas import EntityIn
from basic_memory.models import Entity
from basic_memory.schemas import EntityIn, ObservationIn
from basic_memory.models import Entity, Observation
from basic_memory.fileio import EntityNotFoundError
from . import ServiceError
class EntityService:
"""
Service for managing entities in the database.
@@ -18,28 +20,35 @@ class EntityService:
self.entity_repo = entity_repo
async def create_entity(self, entity: EntityIn) -> Entity:
"""Create a new entity in the database."""
# Create DB record
db_data = {
**entity.model_dump(),
"""Create a new entity in the database.
Note: ID is generated by the EntityIn validator before reaching this method.
"""
# Create base entity first
base_data = {
"id": entity.id, # Include the generated ID
"name": entity.name,
"entity_type": entity.entity_type,
"created_at": datetime.now(UTC),
}
return await self.entity_repo.create(db_data)
created_entity = await self.entity_repo.create(base_data)
await self.entity_repo.refresh(created_entity, ['observations', 'outgoing_relations', 'incoming_relations'])
return created_entity
async def get_entity(self, entity_id: str) -> Entity:
"""Get entity by ID."""
db_entity = await self.entity_repo.find_by_id(entity_id)
if not db_entity:
raise ServiceError(f"Entity not found: {entity_id}")
raise EntityNotFoundError(f"Entity not found: {entity_id}")
return db_entity
# TODO name is not uniaue
# TODO name is not unique
async def get_by_name(self, name: str) -> Entity:
"""Get entity by name."""
db_entity = await self.entity_repo.find_by_name(name)
if not db_entity:
raise ServiceError(f"Entity not found: {name}")
raise EntityNotFoundError(f"Entity not found: {name}")
return db_entity