flaschengeist/geruecht/controller/databaseController/dbPricelistController.py

134 lines
5.9 KiB
Python
Raw Normal View History

import traceback
from geruecht.exceptions import DatabaseExecption
class Base:
def getPriceList(self):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from pricelist")
retVal = cursor.fetchall()
for data in retVal:
data['drink_type'] = self.getDrinkType(data['type'])
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
def getDrinkPrice(self, name):
try:
cursor = self.db.connection.cursor()
if type(name) == str:
sql = "select * from pricelist where name='{}'".format(name)
elif type(name) == int:
sql = 'select * from pricelist where id={}'.format(name)
else:
raise DatabaseExecption("name as no type int or str. name={}, type={}".format(name, type(name)))
cursor.execute(sql)
retVal = cursor.fetchone()
if retVal:
retVal['drink_type'] = self.getDrinkType(retVal['type'])
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
def setDrinkPrice(self, drink):
try:
cursor = self.db.connection.cursor()
cursor.execute(
"insert into pricelist (name, price, price_big, price_club, price_club_big, premium, premium_club, price_extern_club, type) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
(
drink['name'], drink['price'], drink['price_big'], drink['price_club'], drink['price_club_big'],
drink['premium'], drink['premium_club'], drink['price_extern_club'], drink['type']))
self.db.connection.commit()
return self.getDrinkPrice(str(drink['name']))
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
def updateDrinkPrice(self, drink):
try:
cursor = self.db.connection.cursor()
cursor.execute("update pricelist set name=%s, price=%s, price_big=%s, price_club=%s, price_club_big=%s, premium=%s, premium_club=%s, price_extern_club=%s, type=%s where id=%s",
(
drink['name'], drink['price'], drink['price_big'], drink['price_club'], drink['price_club_big'], drink['premium'], drink['premium_club'], drink['price_extern_club'], drink['type'], drink['id']
))
self.db.connection.commit()
return self.getDrinkPrice(drink['id'])
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
def deleteDrink(self, drink):
try:
cursor = self.db.connection.cursor()
cursor.execute("delete from pricelist where id={}".format(drink['id']))
self.db.connection.commit()
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Database: {}".format(err))
def getDrinkType(self, name):
try:
cursor = self.db.connection.cursor()
if type(name) == str:
sql = "select * from drink_type where name='{}'".format(name)
elif type(name) == int:
sql = 'select * from drink_type where id={}'.format(name)
else:
raise DatabaseExecption("name as no type int or str. name={}, type={}".format(name, type(name)))
cursor.execute(sql)
return cursor.fetchone()
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
def setDrinkType(self, name):
try:
cursor = self.db.connection.cursor()
cursor.execute("insert into drink_type (name) values ('{}')".format(name))
self.db.connection.commit()
return self.getDrinkType(name)
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Database: {}".format(err))
def updateDrinkType(self, type):
try:
cursor = self.db.connection.cursor()
cursor.execute("update drink_type set name='{}' where id={}".format(type['name'], type['id']))
self.db.connection.commit()
return self.getDrinkType(type['id'])
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Database: {}".format(err))
def deleteDrinkType(self, type):
try:
cursor = self.db.connection.cursor()
cursor.execute("delete from drink_type where id={}".format(type['id']))
self.db.connection.commit()
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
def getAllDrinkTypes(self):
try:
cursor = self.db.connection.cursor()
cursor.execute('select * from drink_type')
return cursor.fetchall()
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Database: {}".format(err))