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 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 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_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_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 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 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, )