product_app.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. from .exception import bad_request_exception
  9. from .exception import not_found_exception
  10. class product_app(app_route):
  11. def all(self) -> dict:
  12. with self.__products_database as loader:
  13. return self.__collection(loader.load_all())
  14. def get_barcode(self, target: str) -> dict:
  15. with self.__products_database as loader:
  16. return self.__single(loader.get_by_barcode(target))
  17. def get_name(self, target: str) -> dict:
  18. with self.__products_database as loader:
  19. return self.__single(loader.get_by_name(target))
  20. def search_name(self, target: str) -> dict:
  21. with self.__products_database as loader:
  22. return self.__collection(loader.search_by_name(target))
  23. def search_author(self, target: str) -> dict:
  24. with self.__products_database as loader:
  25. return self.__collection(loader.search_by_author(target))
  26. def check_barcode(self, target: str) -> dict:
  27. with self.__products_database as loader:
  28. return self.__exists(loader.barcode_in_use(target))
  29. def check_name(self, target: str) -> dict:
  30. with self.__products_database as loader:
  31. return self.__exists(loader.name_in_use(target))
  32. def create(self, send: dict) -> dict:
  33. with self.__products_database as loader:
  34. target = product_builder().modify(send).result
  35. result = loader.store(target)
  36. return self.__modify(result, "Can nod create product.")
  37. def __select_by_sended(self, send: dict) -> product | None:
  38. barcode = None
  39. name = None
  40. if "target_barcode" in send:
  41. barcode = send["target_barcode"]
  42. if "target_name" in send:
  43. name = send["target_name"]
  44. if barcode is not None and name is not None:
  45. content = "Give only one, target_name or target_barcode"
  46. raise bad_request_exception(content)
  47. if barcode is None and name is None:
  48. content = "Give target_barcode or target_name"
  49. raise bar_request_exception(content)
  50. with self.__product_database as laoder:
  51. result = None
  52. if barcode is not None:
  53. result = loader.get_by_barcode(barcode)
  54. if name is not None:
  55. result = loader.get_by_name(name)
  56. if result is None:
  57. raise not_found_exception()
  58. return result
  59. def update(self, send: dict) -> dict:
  60. target = self.__select_by_sended(send)
  61. updated = product_builder(target).modify(send).result
  62. with self.__products_database as loader:
  63. result = loader.store(updated)
  64. return self.__modify(result, "Can not update product.")
  65. def delete(self, send: dict) -> dict:
  66. target = self.__select_by_sended(send)
  67. with self.__product_database as loader:
  68. result = loader.drop(target)
  69. return self.__modify(result, "Can not delete product.")
  70. def __modify(self, result: bool, cause: str) -> dict:
  71. if result:
  72. return self._success()
  73. else:
  74. return self._fail(cause)
  75. def __exists(self, result: bool) -> dict:
  76. return self._success(exists = result)
  77. def __single(self, target: product) -> dict:
  78. if target is None:
  79. return self._fail("Can not found product in database.")
  80. return self._success(product = product_response(target))
  81. def __collection(self, target: typing.Iterable[product]) -> dict:
  82. return self._success(collection = product_response(target))
  83. @property
  84. def __products_database(self) -> product_loader:
  85. return product_loader(self._connection)