71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
|
|
"""Nexus — Script & ScriptExecution Repository (Async SQLAlchemy)"""
|
||
|
|
|
||
|
|
from typing import Optional, List
|
||
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from server.domain.models import Script, ScriptExecution
|
||
|
|
|
||
|
|
|
||
|
|
class ScriptRepositoryImpl:
|
||
|
|
def __init__(self, session: AsyncSession):
|
||
|
|
self.session = session
|
||
|
|
|
||
|
|
async def get_by_id(self, id: int) -> Optional[Script]:
|
||
|
|
result = await self.session.execute(select(Script).where(Script.id == id))
|
||
|
|
return result.scalar_one_or_none()
|
||
|
|
|
||
|
|
async def get_all(self) -> List[Script]:
|
||
|
|
result = await self.session.execute(select(Script).order_by(Script.id))
|
||
|
|
return list(result.scalars().all())
|
||
|
|
|
||
|
|
async def get_by_category(self, category: str) -> List[Script]:
|
||
|
|
result = await self.session.execute(
|
||
|
|
select(Script).where(Script.category == category).order_by(Script.id)
|
||
|
|
)
|
||
|
|
return list(result.scalars().all())
|
||
|
|
|
||
|
|
async def create(self, script: Script) -> Script:
|
||
|
|
self.session.add(script)
|
||
|
|
await self.session.commit()
|
||
|
|
await self.session.refresh(script)
|
||
|
|
return script
|
||
|
|
|
||
|
|
async def update(self, script: Script) -> Script:
|
||
|
|
await self.session.commit()
|
||
|
|
await self.session.refresh(script)
|
||
|
|
return script
|
||
|
|
|
||
|
|
async def delete(self, id: int) -> bool:
|
||
|
|
script = await self.get_by_id(id)
|
||
|
|
if script:
|
||
|
|
await self.session.delete(script)
|
||
|
|
await self.session.commit()
|
||
|
|
return True
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
class ScriptExecutionRepositoryImpl:
|
||
|
|
def __init__(self, session: AsyncSession):
|
||
|
|
self.session = session
|
||
|
|
|
||
|
|
async def get_by_id(self, id: int) -> Optional[ScriptExecution]:
|
||
|
|
result = await self.session.execute(
|
||
|
|
select(ScriptExecution).where(ScriptExecution.id == id)
|
||
|
|
)
|
||
|
|
return result.scalar_one_or_none()
|
||
|
|
|
||
|
|
async def create(self, execution: ScriptExecution) -> ScriptExecution:
|
||
|
|
self.session.add(execution)
|
||
|
|
await self.session.commit()
|
||
|
|
await self.session.refresh(execution)
|
||
|
|
return execution
|
||
|
|
|
||
|
|
async def update_status(self, id: int, status: str, results: str) -> ScriptExecution:
|
||
|
|
execution = await self.session.get(ScriptExecution, id)
|
||
|
|
if execution:
|
||
|
|
execution.status = status
|
||
|
|
execution.results = results
|
||
|
|
await self.session.commit()
|
||
|
|
await self.session.refresh(execution)
|
||
|
|
return execution
|