product_builder.py 992 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from .product import product
  2. from .product import product_factory
  3. class product_builder:
  4. def __init__(self, target: product | None = None) -> None:
  5. self.__target = target
  6. if self.__target is None:
  7. self.__target = product()
  8. def modify(self, target: dict) -> object:
  9. factory = product_factory(self.__target)
  10. if "name" in target:
  11. factory.name = target["name"]
  12. if "description" in target:
  13. factory.description = target["description"]
  14. if "author" in target:
  15. factory.author = target["author"]
  16. if "image" in target:
  17. factory.image = target["image"]
  18. if "stock_count" in target:
  19. factory.stock_count = int(target["stock_count"])
  20. if "barcode" in target:
  21. factory.barcode = target["barcode"]
  22. self.__target = factory.result
  23. return self
  24. @property
  25. def result(self) -> product:
  26. return self.__target