50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
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()
|