product_app.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import typing
  2. import sqlalchemy
  3. import sqlalchemy.engine.base
  4. from .app_route import app_route_database
  5. from .product import product
  6. from .product import product_factory
  7. from .product_loader import product_loader
  8. from .product_response import product_response
  9. from .product_builder import product_builder
  10. from .exception import bad_request_exception
  11. from .exception import not_found_exception
  12. from .exception import access_denied_exception
  13. from .users_collection import users_collection
  14. class product_app(app_route_database):
  15. def __init__(
  16. self,
  17. connection: sqlalchemy.engine.base.Engine,
  18. users: users_collection
  19. ) -> None:
  20. super().__init__(connection)
  21. self.__users_collection = users
  22. def all(self) -> dict:
  23. with self.__products_database as loader:
  24. return self.__collection(loader.load_all())
  25. def get_barcode(self, target: str) -> dict:
  26. with self.__products_database as loader:
  27. return self.__single(loader.get_by_barcode(target))
  28. def get_name(self, target: str) -> dict:
  29. with self.__products_database as loader:
  30. return self.__single(loader.get_by_name(target))
  31. def search_name(self, target: str) -> dict:
  32. with self.__products_database as loader:
  33. return self.__collection(loader.search_by_name(target))
  34. def search_author(self, target: str) -> dict:
  35. with self.__products_database as loader:
  36. return self.__collection(loader.search_by_author(target))
  37. def check_barcode(self, target: str) -> dict:
  38. with self.__products_database as loader:
  39. return self.__exists(loader.barcode_in_use(target))
  40. def check_name(self, target: str) -> dict:
  41. with self.__products_database as loader:
  42. return self.__exists(loader.name_in_use(target))
  43. def create(self, send: dict) -> dict:
  44. if not self.__logged_in(send):
  45. raise access_denied_exception()
  46. with self.__products_database as loader:
  47. target = product_builder().modify(send).result
  48. result = loader.store(target)
  49. return self.__modify(result, "Can nod create product.")
  50. def __logged_in(self, send: dict) -> bool:
  51. if not "apikey" in send:
  52. return False
  53. return self.__users.get(send["apikey"]) is not None
  54. def __select_by_sended(self, send: dict) -> product | None:
  55. barcode = None
  56. name = None
  57. if "target_barcode" in send:
  58. barcode = send["target_barcode"]
  59. if "target_name" in send:
  60. name = send["target_name"]
  61. if barcode is not None and name is not None:
  62. content = "Give only one, target_name or target_barcode"
  63. raise bad_request_exception(content)
  64. if barcode is None and name is None:
  65. content = "Give target_barcode or target_name"
  66. raise bar_request_exception(content)
  67. with self.__product_database as laoder:
  68. result = None
  69. if barcode is not None:
  70. result = loader.get_by_barcode(barcode)
  71. if name is not None:
  72. result = loader.get_by_name(name)
  73. if result is None:
  74. raise not_found_exception()
  75. return result
  76. def update(self, send: dict) -> dict:
  77. if not self.__logged_in(send):
  78. raise access_denied_exception()
  79. target = self.__select_by_sended(send)
  80. updated = product_builder(target).modify(send).result
  81. with self.__products_database as loader:
  82. result = loader.store(updated)
  83. return self.__modify(result, "Can not update product.")
  84. def delete(self, send: dict) -> dict:
  85. if not self.__logged_in(send):
  86. raise access_denied_exception()
  87. target = self.__select_by_sended(send)
  88. with self.__product_database as loader:
  89. result = loader.drop(target)
  90. return self.__modify(result, "Can not delete product.")
  91. def __modify(self, result: bool, cause: str) -> dict:
  92. if result:
  93. return self._success()
  94. else:
  95. return self._fail(cause)
  96. def __exists(self, result: bool) -> dict:
  97. return self._success(exists = result)
  98. def __single(self, target: product) -> dict:
  99. if target is None:
  100. return self._fail("Can not found product in database.")
  101. return self._success(product = product_response(target))
  102. def __collection(self, target: typing.Iterable[product]) -> dict:
  103. return self._success(collection = product_response(target))
  104. @property
  105. def __users(self) -> users_collection:
  106. return self.__users_collection
  107. @property
  108. def __products_database(self) -> product_loader:
  109. return product_loader(self._connection)