def insert_reservation(self, payload): try: customer_id = payload["customer_id"] product_id = payload["product_id"] quantity = payload["quantity"] except KeyError: return ErrorHandler().bad_request() reservation_id = ReservationDAO().insert_reservation( customer_id, product_id, quantity) if reservation_id == -1: return ErrorHandler().bad_request("Product does not exist") if reservation_id == -2: return ErrorHandler().bad_request( "Must Submit an order, not a reservation.") if reservation_id == -4: return ErrorHandler().bad_request("Not enough resources") return ( self.build_reservation( (reservation_id, customer_id, product_id, quantity)), 201, )
def update_reservation(self, reservation_id, payload): reservation_dao = ReservationDAO() if not reservation_dao.get_reservation_by_id(reservation_id): return ErrorHandler().not_found() try: customer_id = payload["customer_id"] product_id = payload["product_id"] quantity = payload["quantity"] except KeyError: return ErrorHandler().bad_request() reservation_id = reservation_dao.update_reservation( reservation_id, customer_id, product_id, quantity) if reservation_id == -1: return ErrorHandler().bad_request("Given product does not exist") if reservation_id == -2: return ErrorHandler().bad_request( "Must Submit an order for the new product, not a reservation.") if reservation_id == -4: return ErrorHandler().bad_request("Not enough resources") return ( self.build_reservation( (reservation_id, customer_id, product_id, quantity)), 200, )
def update_supplier(self, supplier_id, supplier): if not self.get_supplier_by_id(supplier_id): return ErrorHandler().not_found() try: supplier_name = supplier["customer_first_name"] supplier_city = supplier["customer_city"] latitude = supplier["latitude"] longitude = supplier["longitude"] except KeyError: ErrorHandler().bad_request() if supplier_name and supplier_city and latitude and longitude: supplier_id, location_id = SupplierDAO().update_supplier( supplier_id, supplier_name, supplier_city, ) LocationDAO().update_location(location_id, latitude, longitude) return ( self.build_supplier((supplier_id, supplier_name, supplier_city, location_id)), 200, ) else: return ErrorHandler().bad_request() else: return ErrorHandler().bad_request()
def add_product(self, reservation_id, payload): reservation_dao = ReservationDAO() if not reservation_dao.get_reservation_by_id(reservation_id): return ErrorHandler().not_found() try: product_id = payload["product_id"] quantity = payload["quantity"] except KeyError: return ErrorHandler().bad_request() new_quantity = reservation_dao.add_product(reservation_id, product_id, quantity) if new_quantity == -1: return ErrorHandler().bad_request("Given product does not exist") if new_quantity == -2: return ErrorHandler().bad_request( "Must Submit an order for the new product, not a reservation.") if new_quantity == -4: return ErrorHandler().bad_request("Not enough resources") return self.build_reservation( (reservation_id, "same", product_id, new_quantity)), 201
def check_category_attributes(self, category, payload): try: for attribute in self.categories[category]["attributes"]: if attribute not in payload: return ErrorHandler().bad_request("Invalid category attributes") except KeyError: return ErrorHandler().bad_request("Invalid category")
def insert_product_category_info(self, category_name, product_id, payload): try: category = self.categories[category_name] except KeyError: return ErrorHandler().bad_request("Invalid category") cursor = self.conn.cursor() query = ( "insert into " + category_name + "(" + "".join(map(lambda attribute: attribute + ", ", category["attributes"])) + "product_id) values (" + "".join(["%s, " for i in category["attributes"]]) + "%s) returning " + category_name + "_id;" ) try: temp = [payload[attribute] for attribute in category["attributes"]] temp.append(product_id) except KeyError: return ErrorHandler().bad_request("Invalid category attributes") cursor.execute( query, tuple(temp), ) id = cursor.fetchone()[0] self.conn.commit() return id
def update_product_category_info(self, product_id, payload): product_dao = ProductDAO() if not product_dao.get_product_by_id(product_id): return ErrorHandler().not_found() try: category_attributes = payload["category_attributes"] except KeyError: return ErrorHandler().bad_request() category_dao = CategoryDAO() category = product_dao.get_product_category(product_id) # Check that correct attributes for category were passed category_response = category_dao.check_category_attributes( category, category_attributes) if category_response: return category_response else: category_dao.update_product_category_info(category, product_id, category_attributes) result = { **product_dao.get_product_by_id(product_id), **category_attributes, } return jsonify(Product=result), 200
def get_product_by_id(self, product_id): """ Returns a JSON object containing the product with the indicated ID. """ result = ProductDAO().get_product_by_id(product_id) if not result: return ErrorHandler().not_found() return jsonify(product=result), 200
def search_products(self, args): """ Searches a product by keyword in its name and returns a JSON object containing it. """ dao = ProductDAO() try: keyword = args.get("keyword") except KeyError: return ErrorHandler().bad_request() if keyword: result = dao.get_products_by_keyword(keyword) return jsonify(products=result), 200 else: return ErrorHandler().bad_request()
def search_suppliers(self, supplier): try: supplier_city = supplier["customer_city"] except KeyError: ErrorHandler().bad_request() if supplier_city: supplier_list = SupplierDAO().get_suppliers_by_city( supplier_city) result_list = [] for row in supplier_list: result = self.build_supplier(row) result_list.append(result) return jsonify(suppliers=result_list) else: return ErrorHandler().bad_request()
def delete_reservations_by_customer_id(self, customer_id): reservation_dao = ReservationDAO() if not reservation_dao.get_reservations_by_customer_id(customer_id): return ErrorHandler().not_found() reservation_dao.delete_reservations_by_customer_id(customer_id) return jsonify(Deletion="Reservation Deleted"), 200
def updated_user(self, user_id, user): if not self.get_user_by_id(user_id): return ErrorHandler().not_found() try: username = user["username"] password = user["password"] phone_number = user["phone_number"] except KeyError: ErrorHandler().bad_request() if username and password and phone_number: user_id = UserDAO().update_user(username, password, phone_number) return (self.build_user_dict((username, password, phone_number)), 200) else: return ErrorHandler().bad_request() else: return ErrorHandler().bad_request()
def get_all_products_by_category(self, category): """ Returns a JSON object containing the products in the specified category along with their category specific details. """ if category: result = ProductDAO().get_products_by_category(category) return jsonify(products=result), 200 else: return ErrorHandler().bad_request()
def search_customer(self, customer): try: customer_city = customer["customer_city"] except KeyError: ErrorHandler().bad_request() if customer_city: customers_list = CustomerDAO().get_customers_by_city( customer_city) result_list = [] for row in customers_list: result = self.build_customer(row) result_list.append(result) return jsonify(customer=result_list) else: return ErrorHandler().bad_request()
def update_product(self, product_id, payload): """Updates the attributes of the products with the specified id Arguments: product_id {int} -- Product ID payload {dictionary} -- Holds information to be updated Returns: tuple -- Updated product and/or response """ product_dao = ProductDAO() if not product_dao.get_product_by_id(product_id): return ErrorHandler().not_found() try: product_name = payload["product_name"] product_quantity = payload["product_quantity"] product_price = payload["product_price"] product_description = payload["product_description"] category = payload["category"] except KeyError: return ErrorHandler().bad_request() location_id = product_dao.update_product( product_id, product_name, product_quantity, product_price, category, product_description, ) return ( self.build_product(( product_id, product_name, product_quantity, product_price, category, product_description, location_id, )), 200, )
def get_detailed_product_by_id(self, product_id): """ Returns a JSON object containing the product with the indicated ID. Includes all of the product's details. """ product_dao = ProductDAO() if not product_dao.get_product_by_id(product_id): return ErrorHandler().not_found() result = product_dao.get_detailed_product_by_id(product_id) return jsonify(product=result), 200
def update_admin(self, admin_id, admin): if not self.get_admin_by_id(admin_id): return ErrorHandler().not_found() try: admin_name = admin["admin_name"] except KeyError: ErrorHandler().bad_request() if admin_name: AdminDAO().update_admin(admin_id, admin_name) return (self.build_admin_dict((admin_id, admin_name)), 200) else: return ErrorHandler().bad_request() else: return ErrorHandler().bad_request()
def update_product_location(self, product_id, payload): product_dao = ProductDAO() if not product_dao.get_product_by_id(product_id): return ErrorHandler().not_found() try: latitude = payload["latitude"] longitude = payload["longitude"] except KeyError: return ErrorHandler().bad_request() location_id = product_dao.get_product_location_id(product_id) LocationDAO().update_location(location_id, latitude, longitude) result = {**product_dao.get_product_by_id(product_id), **payload} return jsonify(Product=result), 200
def get_product_location(self, product_id): """Returns the location details of the product with the specified id. Arguments: product_id {int} -- Product Id """ result = ProductDAO().get_product_location(product_id) if not result: return ErrorHandler().not_found() return jsonify(product=result), 200
def delete_product(self, product_id): """ Deletes the product with the specified id. """ product_dao = ProductDAO() if not product_dao.get_product_by_id(product_id): return ErrorHandler().not_found() else: product_category = product_dao.get_product_category(product_id) CategoryDAO().delete_category_info(product_category, product_id) location_id = product_dao.delete_product(product_id) LocationDAO().delete_location(location_id) return jsonify(Deletion="OK"), 200
def update_customer(self, customer_id, customer): if not self.get_customer_by_id(customer_id): return ErrorHandler().not_found() try: customer_first_name = customer["customer_first_name"] customer_last_name = customer["customer_last_name"] customer_city = customer["customer_city"] latitude = customer["latitude"] longitude = customer["longitude"] cc_id = customer["cc_id"] cc_type = customer["cc_type"] cc_number = customer["cc_number"] except KeyError: ErrorHandler().bad_request() customer_id, location_id = CustomerDAO().update_customer( customer_first_name, customer_last_name, customer_city, ) LocationDAO().update_location(location_id, latitude, longitude) CreditCardDAO().update_credit_card(cc_id, cc_number, cc_type, customer_id) return ( self.build_customer_user(( customer_id, customer_first_name, customer_last_name, customer_city, location_id, )), 200, )
def update_product_category_info(self, category_name, product_id, payload): try: category = self.categories[category_name] except KeyError: return ErrorHandler().bad_request("Invalid category") cursor = self.conn.cursor() query = ( "update " + category_name + " set " + "".join( map( lambda attribute: attribute + " = %s, ", category["attributes"][:-1] ) ) + category["attributes"][-1] + "=%s where product_id = %s returning " + category_name + "_id;" ) try: temp = [payload[attribute] for attribute in category["attributes"]] temp.append(product_id) except KeyError: return ErrorHandler().bad_request("Invalid category attributes") cursor.execute( query, tuple(temp), ) id = cursor.fetchone()[0] self.conn.commit() return id
def insert_product(self, payload): """ Adds a product and all its information. """ product_dao = ProductDAO() location_dao = LocationDAO() category_dao = CategoryDAO() try: product_name = payload["product_name"] product_quantity = payload["product_quantity"] product_price = payload["product_price"] product_description = payload["product_description"] latitude = payload["latitude"] longitude = payload["longitude"] category = payload["category"] category_attributes = payload["category_attributes"] except KeyError: return ErrorHandler().bad_request() # Check that correct attributes for category were passed category_response = category_dao.check_category_attributes( category, category_attributes) if category_response: return category_response location_id = location_dao.insert_location(latitude, longitude) product_id = product_dao.insert_product( product_name, product_quantity, product_price, product_description, category, location_id, ) category_id = category_dao.insert_product_category_info( category, product_id, category_attributes) return ( self.build_product(( product_id, product_name, product_quantity, product_price, product_description, category, location_id, )), 201, )
def insert_credit_card_by_customer_id(self, customer_id, card): credit_card_dao = CreditCardDAO() try: cc_type = card["cc_type"] cc_number = card["cc_number"] except KeyError: ErrorHandler().bad_request() credit_id = credit_card_dao.insert_credit_card(cc_type, cc_number, customer_id) return ( self.build_credit_card((cc_type, cc_number, customer_id)), 201, )
def insert_customer(self, customer): customer_dao = CustomerDAO() user_dao = UserDAO() location_dao = LocationDAO() cc_dao = CreditCardDAO() try: customer_first_name = customer["customer_first_name"] customer_last_name = customer["customer_last_name"] customer_city = customer["customer_city"] customer_address = customer["customer_address"] latitude = customer["latitude"] longitude = customer["longitude"] username = customer["username"] password = customer["password"] phone = customer["phone"] except KeyError: ErrorHandler().bad_request() user_id = user_dao.insert_user(username, password, phone) location_id = location_dao.insert_location(latitude, longitude) customer_id = customer_dao.insert_customer( customer_first_name, customer_last_name, customer_city, location_id, user_id, customer_address, ) return ( self.build_customer_user(( customer_id, customer_first_name, customer_last_name, customer_city, location_id, user_id, )), 201, )
def delete_category_info(self, category_name, product_id): try: self.categories[category_name] except KeyError: return ErrorHandler().bad_request("Invalid category") cursor = self.conn.cursor() query = ( "delete from " + category_name + " where product_id= %s returning " + category_name + "_id;" ) cursor.execute(query, (product_id,)) id = cursor.fetchone()[0] self.conn.commit() return id
def insert_user(self, form): user_dao = UserDAO() try: username = form["username"] password = form["password"] phone_number = form["phone_number"] except KeyError: return ErrorHandler().bad_request() user_id = user_dao.insert_user(username, password, phone_number,) return (self.build_user_dict( ( user_id, username, password, phone_number ) ), 201, )
def insert_admin(self, admin): admin_dao = AdminDAO() user_dao = UserDAO() try: admin_name = admin["admin_name"] username = admin["username"] password = admin["password"] phone_number = admin["phone_number"] except KeyError: ErrorHandler().bad_request() user_id = user_dao.insert_user(username, password, phone_number) admin_id = admin_dao.insert_admin(admin_name, user_id) return ( self.build_admin_dict(( admin_id, admin_name, user_id, )), 201, )
def insert_supplier(self, supplier): try: username = supplier["username"] password = supplier["password"] phone = supplier["phone"] supplier_name = supplier["supplier_name"] supplier_city = supplier["supplier_city"] latitude = supplier["latitude"] longitude = supplier["longitude"] except KeyError: ErrorHandler().bad_request() location_id = LocationDAO().insert_location(latitude, longitude) supplier_id = SupplierDAO().insert_supplier(username, password, phone, supplier_name, supplier_city, location_id) return ( self.build_supplier( (supplier_id, supplier_name, supplier_city, location_id)), 201, )
def get_reservations_by_customer_id(self, customer_id): results = ReservationDAO().get_reservations_by_customer_id(customer_id) if not results: return ErrorHandler().not_found() return jsonify(reservations=results), 200