import mysql.connector as mariadb from config import database from scripts import loggorilla class item: def __init__(self): self.db_main = mariadb.connect(**database.db_main) self.cursor = self.db_main.cursor(dictionary=True) def list(self, params): APIADDR = "/api/invlab/item/list" response = {} self.cursor.execute("BEGIN;") try: ls = [] self.cursor.execute("SELECT * FROM `item`;") l1 = self.cursor.fetchall() c1 = 0 for d1 in l1: ls.append({ "id" :d1["id" ], "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"], ) ) 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"], ) ) ls[c1]["price"]["sell"] = self.cursor.fetchall() c1+=1 loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data collected" response["data" ] = ls except Exception as e: self.cursor.execute("ROLLBACK;") 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." finally: self.cursor.execute("COMMIT;") self.cursor.close() self.db_main.close() return response def detail(self, params): APIADDR = "/api/invlab/item/detail" response = {} loggorilla.prcss(APIADDR, "Define parameters") id = params["id"] self.cursor.execute("BEGIN;") try: self.cursor.execute("SELECT * FROM `item` WHERE `id`= %s ;", ( id, ) ) r = self.cursor.fetchone() data = { "id" :r["id" ], "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"], ) ) 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"], ) ) data["price"]["sell"] = self.cursor.fetchall() loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data collected" response["data" ] = data except Exception as e: self.cursor.execute("ROLLBACK;") 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." finally: self.cursor.execute("COMMIT;") self.cursor.close() self.db_main.close() return response def add(self, params): APIADDR = "/api/invlab/item/add" response = {} loggorilla.prcss(APIADDR, "Define parameters") name = params["name" ] self.cursor.execute("BEGIN;") try: loggorilla.prcss(APIADDR, "Inserting") self.cursor.execute("INSERT INTO `item` VALUES (DEFAULT, %s) ;", (name,) ) loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data added" except Exception as e: self.cursor.execute("ROLLBACK;") 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." finally: self.cursor.execute("COMMIT;") self.cursor.close() self.db_main.close() return response def edit(self, params): APIADDR = "/api/invlab/item/edit" response = {} loggorilla.prcss(APIADDR, "Define parameters") key = params["id" ] 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) ) loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data change" except Exception as e: self.cursor.execute("ROLLBACK;") 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." finally: self.cursor.execute("COMMIT;") self.cursor.close() self.db_main.close() return response def remove(self, params): APIADDR = "/api/invlab/item/remove" response = {} loggorilla.prcss(APIADDR, "Define parameters") key = params["id" ] self.cursor.execute("BEGIN;") try: loggorilla.prcss(APIADDR, "Deleting") self.cursor.execute("DELETE FROM `item` WHERE `id` = %s ;", (key,) ) loggorilla.prcss(APIADDR, "Giving response") response["status" ] = "success" response["desc" ] = "data removed" except Exception as e: self.cursor.execute("ROLLBACK;") 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." finally: self.cursor.execute("COMMIT;") self.cursor.close() self.db_main.close() return response