class FoodImplementation(Food.Food): engine = None session = None user_session = None def __init__(self): self.session_and_engine = SessionAndEngine() self.engine = self.session_and_engine.getEngine() self.session = self.session_and_engine.getSession() self.user_session = UserSession() def add_dish(self, dish_name, calories_per_gm, restaurant_id, available_quantity, unit_price): if self.user_session.check_login(): if self.session.query(VendorModel).filter_by( vendor_id=restaurant_id).first(): new_dish = FoodModel(vendor_id=restaurant_id, dish_name=dish_name, calories_per_gm=calories_per_gm, available_quantity=available_quantity, unit_price=unit_price) self.session.add(new_dish) self.session.commit() print(dish_name + " added in restaurant " + str(restaurant_id)) else: print("Invalid restaurant ID.") else: return "Please signup or login first." def get_dish_by_name(self, dish_name, sort_by='ascending'): if self.user_session.check_login(): dishes_rs = None if sort_by == 'descending': dishes_rs = self.session.query(FoodModel).filter_by( dish_name=dish_name).order_by(desc(FoodModel.unit_price)) else: dishes_rs = self.session.query(FoodModel).filter_by( dish_name=dish_name).order_by(FoodModel.unit_price) for row in dishes_rs: print(row) else: return "Please signup or login first." def get_dishes_by_restaurant(self, restaurant_id, sort_by='ascending'): if self.user_session.check_login(): dishes_rs = None if sort_by == 'descending': dishes_rs = self.session.query(FoodModel).filter_by( vendor_id=restaurant_id).order_by( desc(FoodModel.unit_price)) else: dishes_rs = self.session.query(FoodModel).filter_by( vendor_id=restaurant_id).order_by(FoodModel.unit_price) for row in dishes_rs: print(row) else: return "Please signup or login first."
class VendorImplementation(Vendor.Vendor): engine = None session = None user_session = None def __init__(self): self.session_and_engine = SessionAndEngine() self.engine = self.session_and_engine.getEngine() self.session = self.session_and_engine.getSession() self.user_session = UserSession() def add_vendor(self, customer_id, restaurant_name): """Customer id should be in db while restaurant name shouldn't be""" if self.user_session.check_login(): if self.session.query(CustomerModel).filter_by( cust_id=customer_id).first( ) and not self.session.query(VendorModel).filter_by( restaurant_name=restaurant_name).first(): new_vendor = VendorModel(cust_id=customer_id, restaurant_name=restaurant_name) self.session.add(new_vendor) self.session.commit() restaurant_id = new_vendor.vendor_id print("Vendor added successfully with id: " + str(restaurant_id)) else: print("Invalid Customer id or Restaurant name already exists.") else: print("Please signup or login first.") def get_all_vendors(self): """This method will fetch all the vendors list""" if self.user_session.check_login(): for row in self.session.query(VendorModel).all(): print(row) else: return "Please signup or login first."
class CustomerImplementation(Customer.Customer): engine = None session = None user_session = None def __init__(self): self.session_and_engine = SessionAndEngine() self.engine = self.session_and_engine.getEngine() self.session = self.session_and_engine.getSession() self.user_session = UserSession() def add_customer(self, name, username, password, level): #This is akin to user signup if not self.session.query(CustomerModel).filter_by(username=username).first(): new_customer = CustomerModel(name=name, username=username, password=password, level=level) self.session.add(new_customer) self.session.commit() customer_id = new_customer.cust_id print("Customer added with id: " + str(customer_id)) else: print("Username already exists in the database") def login(self, username, password): user_db_tuple = self.session.query(CustomerModel).filter_by(username=username).first() if user_db_tuple is not None and user_db_tuple.password == password: admin_check = False if int(user_db_tuple.level) == 2: admin_check = True self.user_session.login(username, admin_check) print(str(username) + " logged in.") else: print("Invalid username or password.") def logout(self): self.user_session.setup_or_logout() print("User logged out successfully!")
from sqlalchemy import Integer, Column, String, ForeignKey, Date from Models.SqliteSessionandEngine import SessionAndEngine from sqlalchemy.ext.declarative import declarative_base session_and_engine = SessionAndEngine() Base = declarative_base() engine = session_and_engine.getEngine() class CustomerModel(Base): __tablename__ = 'customer' cust_id = Column(Integer, primary_key=True) name = Column(String) username = Column(String) password = Column(String) level = Column(Integer) def __repr__(self): return "<Customer(name='%s', username='******', level='%s')>" % ( self.name, self.username, str(self.level)) class VendorModel(Base): __tablename__ = 'vendor' vendor_id = Column(Integer, primary_key=True) cust_id = Column(Integer, ForeignKey("customer.cust_id")) restaurant_name = Column(String) def __repr__(self):
def __init__(self): self.session_and_engine = SessionAndEngine() self.engine = self.session_and_engine.getEngine() self.session = self.session_and_engine.getSession() self.user_session = UserSession()
class OrderImplementation(Order.Order): engine = None session = None user_session = None def __init__(self): self.session_and_engine = SessionAndEngine() self.engine = self.session_and_engine.getEngine() self.session = self.session_and_engine.getSession() self.user_session = UserSession() def place_order(self, customer_id, total_amount, date): new_order = OrdersModel(cust_id=customer_id, total_amount=total_amount, date=date) self.session.add(new_order) self.session.commit() order_number = new_order.order_id return order_number def add_order_items(self, order_id, food_id, quantity, amount): new_item = OrderItemsModel(order_id=order_id, food_id=food_id, quantity=quantity, amount=amount) self.session.add(new_item) self.session.commit() return True def compose_order(self, order_list): if self.user_session.check_login(): amount = 0 order_items = [] username = self.user_session.loggedIn_User() customer_row = self.session.query(CustomerModel).filter_by( username=username).first() customer_id = customer_row.cust_id for dish_name, restaurant_name, quantity in order_list: restaurant = self.session.query(VendorModel).filter_by( restaurant_name=restaurant_name).first() food_row = self.session.query(FoodModel).filter_by( dish_name=dish_name, vendor_id=restaurant.vendor_id).first() if food_row: orderItem_amount = int(quantity) * int(food_row.unit_price) orderItem_tuple = (food_row.food_id, quantity, orderItem_amount) order_items.append(orderItem_tuple) amount += orderItem_amount order_number = self.place_order(customer_id, amount, datetime.date.today()) for food_id, quantity, item_amount in order_items: self.add_order_items(order_number, food_id, quantity, item_amount) print("Order Placed") else: print("Please signup or login first.") def get_all_orders_by_customer(self, username): customer_row = self.session.query(CustomerModel).filter_by( username=username).first() customer_name = customer_row.name customer_id = customer_row.cust_id order_rows = self.session.query(OrdersModel).filter_by( cust_id=customer_id) for row in order_rows: order_number = row.order_id order_amount = row.total_amount order_date = row.date ##Print the order high level data first below print("-------Order Start---------------") print("Customer Name: " + customer_name) print("Order Number: " + str(order_number)) print("Order Date: " + str(order_date)) print("Total Order Value: " + str(order_amount)) print("Order Items: ") order_items = self.session.query(OrderItemsModel).filter_by( order_id=order_number) for item_row in order_items: food_row = self.session.query(FoodModel).filter_by( food_id=item_row.food_id).first() dish_name = food_row.dish_name unit_price = food_row.unit_price restaurant_row = self.session.query(VendorModel).filter_by( vendor_id=food_row.vendor_id).first() restaurant_name = restaurant_row.restaurant_name item_quantity = item_row.quantity item_amount = item_row.amount print("Dish Name: " + str(dish_name) + "|" + " Restaurant Name: " + str(restaurant_name) + "|" + " Quantity: " + str(item_quantity) + "|" + " Unit Price: " + str(unit_price) + "|" + " Amount: " + str(item_amount)) print("-------Order End---------------") print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") def get_all_orders(self): if self.user_session.check_admin(): for row in self.session.query(OrdersModel).all(): order_number = row.order_id order_amount = row.total_amount order_date = row.date customer_row = self.session.query(CustomerModel).filter_by( cust_id=row.cust_id).first() customer_name = customer_row.name # Print the order high level data first below print("-------Order Start---------------") print("Customer Name: " + customer_name) print("Order Number: " + str(order_number)) print("Order Date: " + str(order_date)) print("Total Order Value: " + str(order_amount)) print("Order Items: ") order_items = self.session.query(OrderItemsModel).filter_by( order_id=order_number) for item_row in order_items: food_row = self.session.query(FoodModel).filter_by( food_id=item_row.food_id).first() dish_name = food_row.dish_name unit_price = food_row.unit_price restaurant_row = self.session.query(VendorModel).filter_by( vendor_id=food_row.vendor_id).first() restaurant_name = restaurant_row.restaurant_name item_quantity = item_row.quantity item_amount = item_row.amount print("Dish Name: " + str(dish_name) + " Restaurant Name: " + str(restaurant_name) + " Quantity: " + str(item_quantity) + " Unit Price: " + str(unit_price) + " Amount: " + str(item_amount)) print("-------Order End---------------") print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") else: print("Only Admin can see all orders.")