product_app.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import typing
  2. from .app_route import app_route
  3. from .product import product
  4. from .product import product_factory
  5. from .product_loader import product_loader
  6. from .product_response import product_response
  7. from .product_builder import product_builder
  8. class product_app(app_route):
  9. def all(self) -> dict:
  10. with self.__products_database as loader:
  11. return self.__collection(loader.load_all())
  12. def get_barcode(self, target: str) -> dict:
  13. with self.__products_database as loader:
  14. return self.__single(loader.get_by_barcode(target))
  15. def get_name(self, target: str) -> dict:
  16. with self.__products_database as loader:
  17. return self.__single(loader.get_by_name(target))
  18. def search_name(self, target: str) -> dict:
  19. with self.__products_database as loader:
  20. return self.__collection(loader.search_by_name(target))
  21. def search_author(self, target: str) -> dict:
  22. with self.__products_database as loader:
  23. return self.__collection(loader.search_by_author(target))
  24. def check_barcode(self, target: str) -> dict:
  25. with self.__products_database as loader:
  26. return self.__exists(loader.barcode_in_use(target))
  27. def check_name(self, target: str) -> dict:
  28. with self.__products_database as loader:
  29. return self.__exists(loader.name_in_use(target))
  30. def create(self, send: dict) -> dict:
  31. with self.__products_database as loader:
  32. target = product_builder().modify(send).result
  33. result = loader.store(target)
  34. return self.__modify(result, "Can nod create product.")
  35. def update(self, send: dict) -> dict:
  36. barcode = send["target_barcode"] if "target_barcode" in send else None
  37. name = send["target_name"] if "target_name" in send else None
  38. if barcode is not None and name is not None:
  39. return self._fail("Give only one, name OR by barcode.")
  40. if barcode is None and name is None:
  41. return self._fail("Identify target by name or barcode.")
  42. with self.__products_database as loader:
  43. if barcode is not None:
  44. target = loader.get_by_barcode(barcode)
  45. else:
  46. target = loader.get_by_name(name)
  47. if target is None:
  48. return self._fail("Not found product to update.")
  49. updated = product_builder(target).modify(send).result
  50. result = loader.store(updated)
  51. return self.__modify(result, "Can not update product.")
  52. def delete(self, send: dict) -> dict:
  53. def __modify(self, result: bool, cause: str) -> dict:
  54. if result:
  55. return self._success()
  56. else:
  57. return self._fail(cause)
  58. def __exists(self, result: bool) -> dict:
  59. return self._success(exists = result)
  60. def __single(self, target: product) -> dict:
  61. if target is None:
  62. return self._fail("Can not found product in database.")
  63. return self.__success(product = product_response(target))
  64. def __collection(self, target: typing.Iterable[product]) -> dict:
  65. return self.__success(collection = product_response(target))
  66. @property
  67. def __products_database(self) -> product_loader:
  68. return product_loader(self._connection)