Пример #1
0
 def save(cls,
          name: str = None,
          price: int = None,
          category: str = None,
          off: float = None) -> bool:
     new_category = Category.get_one_category(category)
     if new_category:
         g = {
             "id": next(Product.__id),
             "name": name,
             "price": int(price),
             "category_id": new_category.get("id"),
             "off": float(off)
         }
         cls.__db.append(g)
         return True
     else:
         return False
Пример #2
0
 def update(cls, _id: int, new_name: str, new_price: int, new_category: str,
            new_off: float) -> (bool, str):
     if new_category:
         if not Category().exist(new_category):
             return False, f"category [{new_category}] not found please first add new category"
     if _id:
         for product in cls.__db:
             if product.get("id") == int(_id):
                 if new_name:
                     product["name"] = new_name
                 if new_price:
                     product["price"] = int(new_price)
                 if new_category:
                     ca = Category.get_one_category(new_category)
                     product["category_id"] = ca.get("id")
                 if new_off:
                     product["off"] = float(new_off)
                 return True, ""
         return False, f"id: {_id} not found"