Refactor streamers configs

This commit is contained in:
2024-11-17 02:12:07 +01:00
parent 3197a788f8
commit cb83a4f03f
9 changed files with 87 additions and 132 deletions

View File

@@ -0,0 +1,28 @@
from domain.streamers import StreamerConfig
from core.mongo import mongo_manager
class StreamerConfigRepository:
COLLECTION_NAME = "streamers"
@classmethod
async def get_by_twitch_id(cls, twitch_id: int) -> StreamerConfig:
async with mongo_manager.connect() as client:
db = client.get_default_database()
collection = db[cls.COLLECTION_NAME]
doc = await collection.find_one({"twitch.id": twitch_id})
if doc is None:
raise ValueError(f"Streamer with twitch id {twitch_id} not found")
return StreamerConfig(**doc)
@classmethod
async def all(cls) -> list[StreamerConfig]:
async with mongo_manager.connect() as client:
db = client.get_default_database()
collection = db[cls.COLLECTION_NAME]
cursor = await collection.find()
return [StreamerConfig(**doc) async for doc in cursor]