ShopBot/utils/database/market.py

48 lines
1.3 KiB
Python
Raw Normal View History

2022-06-04 22:02:51 +03:00
import typing as t
from .model import Catalog as Model
2022-06-05 15:02:41 +03:00
class Catalog:
2022-06-04 22:02:51 +03:00
@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
2022-06-16 00:32:17 +03:00
def get_catalog(cls, item_id:int = None, get_count:bool = False) -> t.Union[t.List[t.Any], t.Dict[t.Any, t.Any]]:
2022-06-04 22:02:51 +03:00
if item_id:
if get_count:
2022-06-05 15:02:41 +03:00
return cls.__get_item(item_id), Model.select().count()
return cls.__get_item(item_id)
2022-06-04 22:02:51 +03:00
items = []
for i in Model.select():
2022-06-05 15:02:41 +03:00
items.append(cls.__get_item(i.id))
2022-06-04 22:02:51 +03:00
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()