You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from sqlalchemy.orm import Session
|
|
from app.db.models import Tool
|
|
|
|
|
|
class ToolRepository:
|
|
|
|
def __init__(self, db: Session):
|
|
"""Inicializa o repositorio com a sessao ativa do banco."""
|
|
self.db = db
|
|
|
|
def get_all(self):
|
|
"""Retorna todas as tools cadastradas."""
|
|
return self.db.query(Tool).all()
|
|
|
|
def get_by_id(self, tool_id: int):
|
|
"""Busca uma tool pelo ID."""
|
|
return self.db.query(Tool).filter(Tool.id == tool_id).first()
|
|
|
|
def create(self, name: str, description: str, parameters: dict):
|
|
"""Cria e persiste uma nova tool."""
|
|
tool = Tool(
|
|
name=name,
|
|
description=description,
|
|
parameters=parameters
|
|
)
|
|
self.db.add(tool)
|
|
self.db.commit()
|
|
self.db.refresh(tool)
|
|
return tool
|
|
|
|
def delete(self, tool_id: int):
|
|
"""Remove uma tool por ID quando existente."""
|
|
tool = self.get_by_id(tool_id)
|
|
if tool:
|
|
self.db.delete(tool)
|
|
self.db.commit()
|
|
return tool
|
|
|
|
def update_by_name(self, name: str, description: str, parameters: dict):
|
|
"""Atualiza descricao e parametros de uma tool encontrada pelo nome."""
|
|
tool = self.db.query(Tool).filter(Tool.name == name).first()
|
|
if not tool:
|
|
return None
|
|
|
|
tool.description = description
|
|
tool.parameters = parameters
|
|
self.db.commit()
|
|
self.db.refresh(tool)
|
|
return tool
|