Init commit
This commit is contained in:
30
utils/database/base.py
Normal file
30
utils/database/base.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from .model import Admin, Operator
|
||||
|
||||
|
||||
def get_admins():
|
||||
users = []
|
||||
for i in Admin.select():
|
||||
users.append(i.user_id)
|
||||
return users
|
||||
|
||||
|
||||
def get_operator():
|
||||
users = []
|
||||
for i in Operator.select():
|
||||
users.append(i.user_id)
|
||||
return users
|
||||
|
||||
def get_full_admin():
|
||||
usr = []
|
||||
for i in Admin.select():
|
||||
usr.append({
|
||||
"user_id": i.user_id,
|
||||
"first_name": i.first_name,
|
||||
"last_name": i.last_name,
|
||||
"username": i.username
|
||||
})
|
||||
return usr
|
||||
|
||||
|
||||
def del_admin(user_id:int):
|
||||
Admin.delete().where(Admin.user_id == user_id).execute()
|
23
utils/database/cart.py
Normal file
23
utils/database/cart.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from .model import Cart
|
||||
|
||||
|
||||
def add_to_cart(user_id:int, item_id: int):
|
||||
if Cart.select().where(Cart.user_id==user_id, Cart.product_id==item_id).exists():
|
||||
count = Cart.get(Cart.user_id==user_id, Cart.product_id==item_id).count
|
||||
Cart.update(count=count+1).where(Cart.user_id==user_id, Cart.product_id==item_id).execute()
|
||||
return
|
||||
Cart.insert(user_id=user_id, product_id=item_id, count=1).execute()
|
||||
|
||||
|
||||
def del_from_cart(user_id: int, item_id: int):
|
||||
Cart.delete().where(Cart.user_id==user_id, Cart.product_id==item_id).execute()
|
||||
|
||||
|
||||
def clean_cart(user_id: int):
|
||||
Cart.delete().where(Cart.user_id==user_id).execute()
|
||||
|
||||
def get_user_cart(user_id:int):
|
||||
cart = []
|
||||
for i in Cart.select().where(Cart.user_id == user_id):
|
||||
cart.append((i.product_id, i.count))
|
||||
return cart
|
49
utils/database/market.py
Normal file
49
utils/database/market.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from ast import Mod
|
||||
import typing as t
|
||||
|
||||
from .model import Catalog as Model
|
||||
|
||||
|
||||
class Catalog():
|
||||
@staticmethod
|
||||
def __get_item(item_id: int) -> dict:
|
||||
item = {}
|
||||
if item_id is not None:
|
||||
i = Model.get_or_none(Model.id == item_id)
|
||||
if i is not None:
|
||||
item.update(
|
||||
{
|
||||
"id": i.id,
|
||||
"name": i.name,
|
||||
"description": i.description,
|
||||
"price": i.price,
|
||||
"image": i.image
|
||||
}
|
||||
)
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
def get_catalog(self, item_id:int = None, get_count:bool = False) -> t.Union[list, dict]:
|
||||
if item_id:
|
||||
if get_count:
|
||||
return self.__get_item(item_id), Model.select().count()
|
||||
return self.__get_item(item_id)
|
||||
|
||||
items = []
|
||||
for i in Model.select():
|
||||
items.append(self.__get_item(i.id))
|
||||
return items
|
||||
|
||||
|
||||
@staticmethod
|
||||
def delete_post(item_id: int):
|
||||
return Model.delete_by_id(item_id)
|
||||
|
||||
|
||||
def add_item(name: str, description: str, price:t.Union[int, float], image:bytes, **kw):
|
||||
Model.insert(
|
||||
name=name,
|
||||
description=description,
|
||||
price=price,
|
||||
image=image
|
||||
).execute()
|
48
utils/database/model.py
Normal file
48
utils/database/model.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from peewee import (Model, BigIntegerField, TextField, BlobField,
|
||||
IntegerField, CharField, FloatField, ForeignKeyField)
|
||||
|
||||
from load import db
|
||||
|
||||
|
||||
class BaseModel(Model):
|
||||
'''Base model. Abstract Class'''
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
user_id = BigIntegerField(null=False, unique=True)
|
||||
first_name = CharField(null=False, max_length=64)
|
||||
last_name = CharField(null=True, max_length=64)
|
||||
username = CharField(null=True, max_length=32)
|
||||
|
||||
|
||||
class Admin(User):
|
||||
pass
|
||||
|
||||
|
||||
class Operator(User):
|
||||
pass
|
||||
|
||||
|
||||
class Catalog(BaseModel):
|
||||
name = TextField()
|
||||
description = TextField()
|
||||
image = BlobField()
|
||||
price = FloatField()
|
||||
|
||||
|
||||
class Cart(BaseModel):
|
||||
user_id = BigIntegerField()
|
||||
product_id = IntegerField()
|
||||
count = IntegerField(default=1)
|
||||
|
||||
|
||||
class UserInfo(BaseModel):
|
||||
user_id = BigIntegerField(null=False, unique=True)
|
||||
first_name = TextField()
|
||||
last_name = TextField()
|
||||
phone_number = CharField(15)
|
||||
address = TextField()
|
||||
|
||||
db.create_tables([Cart, Catalog, Operator, Admin, User, UserInfo])
|
23
utils/database/ordering.py
Normal file
23
utils/database/ordering.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from .model import UserInfo
|
||||
|
||||
|
||||
def save_info(user_id: int, last_name:str, first_name:str, phone_number:str, address:str, **kw):
|
||||
UserInfo.insert(
|
||||
user_id=user_id,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
phone_number=phone_number,
|
||||
address=address
|
||||
).on_conflict_replace().execute()
|
||||
|
||||
def get_info(user_id: int):
|
||||
output = None
|
||||
if UserInfo.select().where(UserInfo.user_id == user_id).exists():
|
||||
user = UserInfo.get(UserInfo.user_id == user_id)
|
||||
output = {
|
||||
"last_name": user.last_name,
|
||||
"first_name": user.first_name,
|
||||
"phone_number": user.phone_number,
|
||||
"address": user.address
|
||||
}
|
||||
return output
|
45
utils/database/user.py
Normal file
45
utils/database/user.py
Normal file
@@ -0,0 +1,45 @@
|
||||
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
|
||||
def __register_user(self, model: Model, user: UserType):
|
||||
model.insert(
|
||||
user_id=user.id,
|
||||
first_name=user.first_name,
|
||||
last_name=user.last_name,
|
||||
username=user.username
|
||||
).on_conflict_replace().execute()
|
||||
|
||||
@classmethod
|
||||
def register_user(self, user: UserType):
|
||||
return self.__register_user(UserModel, user)
|
||||
|
||||
@classmethod
|
||||
def register_admin(self, user: UserType):
|
||||
return self.__register_user(Admin, user)
|
||||
|
||||
@classmethod
|
||||
def register_operator(self, user: UserType):
|
||||
return self.__register_user(Operator, user)
|
||||
|
||||
|
||||
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
|
||||
)
|
Reference in New Issue
Block a user