|
|
@@ -6,6 +6,8 @@ from .product import product_factory
|
|
|
from .product_loader import product_loader
|
|
|
from .product_response import product_response
|
|
|
from .product_builder import product_builder
|
|
|
+from .exception import bad_request_exception
|
|
|
+from .exception import not_found_exception
|
|
|
|
|
|
class product_app(app_route):
|
|
|
def all(self) -> dict:
|
|
|
@@ -43,33 +45,55 @@ class product_app(app_route):
|
|
|
|
|
|
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
|
|
|
+ def __select_by_sended(self, send: dict) -> product | None:
|
|
|
+ barcode = None
|
|
|
+ name = None
|
|
|
+
|
|
|
+ if "target_barcode" in send:
|
|
|
+ barcode = send["target_barcode"]
|
|
|
+
|
|
|
+ if "target_name" in send:
|
|
|
+ name = send["target_name"]
|
|
|
|
|
|
if barcode is not None and name is not None:
|
|
|
- return self._fail("Give only one, name OR by barcode.")
|
|
|
+ content = "Give only one, target_name or target_barcode"
|
|
|
+ raise bad_request_exception(content)
|
|
|
|
|
|
if barcode is None and name is None:
|
|
|
- return self._fail("Identify target by name or barcode.")
|
|
|
+ content = "Give target_barcode or target_name"
|
|
|
+ raise bar_request_exception(content)
|
|
|
+
|
|
|
+ with self.__product_database as laoder:
|
|
|
+ result = None
|
|
|
|
|
|
- with self.__products_database as loader:
|
|
|
if barcode is not None:
|
|
|
- target = loader.get_by_barcode(barcode)
|
|
|
- else:
|
|
|
- target = loader.get_by_name(name)
|
|
|
+ result = loader.get_by_barcode(barcode)
|
|
|
|
|
|
- if target is None:
|
|
|
- return self._fail("Not found product to update.")
|
|
|
+ if name is not None:
|
|
|
+ result = loader.get_by_name(name)
|
|
|
+
|
|
|
+ if result is None:
|
|
|
+ raise not_found_exception()
|
|
|
|
|
|
- updated = product_builder(target).modify(send).result
|
|
|
+ return result
|
|
|
+
|
|
|
+ def update(self, send: dict) -> dict:
|
|
|
+ target = self.__select_by_sended(send)
|
|
|
+ updated = product_builder(target).modify(send).result
|
|
|
+
|
|
|
+ with self.__products_database as loader:
|
|
|
result = loader.store(updated)
|
|
|
|
|
|
return self.__modify(result, "Can not update product.")
|
|
|
|
|
|
def delete(self, send: dict) -> dict:
|
|
|
+ target = self.__select_by_sended(send)
|
|
|
|
|
|
-
|
|
|
+ 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:
|
|
|
if result:
|
|
|
return self._success()
|
|
|
@@ -83,11 +107,11 @@ class product_app(app_route):
|
|
|
if target is None:
|
|
|
return self._fail("Can not found product in database.")
|
|
|
|
|
|
- return self.__success(product = product_response(target))
|
|
|
+ return self._success(product = product_response(target))
|
|
|
|
|
|
def __collection(self, target: typing.Iterable[product]) -> dict:
|
|
|
- return self.__success(collection = product_response(target))
|
|
|
+ return self._success(collection = product_response(target))
|
|
|
|
|
|
@property
|
|
|
def __products_database(self) -> product_loader:
|
|
|
- return product_loader(self._connection)
|
|
|
+ return product_loader(self._connection)
|