70 lines
2.9 KiB
Python
70 lines
2.9 KiB
Python
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
|