29 lines
878 B
Python

from __future__ import annotations
from typing import Optional
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from models.log import Log
class LogRepository:
def __init__(self, session: AsyncSession):
self.session = session
async def list(self, limit: int = 100, offset: int = 0, level: Optional[str] = None, source: Optional[str] = None) -> list[Log]:
stmt = select(Log).order_by(Log.created_at.desc()).offset(offset).limit(limit)
if level:
stmt = stmt.where(Log.level == level)
if source:
stmt = stmt.where(Log.source == source)
result = await self.session.execute(stmt)
return result.scalars().all()
async def create(self, **fields) -> Log:
log = Log(**fields)
self.session.add(log)
await self.session.flush()
return log