def __save_book_in_database(book: Book, age: Age): age_dao = AgeDao() exist_age = age_dao.get_by_slug(age.get_slug()) if exist_age is not None: book.set_age(exist_age) else: age_dao.insert(age_name=age.get_name(), age_slug=age.get_slug()) saved_age = age_dao.get_by_slug(age.get_slug()) book.set_age(saved_age) book_dao = BookDao() book_dao.insert(book)
def create_book(details: dict, content: str, href: str): title = details['title'] author_name = details['authors'][0]['name'] a_slug = details['authors'][0]['slug'] b = Book(book_id=None, title=title, uri=href, age=None, author=author_name, author_slug=a_slug) b.set_content(content) return b
def fetch_content(self, book: Book) -> Book: connection = sqlite3.connect(config.database['path']) cursor = connection.cursor() cursor.execute('SELECT content FROM book ' 'WHERE book.id = :id', {'id': book.get_id()}) row = cursor.fetchone() connection.commit() connection.close() book.set_content(row[0]) return book
def list_by_title(self, book_title: str) -> List[Book]: connection = sqlite3.connect(config.database['path']) cursor = connection.cursor() cursor.execute( 'SELECT book.id, title, uri, author, author_slug, ' 'age.id, age.name, age.slug FROM book ' 'LEFT OUTER JOIN age ON age.id = book.age_id ' 'WHERE book.title like :title', {'title': '%' + book_title + '%'}) data = cursor.fetchall() book_list = [] for row in data: age = Age(row[5], row[6], row[7]) if row else None book_list.append( Book(book_id=row[0], title=row[1], uri=row[2], age=age, author=row[3], author_slug=row[4])) connection.commit() connection.close() return book_list
def insert(self, book: Book) -> None: connection = sqlite3.connect(config.database['path']) cursor = connection.cursor() author = book.get_author() author_slug = book.get_author_slug() age = book.get_age() age_id = age.get_id() if age else None cursor.execute( 'INSERT INTO ' 'book(title, content, uri, author, author_slug, ' 'age_id) ' 'VALUES ' '(:title, :content, :uri, :author, :author_slug, ' ':age_id)', { 'title': book.get_title(), 'content': book.get_content(), 'uri': book.get_uri(), 'author': author, 'author_slug': author_slug, 'age_id': age_id }) connection.commit() connection.close()
def get_by_id(self, book_id: int) -> Book: connection = sqlite3.connect(config.database['path']) cursor = connection.cursor() cursor.execute( 'SELECT book.id, title, uri, author, author_slug, ' 'age.id, age.name, age.slug FROM book ' 'LEFT OUTER JOIN age ON age.id = book.age_id ' 'WHERE book.id = :id', {'id': book_id}) row = cursor.fetchone() connection.commit() connection.close() age = Age(row[5], row[6], row[7]) if row else None return Book(book_id=row[0], title=row[1], uri=row[2], age=age, author=row[3], author_slug=row[4]) if row else None