inventory/modules/api/price.py

140 lines
5.9 KiB
Python
Raw Permalink Normal View History

2025-01-02 15:12:48 +07:00
import mysql.connector as mariadb
from config import database
from scripts import loggorilla
class price:
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/price/list"
response = {}
self.cursor.execute("BEGIN;")
try:
self.cursor.execute("SELECT `id`, `item`, `type`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` FROM `item_price` ORDER BY periods DESC, id DESC;")
ls = self.cursor.fetchall()
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/price/detail"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
id = params["id"]
self.cursor.execute("BEGIN;")
try:
self.cursor.execute("SELECT `id`, `item`, `type`, `currency`, `value`, DATE_FORMAT(`periods`, '%Y-%m-%d %H:%i:%S') AS `periods` FROM `item_price` WHERE id= %s ORDER BY periods DESC, id DESC;", (id,))
data = self.cursor.fetchone()
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/price/add"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
item = params["item" ]
type = params["type" ]
currency = params["currency" ]
value = params["value" ]
periods = params["periods" ]
self.cursor.execute("BEGIN;")
try:
loggorilla.prcss(APIADDR, "Inserting")
self.cursor.execute("INSERT INTO `item_price` VALUES (DEFAULT, %s, %s, %s, %s, %s) ;", (item, type, currency, value, periods) )
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/price/edit"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
key = params["id" ]
item = params["item" ]
type = params["type" ]
currency = params["currency" ]
value = params["value" ]
periods = params["periods" ]
self.cursor.execute("BEGIN;")
try:
loggorilla.prcss(APIADDR, "Updating")
self.cursor.execute("UPDATE `item_price` SET `item` = %s,`type` = %s,`currency` = %s,`value` = %s,`periods` = %s WHERE `id` = %s ;", (item, type, currency, value, periods, 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/price/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_price` 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