Пример #1
0
async def create_book(pool: asyncpg.pool.Pool, logger: logging.Logger,
                      book: Book) -> Book:
    async with pool.acquire() as connection:
        connection: asyncpg.connection.Connection
        column: Tuple[str, str, int, bool] = (
            book.get_name(),
            book.get_author(),
            book.get_assessment(),
            book.get_removed(),
        )
        id_book = await connection.fetchval(
            INSERT_BOOK.format(table_name=get_table_name()), *column)
        book.id = id_book
        logger.info(
            f"CREATE book: (id: {book.get_id()}, name: {book.get_name()}, author: {book.get_author()})"
        )
        del column, id_book
        return book
Пример #2
0
async def find_book(pool: asyncpg.pool.Pool,
                    logger: logging.Logger,
                    book: Book,
                    id_book: int = None) -> List[Book]:
    async with pool.acquire() as connection:
        connection: asyncpg.connection.Connection
        column: Tuple[str, str] = (book.get_name(), book.get_author())
        logger.debug("FIND_BOOK")

        if id_book:
            books = await connection.fetch(
                SELECT_ID_BOOK.format(table_name=get_table_name()), id_book)
        else:
            books = await connection.fetch(
                SELECT_FIND_BOOK.format(table_name=get_table_name()), *column)

        if books:
            result: List[Book] = [Book(**i) for i in books]
        else:
            result: List[Book] = list()

        return result