示例#1
0
    def find_by_name(self, first_name, last_name):
        """
        Validates first_name & last_name are str
        Returns Author if exists
        :param first_name: str
        :param last_name: str
        :return: Author | None
        """

        # Validates type
        if not TypeUtils.all_of_type(first_name, last_name, var_type=str):
            print(ErrorMsgUtils.type_error(first_name, last_name,
                                           var_type=str))
            return

        # Validate legal value
        if not first_name:
            print(ErrorMsgUtils.illegal_value(first_name))
        if not last_name:
            print(ErrorMsgUtils.illegal_value(last_name))

        sql = f"SELECT * FROM AUTHOR WHERE FIRST_NAME = '{first_name}' AND LAST_NAME = '{last_name}'"
        # Execution
        try:
            self.cursor.execute(sql)
            results = self.cursor.fetchone()
            if not results:
                print(
                    ErrorMsgUtils.does_not_exists(
                        table_name='Author', var=f"{first_name}, {last_name}"))
                return

            # Return results
            return Author(id=results[0],
                          first_name=results[1],
                          last_name=results[2])

        except mysql.connector.Error as error:
            print(ErrorMsgUtils.display_error(error))
示例#2
0
    def name(self, new_name):
        # Validates type
        if not TypeUtils.is_type(new_name, var_type=str):
            print(ErrorMsgUtils.type_error(new_name, var_type=str))
            return

            # Validates legal value
        if not new_name:
            print(ErrorMsgUtils.illegal_value(new_name))
            return

        # Set value
        self.__name = new_name
示例#3
0
    def price(self, new_price):
        # Validates type
        if not TypeUtils.is_type_or(new_price, var_type1=int, var_type2=float):
            print(ErrorMsgUtils.none_of_type(int, float, var=new_price))
            return

        # Validates legal value
        if new_price <= 0:
            print(ErrorMsgUtils.illegal_value(new_price))
            return

        # Set value
        self.__price = new_price
示例#4
0
    def id(self, new_id):
        # Validates type
        if not TypeUtils.is_type(new_id, var_type=int):
            print(ErrorMsgUtils.type_error(new_id, var_type=int))
            return

        # Validate legal value
        if new_id < 1:
            print(ErrorMsgUtils.illegal_value(new_id))
            return

        # Set value
        self.__id = new_id