示例#1
0
 def execute(self):
     while not self.op == Menu.MAIN_MENU and not self.done:
         print(self.menu)
         self.op = prompt("$ ", end='')
         if self.op.isalnum() and self.op_in_options():
             return self.trigger_menu_item()
         else:
             print_error("Invalid option '{}'".format(self.op))
示例#2
0
 def delete(self, entity_id: str):
     if entity_id:
         delete_stm = self.sql_factory.delete(
             filters=["UUID = '{}'".format(entity_id)])
         LOG.info('Executing SQL statement: {}'.format(delete_stm))
         self.cursor.execute(delete_stm)
         self.connector.commit()
         print(
             f'Book with index {entity_id} successfully deleted from the database'
         )
     else:
         print_error('Cannot delete this book from database')
 def search_book(self):
     criteria_hint = '* or criteria_1, ... criteria_N ([book_name|author_name|published|pages|available]=value)'
     criteria = prompt(
         "Please type the search criteria: {}\n$ ".format(criteria_hint),
         clear=True)
     try:
         if criteria or criteria == '*':
             found = self.book_service.list(
                 filters=criteria if criteria != '*' else None)
             if found and len(found) > 0:
                 print_list(found)
             else:
                 print_warning(
                     'No books found for the matching criteria {}'.format(
                         criteria))
     except InternalError:
         print_error('Invalid criteria {}'.format(criteria))
示例#4
0
    def connect(self):
        if not self.is_connected():
            try:
                self.connector = pymysql.connect(host=self.hostname,
                                                 user=self.user,
                                                 port=self.port,
                                                 password=self.password,
                                                 database=self.database)
                assert self.is_connected(), "Not connected to the database"
                self.cursor = self.connector.cursor()
                LOG.info('Connected to {} established'.format(str(self)))
            except OperationalError:
                LOG.error('Unable to connect to {}'.format(str(self)))
                print_error('Unable to connect to {}'.format(str(self)))
                sys.exit(1)

        return self.connector
示例#5
0
    def update(self, entity_id: str):
        if entity_id:
            valid = False
            new_book_name = new_author_name = new_published = new_pages = None
            while not valid:
                new_book_name = prompt("New Book Name: ", clear=True).strip() if new_book_name \
                                                                                 is None else new_book_name
                if not validate_string(
                        new_book_name, "[a-zA-Z0-9]+", min_len=1, max_len=60):
                    print_error(f'Invalid name {new_book_name}')
                    new_book_name = None
                    continue
                new_author_name = prompt("New Author Name: ").strip(
                ) if new_author_name is None else new_author_name
                if not validate_string(
                        new_author_name, "[a-zA-Z0-9]+", min_len=1,
                        max_len=60):
                    print_error(f'Invalid author name {new_author_name}')
                    new_author_name = None
                    continue
                new_published = prompt("New Published date: ").strip(
                ) if new_published is None else new_published
                if not validate_date(new_published, "%d/%m/%Y"):
                    print_error(f'Invalid published date {new_published}')
                    new_published = None
                    continue
                new_pages = prompt(
                    "New Pages: ").strip() if new_pages is None else new_pages
                if not validate_int(new_pages, min_value=1, max_value=1000):
                    print_error(f'Invalid pages number {new_pages}')
                    new_pages = None
                    continue
                valid = True

            update_stm1 = self.sql_factory.update(
                key='BOOK_NAME',
                value=new_book_name,
                filters=["UUID = '{}'".format(entity_id)])
            LOG.info('Executing SQL statement: {}'.format(update_stm1))
            self.cursor.execute(update_stm1)
            self.connector.commit()

            update_stm2 = self.sql_factory.update(
                key='AUTHOR_NAME',
                value=new_author_name,
                filters=["UUID = '{}'".format(entity_id)])
            LOG.info('Executing SQL statement: {}'.format(update_stm2))
            self.cursor.execute(update_stm2)
            self.connector.commit()

            update_stm3 = self.sql_factory.update(
                key='PUBLISHED',
                value=new_published,
                filters=["UUID = '{}'".format(entity_id)])
            LOG.info('Executing SQL statement: {}'.format(update_stm3))
            self.cursor.execute(update_stm3)
            self.connector.commit()

            update_stm4 = self.sql_factory.update(
                key='PAGES',
                value=new_pages,
                filters=["UUID = '{}'".format(entity_id)])
            LOG.info('Executing SQL statement: {}'.format(update_stm4))
            self.cursor.execute(update_stm4)
            self.connector.commit()
        else:
            print_error('Cannot edit this book from database')
示例#6
0
 def delete(self, data: Entity):
     if Entity:
         self.repository.delete(data)
     else:
         print_error('Cannot found that book to delete')
示例#7
0
 def update(self, data: Entity):
     if Entity:
         self.repository.update(data)
     else:
         print_error('Cannot found that book to edit')