|
@@ -1,6 +1,8 @@
|
|
|
import typing
|
|
import typing
|
|
|
|
|
+import sqlalchemy
|
|
|
|
|
+import sqlalchemy.engine.base
|
|
|
|
|
|
|
|
-from .app_route import app_route
|
|
|
|
|
|
|
+from .app_route import app_route_database
|
|
|
from .product import product
|
|
from .product import product
|
|
|
from .product import product_factory
|
|
from .product import product_factory
|
|
|
from .product_loader import product_loader
|
|
from .product_loader import product_loader
|
|
@@ -8,8 +10,18 @@ from .product_response import product_response
|
|
|
from .product_builder import product_builder
|
|
from .product_builder import product_builder
|
|
|
from .exception import bad_request_exception
|
|
from .exception import bad_request_exception
|
|
|
from .exception import not_found_exception
|
|
from .exception import not_found_exception
|
|
|
|
|
+from .exception import access_denied_exception
|
|
|
|
|
+from .users_collection import users_collection
|
|
|
|
|
+
|
|
|
|
|
+class product_app(app_route_database):
|
|
|
|
|
+ def __init__(
|
|
|
|
|
+ self,
|
|
|
|
|
+ connection: sqlalchemy.engine.base.Engine,
|
|
|
|
|
+ users: users_collection
|
|
|
|
|
+ ) -> None:
|
|
|
|
|
+ super().__init__(connection)
|
|
|
|
|
+ self.__users_collection = users
|
|
|
|
|
|
|
|
-class product_app(app_route):
|
|
|
|
|
def all(self) -> dict:
|
|
def all(self) -> dict:
|
|
|
with self.__products_database as loader:
|
|
with self.__products_database as loader:
|
|
|
return self.__collection(loader.load_all())
|
|
return self.__collection(loader.load_all())
|
|
@@ -39,12 +51,21 @@ class product_app(app_route):
|
|
|
return self.__exists(loader.name_in_use(target))
|
|
return self.__exists(loader.name_in_use(target))
|
|
|
|
|
|
|
|
def create(self, send: dict) -> dict:
|
|
def create(self, send: dict) -> dict:
|
|
|
|
|
+ if not self.__logged_in(send):
|
|
|
|
|
+ raise access_denied_exception()
|
|
|
|
|
+
|
|
|
with self.__products_database as loader:
|
|
with self.__products_database as loader:
|
|
|
target = product_builder().modify(send).result
|
|
target = product_builder().modify(send).result
|
|
|
result = loader.store(target)
|
|
result = loader.store(target)
|
|
|
|
|
|
|
|
return self.__modify(result, "Can nod create product.")
|
|
return self.__modify(result, "Can nod create product.")
|
|
|
|
|
|
|
|
|
|
+ def __logged_in(self, send: dict) -> bool:
|
|
|
|
|
+ if not "apikey" in send:
|
|
|
|
|
+ return False
|
|
|
|
|
+
|
|
|
|
|
+ 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) -> product | None:
|
|
|
barcode = None
|
|
barcode = None
|
|
|
name = None
|
|
name = None
|
|
@@ -78,6 +99,9 @@ class product_app(app_route):
|
|
|
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()
|
|
|
|
|
+
|
|
|
target = self.__select_by_sended(send)
|
|
target = self.__select_by_sended(send)
|
|
|
updated = product_builder(target).modify(send).result
|
|
updated = product_builder(target).modify(send).result
|
|
|
|
|
|
|
@@ -87,6 +111,9 @@ class product_app(app_route):
|
|
|
return self.__modify(result, "Can not update product.")
|
|
return self.__modify(result, "Can not update product.")
|
|
|
|
|
|
|
|
def delete(self, send: dict) -> dict:
|
|
def delete(self, send: dict) -> dict:
|
|
|
|
|
+ if not self.__logged_in(send):
|
|
|
|
|
+ raise access_denied_exception()
|
|
|
|
|
+
|
|
|
target = self.__select_by_sended(send)
|
|
target = self.__select_by_sended(send)
|
|
|
|
|
|
|
|
with self.__product_database as loader:
|
|
with self.__product_database as loader:
|
|
@@ -112,6 +139,10 @@ class product_app(app_route):
|
|
|
def __collection(self, target: typing.Iterable[product]) -> dict:
|
|
def __collection(self, target: typing.Iterable[product]) -> dict:
|
|
|
return self._success(collection = product_response(target))
|
|
return self._success(collection = product_response(target))
|
|
|
|
|
|
|
|
|
|
+ @property
|
|
|
|
|
+ def __users(self) -> users_collection:
|
|
|
|
|
+ return self.__users_collection
|
|
|
|
|
+
|
|
|
@property
|
|
@property
|
|
|
def __products_database(self) -> product_loader:
|
|
def __products_database(self) -> product_loader:
|
|
|
return product_loader(self._connection)
|
|
return product_loader(self._connection)
|