mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-06 15:15:37 +01:00
Update
This commit is contained in:
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