product_builder.py 1.2 KB

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