From 1b7addaefba67ab899f06255314596477fc7d5e9 Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Sun, 29 Jun 2025 19:34:49 +0700 Subject: [PATCH] Highlight module for catalog (sample) --- modules/api/hightlight/catalog.py | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 modules/api/hightlight/catalog.py diff --git a/modules/api/hightlight/catalog.py b/modules/api/hightlight/catalog.py new file mode 100644 index 0000000..b7010be --- /dev/null +++ b/modules/api/hightlight/catalog.py @@ -0,0 +1,69 @@ +import mysql.connector as mariadb + +from bottle import request +from config import database +from scripts import loggorilla + +class catalog: + + def __init__(self): + self.db_main = mariadb.connect(**database.db_main) + self.cursor = self.db_main.cursor(dictionary=True) + + def add(self, params): + APIADDR = "/api/highlight/catalog/add" + response = {} + loggorilla.prcss(APIADDR, "Define Models") + item = params["item" ] + category = params["category" ] + self.cursor.execute("BEGIN;") + try: + self.cursor.execute("SELECT COUNT(*) AS `count` FROM `highlight_catalog` WHERE `item` = %s AND `category` = %s ; ", (item, category) ) + exist = self.cursor.fetchone() + loggorilla.prcss(APIADDR, "Validation") + if exist["count"] > 0: + loggorilla.prcss(APIADDR, "Set Response") + response["status" ] = "failed" + response["desc" ] = "Your data already exist" + else: + loggorilla.prcss(APIADDR, "Activity: Insert") + self.cursor.execute("INSERT INTO `highlight_catalog` VALUES ( DEFAULT, %s, %s ) ; ", (item, category) ) + loggorilla.prcss(APIADDR, "Set Response") + response["status" ] = "success" + response["desc" ] = "Data Added" + except Exception as e: + self.cursor.execute("ROLLBACK;") + loggorilla.error(APIADDR, str(e) ) + loggorilla.prcss(APIADDR, "Set 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/highlight/catalog/remove" + response = {} + loggorilla.prcss(APIADDR, "Define Models") + item = params["item" ] + category = params["category" ] + self.cursor.execute("BEGIN;") + try: + loggorilla.prcss(APIADDR, "Activity: Remove") + self.cursor.execute("DELETE FROM `highlight_catalog` WHERE `item` = %s AND `category` = %s ; ", (item, category) ) + loggorilla.prcss(APIADDR, "Set Response") + response["status" ] = "success" + response["desc" ] = "data removed" + except Exception as e: + self.cursor.execute("ROLLBACK;") + loggorilla.error(APIADDR, str(e) ) + loggorilla.prcss(APIADDR, "Set 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