init
This commit is contained in:
39
database/account/account_data.py
Normal file
39
database/account/account_data.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from database.mongodb import get_collection
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
from utils.crypto import generate_combo_token
|
||||
|
||||
class AccountModel(BaseModel):
|
||||
id: int
|
||||
username: str
|
||||
token: str
|
||||
|
||||
account_collection = get_collection("accounts")
|
||||
|
||||
def find_account_by_uid(uid: int) -> Optional[AccountModel]:
|
||||
account_data = account_collection.find_one({"_id": uid})
|
||||
if account_data:
|
||||
account_data["id"] = int(account_data["_id"])
|
||||
del account_data["_id"]
|
||||
return AccountModel(**account_data)
|
||||
return None
|
||||
|
||||
def find_account_by_name(name: str) -> Optional[AccountModel]:
|
||||
account_data = account_collection.find_one({"username": name})
|
||||
if account_data:
|
||||
account_data["id"] = int(account_data["_id"])
|
||||
del account_data["_id"]
|
||||
return AccountModel(**account_data)
|
||||
return None
|
||||
|
||||
def create_new_account(username: str) -> AccountModel:
|
||||
last_account = account_collection.find_one(sort=[("_id", -1)])
|
||||
uid = (last_account["_id"] + 1) if last_account else 1001
|
||||
token = generate_combo_token(str(uid))
|
||||
new_account = {
|
||||
"_id": uid,
|
||||
"username": username,
|
||||
"token": token
|
||||
}
|
||||
account_collection.insert_one(new_account)
|
||||
return AccountModel(id=uid, username=username, token=new_account["token"])
|
||||
Reference in New Issue
Block a user