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))
def get_information(self): entity_id = prompt("Book INDEX: ", clear=True) if entity_id: found = self.book_service.get(entity_id) if found is not None: print_one(found) else: print_warning( 'Book with index = {} was not found'.format(entity_id))
def update_book(self): entity_id = prompt("Book UUID: ", clear=True) if entity_id: found = self.book_service.get(entity_id) if found is not None: self.book_service.update(entity_id) else: print_warning( 'Book with index = {} was not found'.format(entity_id))
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))
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')