Highlight module for catalog (sample)
This commit is contained in:
parent
a50f9ab69a
commit
1b7addaefb
69
modules/api/hightlight/catalog.py
Normal file
69
modules/api/hightlight/catalog.py
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user