cixo 6 hónapja
szülő
commit
a913a41753

+ 3 - 0
assets/__init__.py

@@ -13,3 +13,6 @@ from .product import product
 from .product import product_factory
 from .product_loader import product_loader
 
+""" Apps and routes. """
+from .app_route import app_route
+from .product_app import product_app

+ 12 - 4
assets/exception.py

@@ -1,17 +1,25 @@
 class validator_exception(Exception):
-    def __init__(self, name: str):
+    def __init__(self, name: str) -> None:
         super().__init__("Invalid property names: " + name + ".")
 
 class exists_exception(Exception):
-    def __init__(self, name: str):
+    def __init__(self, name: str) -> None:
         super().__init__("Item with this " + name + " already in database.")
 
+class bad_request_exception(Exception):
+    def __init__(self, content: str) -> None:
+        super().__init__("Bad request: " + content + ".")
+
+class not_found_exception(Exception):
+    def __init__(self) -> None:
+        super().__init__("Not found required target.")
+
 class not_ready_exception(Exception):
-    def __init__(self, target: any):
+    def __init__(self, target: any) -> None:
         dump = str(target)
         what = str(type(target))
         
         info = "Can not work, because " + what + " is not raeady.\n"
         info = info + "Dump:\n" + what + "\n\n"
 
-        super().__init__(info)
+        super().__init__(info)

+ 2 - 2
assets/product.py

@@ -211,7 +211,7 @@ class product_factory:
     def image(self, target: str | None) -> None:
         """ This set image link of the product. """
 
-        self.__target.image
+        self.__target.image = target
 
     @property
     def stock_count(self) -> int:
@@ -226,4 +226,4 @@ class product_factory:
         if target < 0:
             raise validator_exception("product.stock_count")
 
-        self.__target.stock_count = target
+        self.__target.stock_count = target

+ 40 - 16
assets/product_app.py

@@ -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)

+ 3 - 3
assets/product_builder.py

@@ -9,7 +9,7 @@ class product_builder:
             self.__target = product()
 
     def modify(self, target: dict) -> object:
-        factory = product_factory(target)
+        factory = product_factory(self.__target)
 
         if "name" in target:
             factory.name = target["name"]
@@ -24,7 +24,7 @@ class product_builder:
             factory.image = target["image"]
 
         if "stock_count" in target:
-            factory.stock_count = target["stock_count"]
+            factory.stock_count = int(target["stock_count"])
 
         if "barcode" in target:
             factory.barcode = target["barcode"]
@@ -35,4 +35,4 @@ class product_builder:
 
     @property
     def result(self) -> product:
-        return self.__target
+        return self.__target

+ 8 - 8
assets/product_response.py

@@ -19,16 +19,16 @@ class product_response:
 
 
     def single(target: product) -> dict:
-        if not product.ready:
+        if not target.ready:
             raise not_ready_exception(target)
 
         return {
-            "name": product.name,
-            "description": product.description,
-            "author": product.author,
-            "image": product.image,
-            "stock_count": product.stock_count,
-            "barcode": product.barcode
+            "name": target.name,
+            "description": target.description,
+            "author": target.author,
+            "image": target.image,
+            "stock_count": target.stock_count,
+            "barcode": target.barcode
         }
 
     def collection(targets: typing.Iterable[product]) -> list:
@@ -40,4 +40,4 @@ class product_response:
 
             result.append(product_response.single(count))
 
-        return result
+        return result

+ 1 - 1
tests/002-product.py

@@ -81,4 +81,4 @@ with assets.product_loader(connection) as loader:
     print(loader.get_by_barcode("123456789012"))
     print()
 
-drop_database()
+drop_database()

+ 71 - 0
tests/003-product_app.py

@@ -0,0 +1,71 @@
+import pathlib
+
+current = pathlib.Path(__file__).parent
+root = current.parent
+
+import sys
+sys.path.append(str(root))
+
+import assets
+import sqlmodel
+
+def drop_database() -> None:
+    db = pathlib.Path("./003-product_app.db")
+
+    if db.is_file():
+        db.unlink()
+
+drop_database()
+
+connection = sqlmodel.create_engine("sqlite:///003-product_app.db")
+sqlmodel.SQLModel.metadata.create_all(connection)
+
+app = assets.product_app(connection)
+
+print("App initialized.")
+
+create = app.create({
+    "barcode": "123456789012",
+    "name": "Sample",
+    "description": "This is sample name.",
+    "author": "John Snow",
+    "image": "https://uuu.owo.pl",
+    "stock_count": "10"
+})
+
+print("Create:")
+print(create)
+print()
+
+select = app.get_barcode("123456789012")
+
+print("Select by barcode:")
+print(select)
+print()
+
+select = app.get_name("Sample")
+
+print("Select by name:")
+print(select)
+print()
+
+create = app.create({
+    "barcode": "210987654321",
+    "name": "Second sample",
+    "description": "This is sample item second.",
+    "author": "other",
+    "image": "https://test.pl",
+    "stock_count": "20"
+})
+
+print("Create second:")
+print(create)
+print()
+
+alls = app.all()
+
+print("Select all:")
+print(alls)
+print()
+
+drop_database()

+ 1 - 0
todo.md

@@ -0,0 +1 @@
+#