From 0121f3fcd01307a0635f24355a28d5fc49b8a6eb Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Fri, 28 Feb 2025 17:47:57 +0700 Subject: [PATCH] Update item class to use sku as primary key of item --- bruno/item/Item Add.bru | 5 ++++- bruno/item/Item Detail.bru | 2 +- bruno/item/Item Edit.bru | 2 +- bruno/item/Item Remove.bru | 4 +--- modules/api/item.py | 33 +++++++++++++++++---------------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/bruno/item/Item Add.bru b/bruno/item/Item Add.bru index 11a76f7..44a7cba 100644 --- a/bruno/item/Item Add.bru +++ b/bruno/item/Item Add.bru @@ -11,5 +11,8 @@ post { } body:json { - {"name":"Card holder wallet"} + { + "sku":"TEST-001", + "name":"Card holder wallet" + } } diff --git a/bruno/item/Item Detail.bru b/bruno/item/Item Detail.bru index 93108ac..f84ddab 100644 --- a/bruno/item/Item Detail.bru +++ b/bruno/item/Item Detail.bru @@ -11,5 +11,5 @@ post { } body:json { - {"id":2} + {"sku":"TEST-001"} } diff --git a/bruno/item/Item Edit.bru b/bruno/item/Item Edit.bru index f68f650..bea58a6 100644 --- a/bruno/item/Item Edit.bru +++ b/bruno/item/Item Edit.bru @@ -12,7 +12,7 @@ post { body:json { { - "id":3, + "sku":"TEST-001", "name":"Card holder" } } diff --git a/bruno/item/Item Remove.bru b/bruno/item/Item Remove.bru index 65d4082..501dcce 100644 --- a/bruno/item/Item Remove.bru +++ b/bruno/item/Item Remove.bru @@ -11,7 +11,5 @@ post { } body:json { - { - "id":3 - } + {"sku":"TEST-001"} } diff --git a/modules/api/item.py b/modules/api/item.py index 4e302e3..67785a3 100644 --- a/modules/api/item.py +++ b/modules/api/item.py @@ -20,16 +20,16 @@ class item: c1 = 0 for d1 in l1: ls.append({ - "id" :d1["id" ], + "sku" :d1["sku" ], "name" :d1["name" ], "price" :{ "buy":[], "sell":[] } }) - self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `item` = %s AND `type`='buy' ORDER BY periods DESC, id DESC; ", ( d1["id"], ) ) + self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `sku` = %s AND `type`='buy' ORDER BY periods DESC, id DESC; ", ( d1["sku"], ) ) ls[c1]["price"]["buy" ] = self.cursor.fetchall() - self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `item` = %s AND `type`='sell' ORDER BY periods DESC, id DESC; ", ( d1["id"], ) ) + self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `sku` = %s AND `type`='sell' ORDER BY periods DESC, id DESC; ", ( d1["sku"], ) ) ls[c1]["price"]["sell"] = self.cursor.fetchall() c1+=1 loggorilla.prcss(APIADDR, "Giving response") @@ -52,22 +52,22 @@ class item: APIADDR = "/api/invlab/item/detail" response = {} loggorilla.prcss(APIADDR, "Define parameters") - id = params["id"] + sku = params["sku"] self.cursor.execute("BEGIN;") try: - self.cursor.execute("SELECT * FROM `item` WHERE `id`= %s ;", ( id, ) ) + self.cursor.execute("SELECT * FROM `item` WHERE `sku` = %s ;", ( sku, ) ) r = self.cursor.fetchone() data = { - "id" :r["id" ], + "sku" :r["sku" ], "name" :r["name" ], "price" :{ "buy":[], "sell":[] } } - self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `item` = %s AND `type`='buy' ORDER BY periods DESC, id DESC; ", ( r["id"], ) ) + self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `sku` = %s AND `type`='buy' ORDER BY periods DESC, id DESC; ", ( sku, ) ) data["price"]["buy" ] = self.cursor.fetchall() - self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `item` = %s AND `type`='sell' ORDER BY periods DESC, id DESC; ", ( r["id"], ) ) + self.cursor.execute("SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` from item_price WHERE `sku` = %s AND `type`='sell' ORDER BY periods DESC, id DESC; ", ( sku, ) ) data["price"]["sell"] = self.cursor.fetchall() loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" @@ -89,11 +89,12 @@ class item: APIADDR = "/api/invlab/item/add" response = {} loggorilla.prcss(APIADDR, "Define parameters") - name = params["name" ] + sku = params["sku" ] + name = params["name" ] self.cursor.execute("BEGIN;") try: loggorilla.prcss(APIADDR, "Inserting") - self.cursor.execute("INSERT INTO `item` VALUES (DEFAULT, %s) ;", (name,) ) + self.cursor.execute("INSERT INTO `item` VALUES (%s, %s) ;", (sku, name) ) loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data added" @@ -102,7 +103,7 @@ class item: loggorilla.error(APIADDR, str(e) ) loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "failed" - response["desc" ] = "Internal Server Error. Please contact us if you still have an error." + response["desc" ] = "Internal Server Error. Please contact us if you still have an error." finally: self.cursor.execute("COMMIT;") self.cursor.close() @@ -113,12 +114,12 @@ class item: APIADDR = "/api/invlab/item/edit" response = {} loggorilla.prcss(APIADDR, "Define parameters") - key = params["id" ] - name = params["name" ] + key = params["sku" ] + name = params["name" ] self.cursor.execute("BEGIN;") try: loggorilla.prcss(APIADDR, "Updating") - self.cursor.execute("UPDATE `item` SET `name` = %s WHERE `id` = %s ;", (name, key) ) + self.cursor.execute("UPDATE `item` SET `name` = %s WHERE `sku` = %s ;", (name, key) ) loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data change" @@ -138,11 +139,11 @@ class item: APIADDR = "/api/invlab/item/remove" response = {} loggorilla.prcss(APIADDR, "Define parameters") - key = params["id" ] + key = params["sku" ] self.cursor.execute("BEGIN;") try: loggorilla.prcss(APIADDR, "Deleting") - self.cursor.execute("DELETE FROM `item` WHERE `id` = %s ;", (key,) ) + self.cursor.execute("DELETE FROM `item` WHERE `sku` = %s ;", (key,) ) loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data removed"