mirror of
https://github.com/kurbezz/discord-bot.git
synced 2026-03-03 22:00:48 +01:00
Update
This commit is contained in:
18
src/repositories/base.py
Normal file
18
src/repositories/base.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import abc
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from core.mongo import mongo_manager
|
||||
|
||||
|
||||
class BaseRepository(abc.ABC):
|
||||
COLLECTION_NAME: str
|
||||
|
||||
@asynccontextmanager
|
||||
@classmethod
|
||||
async def connect(cls):
|
||||
async with mongo_manager.connect() as client:
|
||||
db = client.get_default_database()
|
||||
collection = db[cls.COLLECTION_NAME]
|
||||
|
||||
yield collection
|
||||
@@ -1,17 +1,14 @@
|
||||
from domain.streamers import StreamerConfig
|
||||
|
||||
from core.mongo import mongo_manager
|
||||
from .base import BaseRepository
|
||||
|
||||
|
||||
class StreamerConfigRepository:
|
||||
class StreamerConfigRepository(BaseRepository):
|
||||
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]
|
||||
|
||||
async with cls.connect() as collection:
|
||||
doc = await collection.find_one({"twitch.id": twitch_id})
|
||||
if doc is None:
|
||||
raise ValueError(f"Streamer with twitch id {twitch_id} not found")
|
||||
@@ -34,10 +31,7 @@ class StreamerConfigRepository:
|
||||
"integrations.discord.games_list.channel_id"
|
||||
] = integration_discord_games_list_channel_id
|
||||
|
||||
async with mongo_manager.connect() as client:
|
||||
db = client.get_default_database()
|
||||
collection = db[cls.COLLECTION_NAME]
|
||||
|
||||
async with cls.connect() as collection:
|
||||
doc = await collection.find_one(filters)
|
||||
if doc is None:
|
||||
return None
|
||||
@@ -46,9 +40,6 @@ class StreamerConfigRepository:
|
||||
|
||||
@classmethod
|
||||
async def all(cls) -> list[StreamerConfig]:
|
||||
async with mongo_manager.connect() as client:
|
||||
db = client.get_default_database()
|
||||
collection = db[cls.COLLECTION_NAME]
|
||||
|
||||
async with cls.connect() as collection:
|
||||
cursor = await collection.find()
|
||||
return [StreamerConfig(**doc) async for doc in cursor]
|
||||
|
||||
29
src/repositories/users.py
Normal file
29
src/repositories/users.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from domain.users import CreateUser, User
|
||||
|
||||
from .base import BaseRepository
|
||||
|
||||
|
||||
class UserRepository(BaseRepository):
|
||||
COLLECTION_NAME = "users"
|
||||
|
||||
@classmethod
|
||||
async def get_or_create_user(cls, newUser: CreateUser) -> User:
|
||||
filter_data = {}
|
||||
|
||||
for provider, data in newUser.oauths.items():
|
||||
filter_data[f"oauths.{provider}.id"] = data.id
|
||||
|
||||
async with cls.connect() as collection:
|
||||
await collection.update_one(
|
||||
filter_data,
|
||||
{
|
||||
"$setOnInsert": {
|
||||
**newUser.model_dump(),
|
||||
}
|
||||
},
|
||||
upsert=True,
|
||||
)
|
||||
|
||||
user = await collection.find_one(filter_data)
|
||||
|
||||
return User(**user)
|
||||
Reference in New Issue
Block a user