Пример #1
0
    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_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,
        )