Basic API
This commit is contained in:
parent
059365b3f3
commit
607ad3f6b1
79
handler.py
79
handler.py
@ -5,11 +5,16 @@
|
|||||||
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
# You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
|
# You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
|
|
||||||
from bottle import Bottle, route
|
from bottle import Bottle, route, request, response
|
||||||
from config import directory
|
from config import directory
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
import templates.plain.main as template_public
|
import templates.plain.main as template_public
|
||||||
import modules.public.home as public_home
|
import modules.public.home as public_home
|
||||||
|
import modules.api.item as api_invlab_item
|
||||||
|
import modules.api.price as api_invlab_price
|
||||||
|
import modules.api.inventory as api_invlab_inventory
|
||||||
|
|
||||||
app = Bottle()
|
app = Bottle()
|
||||||
|
|
||||||
@ -21,3 +26,75 @@ def index():
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return public_home.main().html(params)
|
return public_home.main().html(params)
|
||||||
|
|
||||||
|
@app.route('/api/invlab/item/<crud>', method=['OPTIONS', 'POST'])
|
||||||
|
def index(crud):
|
||||||
|
try:
|
||||||
|
if request.method == 'OPTIONS':
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
response.content_type = 'application/json'
|
||||||
|
params = request.json
|
||||||
|
if crud == 'list':
|
||||||
|
return json.dumps(api_invlab_item.item().list(params), indent = 2).encode()
|
||||||
|
elif crud == 'detail':
|
||||||
|
return json.dumps(api_invlab_item.item().detail(params), indent = 2).encode()
|
||||||
|
elif crud == 'add':
|
||||||
|
return json.dumps(api_invlab_item.item().add(params), indent = 2).encode()
|
||||||
|
elif crud == 'edit':
|
||||||
|
return json.dumps(api_invlab_item.item().edit(params), indent = 2).encode()
|
||||||
|
elif crud == 'remove':
|
||||||
|
return json.dumps(api_invlab_item.item().remove(params), indent = 2).encode()
|
||||||
|
else:
|
||||||
|
return json.dumps({}, indent = 2).encode()
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e),flush=True)
|
||||||
|
return json.dumps({}, indent = 2).encode()
|
||||||
|
|
||||||
|
@app.route('/api/invlab/price/<crud>', method=['OPTIONS', 'POST'])
|
||||||
|
def index(crud):
|
||||||
|
try:
|
||||||
|
if request.method == 'OPTIONS':
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
response.content_type = 'application/json'
|
||||||
|
params = request.json
|
||||||
|
if crud == 'list':
|
||||||
|
return json.dumps(api_invlab_price.price().list(params), indent = 2).encode()
|
||||||
|
elif crud == 'detail':
|
||||||
|
return json.dumps(api_invlab_price.price().detail(params), indent = 2).encode()
|
||||||
|
elif crud == 'add':
|
||||||
|
return json.dumps(api_invlab_price.price().add(params), indent = 2).encode()
|
||||||
|
elif crud == 'edit':
|
||||||
|
return json.dumps(api_invlab_price.price().edit(params), indent = 2).encode()
|
||||||
|
elif crud == 'remove':
|
||||||
|
return json.dumps(api_invlab_price.price().remove(params), indent = 2).encode()
|
||||||
|
else:
|
||||||
|
return json.dumps({}, indent = 2).encode()
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e),flush=True)
|
||||||
|
return json.dumps({}, indent = 2).encode()
|
||||||
|
|
||||||
|
@app.route('/api/invlab/inventory/<crud>', method=['OPTIONS', 'POST'])
|
||||||
|
def index(crud):
|
||||||
|
try:
|
||||||
|
if request.method == 'OPTIONS':
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
response.content_type = 'application/json'
|
||||||
|
params = request.json
|
||||||
|
if crud == 'list':
|
||||||
|
return json.dumps(api_invlab_inventory.inventory().list(params), indent = 2).encode()
|
||||||
|
elif crud == 'detail':
|
||||||
|
return json.dumps(api_invlab_inventory.inventory().detail(params), indent = 2).encode()
|
||||||
|
elif crud == 'add':
|
||||||
|
return json.dumps(api_invlab_inventory.inventory().add(params), indent = 2).encode()
|
||||||
|
elif crud == 'edit':
|
||||||
|
return json.dumps(api_invlab_inventory.inventory().edit(params), indent = 2).encode()
|
||||||
|
elif crud == 'remove':
|
||||||
|
return json.dumps(api_invlab_inventory.inventory().remove(params), indent = 2).encode()
|
||||||
|
else:
|
||||||
|
return json.dumps({}, indent = 2).encode()
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e),flush=True)
|
||||||
|
return json.dumps({}, indent = 2).encode()
|
||||||
|
193
modules/api/inventory.py
Normal file
193
modules/api/inventory.py
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
import mysql.connector as mariadb
|
||||||
|
|
||||||
|
from config import database
|
||||||
|
from scripts import loggorilla
|
||||||
|
|
||||||
|
class inventory:
|
||||||
|
|
||||||
|
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/inventory/list"
|
||||||
|
response = {}
|
||||||
|
self.cursor.execute("BEGIN;")
|
||||||
|
try:
|
||||||
|
ls = []
|
||||||
|
self.cursor.execute("SELECT item.id AS item_id, item.name AS item_name, SUM(CASE WHEN inv.flow = 'sell' THEN price.value * inv.qty WHEN inv.flow = 'buy' THEN -price.value * inv.qty ELSE 0 END) AS profit, SUM(CASE WHEN inv.flow = 'buy' THEN inv.qty WHEN inv.flow = 'sell' THEN -inv.qty ELSE 0 END) AS current_qty FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1) GROUP BY item.id, item.name ORDER BY item.name ASC;")
|
||||||
|
l1 = self.cursor.fetchall()
|
||||||
|
c1 = 0
|
||||||
|
for d1 in l1:
|
||||||
|
ls.append({
|
||||||
|
"item":{
|
||||||
|
"id" : d1["item_id" ],
|
||||||
|
"name" : d1["item_name" ]
|
||||||
|
},
|
||||||
|
"qty" : int(d1["current_qty" ]),
|
||||||
|
"profit" : int(d1["profit" ]),
|
||||||
|
"history" : []
|
||||||
|
})
|
||||||
|
self.cursor.execute("SELECT inv.id, inv.flow, DATE_FORMAT(inv.when, '%Y-%m-%d %H:%i:%S') AS `when`, DATE_FORMAT(price.periods, '%Y-%m-%d %H:%i:%S') AS price_periods, price.value AS price_value, inv.qty, (price.value * inv.qty) AS `total` FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1 ) WHERE item.id = %s ORDER BY inv.when DESC; ", ( d1["item_id"], ) )
|
||||||
|
l2 = self.cursor.fetchall()
|
||||||
|
c2 = 0
|
||||||
|
for d2 in l2:
|
||||||
|
ls[c1]["history"].append({
|
||||||
|
"id" : d2["id" ],
|
||||||
|
"flow" : d2["flow" ],
|
||||||
|
"when" : d2["when" ],
|
||||||
|
"price" : {
|
||||||
|
"periods" : d2["price_periods" ],
|
||||||
|
"value" : d2["price_value" ]
|
||||||
|
},
|
||||||
|
"qty" : d2["qty" ],
|
||||||
|
"total" : d2["total" ]
|
||||||
|
})
|
||||||
|
c2+=2
|
||||||
|
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/inventory/detail"
|
||||||
|
response = {}
|
||||||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||||||
|
id = params["id"]
|
||||||
|
self.cursor.execute("BEGIN;")
|
||||||
|
try:
|
||||||
|
self.cursor.execute("SELECT item.id AS item_id, item.name AS item_name, SUM(CASE WHEN inv.flow = 'sell' THEN price.value * inv.qty WHEN inv.flow = 'buy' THEN -price.value * inv.qty ELSE 0 END) AS profit, SUM(CASE WHEN inv.flow = 'buy' THEN inv.qty WHEN inv.flow = 'sell' THEN -inv.qty ELSE 0 END) AS current_qty FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1) WHERE item.id = %s GROUP BY item.id, item.name ORDER BY item.name ASC;", ( id, ) )
|
||||||
|
d1 = self.cursor.fetchone()
|
||||||
|
data = {
|
||||||
|
"item":{
|
||||||
|
"id" : d1["item_id" ],
|
||||||
|
"name" : d1["item_name" ]
|
||||||
|
},
|
||||||
|
"qty" : int(d1["current_qty" ]),
|
||||||
|
"profit" : int(d1["profit" ]),
|
||||||
|
"history" : []
|
||||||
|
}
|
||||||
|
self.cursor.execute("SELECT inv.id, inv.flow, DATE_FORMAT(inv.when, '%Y-%m-%d %H:%i:%S') AS `when`, DATE_FORMAT(price.periods, '%Y-%m-%d %H:%i:%S') AS price_periods, price.value AS price_value, inv.qty, (price.value * inv.qty) AS `total` FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1 ) WHERE item.id = %s ORDER BY inv.when DESC; ", ( d1["item_id"], ) )
|
||||||
|
l2 = self.cursor.fetchall()
|
||||||
|
c2 = 0
|
||||||
|
for d2 in l2:
|
||||||
|
data["history"].append({
|
||||||
|
"id" : d2["id" ],
|
||||||
|
"flow" : d2["flow" ],
|
||||||
|
"when" : d2["when" ],
|
||||||
|
"price" : {
|
||||||
|
"periods" : d2["price_periods" ],
|
||||||
|
"value" : d2["price_value" ]
|
||||||
|
},
|
||||||
|
"qty" : d2["qty" ],
|
||||||
|
"total" : d2["total" ]
|
||||||
|
})
|
||||||
|
c2+=2
|
||||||
|
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/inventory/add"
|
||||||
|
response = {}
|
||||||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||||||
|
item = params["item" ]
|
||||||
|
flow = params["flow" ]
|
||||||
|
when = params["when" ]
|
||||||
|
qty = params["qty" ]
|
||||||
|
note = params["note" ]
|
||||||
|
self.cursor.execute("BEGIN;")
|
||||||
|
try:
|
||||||
|
loggorilla.prcss(APIADDR, "Inserting")
|
||||||
|
self.cursor.execute("INSERT INTO `inventory` VALUES (DEFAULT, %s, %s, %s, %s, %s) ;", (item, flow, when, qty, note) )
|
||||||
|
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/inventory/edit"
|
||||||
|
response = {}
|
||||||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||||||
|
key = params["id" ]
|
||||||
|
item = params["item" ]
|
||||||
|
flow = params["flow" ]
|
||||||
|
when = params["when" ]
|
||||||
|
qty = params["qty" ]
|
||||||
|
note = params["note" ]
|
||||||
|
self.cursor.execute("BEGIN;")
|
||||||
|
try:
|
||||||
|
loggorilla.prcss(APIADDR, "Updating")
|
||||||
|
self.cursor.execute("UPDATE `inventory` SET `item` = %s, `flow` = %s, `when` = %s, `qty` = %s, `note` = %s WHERE `id` = %s ;", (item, flow, when, qty, note, 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/inventory/remove"
|
||||||
|
response = {}
|
||||||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||||||
|
key = params["id" ]
|
||||||
|
self.cursor.execute("BEGIN;")
|
||||||
|
try:
|
||||||
|
loggorilla.prcss(APIADDR, "Deleting")
|
||||||
|
self.cursor.execute("DELETE FROM `inventory` 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
|
159
modules/api/item.py
Normal file
159
modules/api/item.py
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
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
|
139
modules/api/price.py
Normal file
139
modules/api/price.py
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
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
|
13
scripts/loggorilla.py
Normal file
13
scripts/loggorilla.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
|
def prcss(loc, msg):
|
||||||
|
print(f"[loggorilla][{datetime.datetime.now()}][\033[32mprcss\033[39m][\033[95m{loc}\033[39m] {msg}", flush=True)
|
||||||
|
|
||||||
|
def accss(loc, msg):
|
||||||
|
print(f"[loggorilla][{datetime.datetime.now()}][\033[36maccss\033[39m][\033[95m{loc}\033[39m] {msg}", flush=True)
|
||||||
|
|
||||||
|
def fyinf(loc, msg):
|
||||||
|
print(f"[loggorilla][{datetime.datetime.now()}][\033[93mfyinf\033[39m][\033[95m{loc}\033[39m] {msg}", flush=True)
|
||||||
|
|
||||||
|
def error(loc, msg):
|
||||||
|
print(f"[loggorilla][{datetime.datetime.now()}][\033[31merror\033[39m][\033[95m{loc}\033[39m] {msg}", flush=True)
|
Loading…
Reference in New Issue
Block a user