ShopBot/utils/database/user.py

58 lines
1.7 KiB
Python
Raw Normal View History

2022-06-04 22:02:51 +03:00
from aiogram.types import User as UserType
from peewee import Model
from .model import User as UserModel
from .model import Admin, Operator
from utils import types
class Register:
@classmethod
2022-06-05 15:02:41 +03:00
def __register_user(cls, model: Model, user: UserType):
2022-06-04 22:02:51 +03:00
model.insert(
user_id=user.id,
first_name=user.first_name,
last_name=user.last_name,
username=user.username
).on_conflict_replace().execute()
@classmethod
2022-06-05 15:02:41 +03:00
def register_user(cls, user: UserType):
return cls.__register_user(UserModel, user)
2022-06-04 22:02:51 +03:00
@classmethod
2022-06-05 15:02:41 +03:00
def register_admin(cls, user: UserType):
return cls.__register_user(Admin, user)
2022-06-04 22:02:51 +03:00
@classmethod
2022-06-05 15:02:41 +03:00
def register_operator(cls, user: UserType):
return cls.__register_user(Operator, user)
2022-06-04 22:02:51 +03:00
class User:
@staticmethod
def get_user(user_id: int):
if not UserModel.select().where(UserModel.user_id==user_id).exists():
return None
data = UserModel.get(UserModel.user_id==user_id)
return types.User(
user_id=data.user_id,
first_name=data.first_name,
last_name=data.last_name,
username=data.username
)
2022-06-05 15:02:41 +03:00
@staticmethod
def get_state(user_id:int):
if Operator.select().where(Operator.user_id == user_id).exists():
return Operator.get(Operator.user_id == user_id).active
return False
@staticmethod
def set_state(user_id:int, state:bool=False):
if Operator.select().where(Operator.user_id == user_id).exists():
Operator.update(active=state).where(Operator.user_id == user_id).execute()
return None