|
@@ -0,0 +1,93 @@
|
|
|
|
|
+import typing
|
|
|
|
|
+
|
|
|
|
|
+from .app_route import app_route
|
|
|
|
|
+from .product import product
|
|
|
|
|
+from .product import product_factory
|
|
|
|
|
+from .product_loader import product_loader
|
|
|
|
|
+from .product_response import product_response
|
|
|
|
|
+from .product_builder import product_builder
|
|
|
|
|
+
|
|
|
|
|
+class product_app(app_route):
|
|
|
|
|
+ def all(self) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__collection(loader.load_all())
|
|
|
|
|
+
|
|
|
|
|
+ def get_barcode(self, target: str) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__single(loader.get_by_barcode(target))
|
|
|
|
|
+
|
|
|
|
|
+ def get_name(self, target: str) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__single(loader.get_by_name(target))
|
|
|
|
|
+
|
|
|
|
|
+ def search_name(self, target: str) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__collection(loader.search_by_name(target))
|
|
|
|
|
+
|
|
|
|
|
+ def search_author(self, target: str) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__collection(loader.search_by_author(target))
|
|
|
|
|
+
|
|
|
|
|
+ def check_barcode(self, target: str) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__exists(loader.barcode_in_use(target))
|
|
|
|
|
+
|
|
|
|
|
+ def check_name(self, target: str) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__exists(loader.name_in_use(target))
|
|
|
|
|
+
|
|
|
|
|
+ def create(self, send: dict) -> dict:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ target = product_builder().modify(send).result
|
|
|
|
|
+ result = loader.store(target)
|
|
|
|
|
+
|
|
|
|
|
+ return self.__modify(result, "Can nod create product.")
|
|
|
|
|
+
|
|
|
|
|
+ def update(self, send: dict) -> dict:
|
|
|
|
|
+ barcode = send["target_barcode"] if "target_barcode" in send else None
|
|
|
|
|
+ name = send["target_name"] if "target_name" in send else None
|
|
|
|
|
+
|
|
|
|
|
+ if barcode is not None and name is not None:
|
|
|
|
|
+ return self._fail("Give only one, name OR by barcode.")
|
|
|
|
|
+
|
|
|
|
|
+ if barcode is None and name is None:
|
|
|
|
|
+ return self._fail("Identify target by name or barcode.")
|
|
|
|
|
+
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ if barcode is not None:
|
|
|
|
|
+ target = loader.get_by_barcode(barcode)
|
|
|
|
|
+ else:
|
|
|
|
|
+ target = loader.get_by_name(name)
|
|
|
|
|
+
|
|
|
|
|
+ if target is None:
|
|
|
|
|
+ return self._fail("Not found product to update.")
|
|
|
|
|
+
|
|
|
|
|
+ updated = product_builder(target).modify(send).result
|
|
|
|
|
+ result = loader.store(updated)
|
|
|
|
|
+
|
|
|
|
|
+ return self.__modify(result, "Can not update product.")
|
|
|
|
|
+
|
|
|
|
|
+ def delete(self, send: dict) -> dict:
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def __modify(self, result: bool, cause: str) -> dict:
|
|
|
|
|
+ if result:
|
|
|
|
|
+ return self._success()
|
|
|
|
|
+ else:
|
|
|
|
|
+ return self._fail(cause)
|
|
|
|
|
+
|
|
|
|
|
+ def __exists(self, result: bool) -> dict:
|
|
|
|
|
+ return self._success(exists = result)
|
|
|
|
|
+
|
|
|
|
|
+ def __single(self, target: product) -> dict:
|
|
|
|
|
+ if target is None:
|
|
|
|
|
+ return self._fail("Can not found product in database.")
|
|
|
|
|
+
|
|
|
|
|
+ return self.__success(product = product_response(target))
|
|
|
|
|
+
|
|
|
|
|
+ def __collection(self, target: typing.Iterable[product]) -> dict:
|
|
|
|
|
+ return self.__success(collection = product_response(target))
|
|
|
|
|
+
|
|
|
|
|
+ @property
|
|
|
|
|
+ def __products_database(self) -> product_loader:
|
|
|
|
|
+ return product_loader(self._connection)
|