Files
sdb2-backend/app/repositories/tts.py
JSC acdf191a5a
Some checks failed
Backend CI / lint (push) Failing after 10s
Backend CI / test (push) Failing after 1m36s
refactor: Improve code readability and structure across TTS modules
2025-09-21 19:07:32 +02:00

75 lines
1.8 KiB
Python

"""TTS repository for database operations."""
from collections.abc import Sequence
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import select
from app.models.tts import TTS
from app.repositories.base import BaseRepository
class TTSRepository(BaseRepository[TTS]):
"""Repository for TTS operations."""
def __init__(self, session: "AsyncSession") -> None:
"""Initialize TTS repository.
Args:
session: Database session for operations
"""
super().__init__(TTS, session)
async def get_by_user_id(
self,
user_id: int,
limit: int = 50,
offset: int = 0,
) -> Sequence[TTS]:
"""Get TTS records by user ID with pagination.
Args:
user_id: User ID to filter by
limit: Maximum number of records to return
offset: Number of records to skip
Returns:
List of TTS records
"""
stmt = (
select(self.model)
.where(self.model.user_id == user_id)
.order_by(self.model.created_at.desc())
.limit(limit)
.offset(offset)
)
result = await self.session.exec(stmt)
return result.all()
async def get_by_user_and_id(
self,
user_id: int,
tts_id: int,
) -> TTS | None:
"""Get a specific TTS record by user ID and TTS ID.
Args:
user_id: User ID to filter by
tts_id: TTS ID to retrieve
Returns:
TTS record if found and belongs to user, None otherwise
"""
stmt = select(self.model).where(
self.model.id == tts_id,
self.model.user_id == user_id,
)
result = await self.session.exec(stmt)
return result.first()