Skip to content
Snippets Groups Projects
Commit d8240448 authored by n9-baker's avatar n9-baker
Browse files

working on createBooking..

parent f4a687b6
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -57,7 +57,6 @@ def select_from_table_where(table:str, condition:str, selection:str):
table (str): database table name, eg; "tblUsers"
condition (str): condition string in SQL format, eg; "WHERE username = %s AND password_hash = %s"
*columns: excepts all aditinal arguments and converts them into SQL query following SELECT as arguments, if none SQL statemnt uses *.
Returns:
Returns:
list(dict())
"""
......@@ -69,7 +68,7 @@ def select_from_table_where(table:str, condition:str, selection:str):
#logging.debug(table, condition, selection)
cursor = conn.cursor(dictionary=True)
query = f"SELECT {selection} FROM {table} WHERE {condition}"
logging.debug(f"query={query}")
logging.debug(f"query='{query}'")
cursor.execute(query)
results = cursor.fetchall() # returns string in format str(dict(key:values))
if not results:
......@@ -85,7 +84,7 @@ def select_from_table_where(table:str, condition:str, selection:str):
# conn.close()
# Function to INSERT data into any table
def insert_into_table(table, data):
def insert_into_table(table:str, data:dict):
"""Inserts data into a specified table."""
conn = get_connection()
if not conn:
......@@ -96,7 +95,7 @@ def insert_into_table(table, data):
columns = ', '.join(data.keys())
values = ', '.join(['%s'] * len(data))
query = f"INSERT INTO {table} ({columns}) VALUES ({values})"
cursor.execute(query, tuple(data.values()))
cursor.execute(query, data.values())
conn.commit()
logging.info("Data inserted successfully.")
return True
......
......@@ -77,8 +77,51 @@ class StaffUser:
def createBooking(self):
""" Adds booking to db and returns ticket if succsessful.
"""
logging.debug("called createBooking method")
raise NotImplementedError()
# input data and validation loop
while True:
print("Please input booking info.")
showing_id = int(input("\nShowing ID:"))
seats = input("\nSeats in format '1' or '1 2 3' (no dupes):")
if showing_id != None and seats != None:
# import showing data
showing_data = select_from_table_where(table="tblShowings",
selection="*",
condition=f"showing_id={showing_id}")
showing_data = showing_data[0]
logging.debug(type(showing_data))
logging.debug(f"showing_data={showing_data}")
# check if showing_data was returned from db
if showing_data != None:
# validate selected seats
seats = seats.split(" ")
logging.debug(f"seats.split={seats}")
seats = [int(x) for x in seats] # use while seats are int, remove when seats are str.
logging.debug(f"seats.int={seats}")
seating_cap = select_from_table_where(table="tblScreens",
selection="seating_capacity",
condition=f"screen_id={showing_data['screen_id']}")
seating_cap = seating_cap[0]['seating_capacity']
logging.debug(f"seating_cap={seating_cap}")
# check if seat selection in range
if max(seats) <= int(seating_cap) and min(seats) > 0:
# showing_id and seats exist, and seat selection is in range now save to tblBookings
price = showing_data['price'] * len(seats)
insert_data=dict(user_id=self.user_id, showing_id=showing_data['showing_id'], seat_numbers=seats, total_price=price, booking_date=showing_data['show_time'])
logging.debug(f"insert_data={insert_data}")
insert_into_table(table="tblBookings",
data=insert_data)
return
print("Invalid seating selection.")
else:
print("Invalid showing_id.")
else:
print("Missing input please try again.")
def veiwBookings(self) -> dict:
""" Gets bookings from db
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment