ShopBot/utils/database/market.py
2022-06-05 15:02:41 +03:00

48 lines
1.3 KiB
Python

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(cls, item_id:int = None, get_count:bool = False) -> t.Union[list, dict]:
if item_id:
if get_count:
return cls.__get_item(item_id), Model.select().count()
return cls.__get_item(item_id)
items = []
for i in Model.select():
items.append(cls.__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()