|
@@ -23,42 +23,74 @@ class product_app(app_route_database):
|
|
|
self.__users_collection = users
|
|
self.__users_collection = users
|
|
|
|
|
|
|
|
def all(self) -> dict:
|
|
def all(self) -> dict:
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- return self.__collection(loader.load_all())
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__collection(loader.load_all())
|
|
|
|
|
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
+
|
|
|
def get_barcode(self, target: str) -> dict:
|
|
def get_barcode(self, target: str) -> dict:
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- return self.__single(loader.get_by_barcode(target))
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__single(loader.get_by_barcode(target))
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
def get_name(self, target: str) -> dict:
|
|
def get_name(self, target: str) -> dict:
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- return self.__single(loader.get_by_name(target))
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__single(loader.get_by_name(target))
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
def search_name(self, target: str) -> dict:
|
|
def search_name(self, target: str) -> dict:
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- return self.__collection(loader.search_by_name(target))
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__collection(loader.search_by_name(target))
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
def search_author(self, target: str) -> dict:
|
|
def search_author(self, target: str) -> dict:
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- return self.__collection(loader.search_by_author(target))
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__collection(loader.search_by_author(target))
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
def check_barcode(self, target: str) -> dict:
|
|
def check_barcode(self, target: str) -> dict:
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- return self.__exists(loader.barcode_in_use(target))
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__exists(loader.barcode_in_use(target))
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
def check_name(self, target: str) -> dict:
|
|
def check_name(self, target: str) -> dict:
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- return self.__exists(loader.name_in_use(target))
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ return self.__exists(loader.name_in_use(target))
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
def create(self, send: dict) -> dict:
|
|
def create(self, send: dict) -> dict:
|
|
|
- if not self.__logged_in(send):
|
|
|
|
|
- raise access_denied_exception()
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ if not self.__logged_in(send):
|
|
|
|
|
+ raise access_denied_exception()
|
|
|
|
|
+
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ target = product_builder().modify(send).result
|
|
|
|
|
+ result = loader.store(target)
|
|
|
|
|
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- target = product_builder().modify(send).result
|
|
|
|
|
- result = loader.store(target)
|
|
|
|
|
|
|
+ return self.__modify(result, "Can nod create product.")
|
|
|
|
|
|
|
|
- return self.__modify(result, "Can nod create product.")
|
|
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
def __logged_in(self, send: dict) -> bool:
|
|
def __logged_in(self, send: dict) -> bool:
|
|
|
if not "apikey" in send:
|
|
if not "apikey" in send:
|
|
@@ -66,7 +98,11 @@ class product_app(app_route_database):
|
|
|
|
|
|
|
|
return self.__users.get(send["apikey"]) is not None
|
|
return self.__users.get(send["apikey"]) is not None
|
|
|
|
|
|
|
|
- def __select_by_sended(self, send: dict) -> product | None:
|
|
|
|
|
|
|
+ def __select_by_sended(
|
|
|
|
|
+ self,
|
|
|
|
|
+ send: dict,
|
|
|
|
|
+ loader: product_loader
|
|
|
|
|
+ ) -> product | None:
|
|
|
barcode = None
|
|
barcode = None
|
|
|
name = None
|
|
name = None
|
|
|
|
|
|
|
@@ -82,45 +118,50 @@ class product_app(app_route_database):
|
|
|
|
|
|
|
|
if barcode is None and name is None:
|
|
if barcode is None and name is None:
|
|
|
content = "Give target_barcode or target_name"
|
|
content = "Give target_barcode or target_name"
|
|
|
- raise bar_request_exception(content)
|
|
|
|
|
|
|
+ raise bad_request_exception(content)
|
|
|
|
|
|
|
|
- with self.__product_database as laoder:
|
|
|
|
|
- result = None
|
|
|
|
|
|
|
+ result = None
|
|
|
|
|
|
|
|
- if barcode is not None:
|
|
|
|
|
- result = loader.get_by_barcode(barcode)
|
|
|
|
|
|
|
+ if barcode is not None:
|
|
|
|
|
+ result = loader.get_by_barcode(barcode)
|
|
|
|
|
|
|
|
- if name is not None:
|
|
|
|
|
- result = loader.get_by_name(name)
|
|
|
|
|
-
|
|
|
|
|
- if result is None:
|
|
|
|
|
- raise not_found_exception()
|
|
|
|
|
|
|
+ if name is not None:
|
|
|
|
|
+ result = loader.get_by_name(name)
|
|
|
|
|
+
|
|
|
|
|
+ if result is None:
|
|
|
|
|
+ raise not_found_exception()
|
|
|
|
|
|
|
|
- return result
|
|
|
|
|
|
|
+ return result
|
|
|
|
|
|
|
|
def update(self, send: dict) -> dict:
|
|
def update(self, send: dict) -> dict:
|
|
|
- if not self.__logged_in(send):
|
|
|
|
|
- raise access_denied_exception()
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ if not self.__logged_in(send):
|
|
|
|
|
+ raise access_denied_exception()
|
|
|
|
|
|
|
|
- target = self.__select_by_sended(send)
|
|
|
|
|
- updated = product_builder(target).modify(send).result
|
|
|
|
|
-
|
|
|
|
|
- with self.__products_database as loader:
|
|
|
|
|
- result = loader.store(updated)
|
|
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ target = self.__select_by_sended(send, loader)
|
|
|
|
|
+ updated = product_builder(target).modify(send).result
|
|
|
|
|
+ result = loader.store(updated)
|
|
|
|
|
|
|
|
- return self.__modify(result, "Can not update product.")
|
|
|
|
|
|
|
+ return self.__modify(result, "Can not update product.")
|
|
|
|
|
|
|
|
- def delete(self, send: dict) -> dict:
|
|
|
|
|
- if not self.__logged_in(send):
|
|
|
|
|
- raise access_denied_exception()
|
|
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
- target = self.__select_by_sended(send)
|
|
|
|
|
|
|
+ def delete(self, send: dict) -> dict:
|
|
|
|
|
+ try:
|
|
|
|
|
+ if not self.__logged_in(send):
|
|
|
|
|
+ raise access_denied_exception()
|
|
|
|
|
+
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ target = self.__select_by_sended(send, loader)
|
|
|
|
|
+ result = loader.drop(target)
|
|
|
|
|
+
|
|
|
|
|
+ return self.__modify(result, "Can not delete product.")
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
- with self.__product_database as loader:
|
|
|
|
|
- result = loader.drop(target)
|
|
|
|
|
-
|
|
|
|
|
- return self.__modify(result, "Can not delete product.")
|
|
|
|
|
-
|
|
|
|
|
def __modify(self, result: bool, cause: str) -> dict:
|
|
def __modify(self, result: bool, cause: str) -> dict:
|
|
|
if result:
|
|
if result:
|
|
|
return self._success()
|
|
return self._success()
|