Ejemplo n.º 1
0
async def update_create_book(pool: asyncpg.pool.Pool,
                             logger: logging.Logger,
                             book: Book,
                             delete: bool = False) -> Book:
    async with pool.acquire() as connection:
        connection: asyncpg.connection.Connection
        column: Tuple[int, bool,
                      int] = (book.get_assessment(), delete, book.get_id())
        await connection.execute(
            UPDATE_BOOK_REMOVED.format(table_name=get_table_name()), *column)
        logger.info(
            f"UPDATE CREATE book: (id: {book.get_id()}, name: {book.get_name()}, "
            f"author: {book.get_author()}), assessment: {book.get_assessment()})"
        )
        del column
        return book
Ejemplo n.º 2
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
Ejemplo n.º 3
0
async def create_api(request: Request) -> Response:
    text: str = await request.text()
    data: dict = dict()
    try:
        data = json.loads(text)
    except Exception as e:
        return web.json_response(
            {
                "massage": "Error decode json",
                "error": f"{e}",
                "successful": False
            },
            status=415)

    if not verify_json_post_create(data, ["name", "author", "assessment"]):
        return web.json_response(
            {
                "massage": "Error keys json",
                "error": 'name, author',
                "successful": False
            },
            status=422)

    book: Book = Book(**data)
    book_find: List[Book] = await load_book(request, book)

    if not book_find:
        book = await create_book(request.app["pool"], request.app["logger"],
                                 book)
        return web.json_response({
            "id": f"{book.get_id()}",
            "successful": True
        },
                                 status=200)
    else:
        # если ранее был удален, меняем статус
        if len(book_find) == 1 and book_find[0].get_removed():
            book.id = book_find[0].get_id()
            await update_create_book(request.app["pool"],
                                     request.app["logger"], book)
            return web.json_response(
                {
                    "id": f"{book.get_id()}",
                    "successful": True
                }, status=200)
        else:
            return web.json_response(
                {
                    "massage": "Duplicate",
                    "successful": False,
                    "error": ", ".join([str(i.get_id()) for i in book_find])
                },
                status=409)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
async def read_book(pool: asyncpg.pool.Pool,
                    logger: logging.Logger) -> List[Book]:
    async with pool.acquire() as connection:
        connection: asyncpg.connection.Connection
        column: Tuple[bool] = (True, )
        logger.debug("READ_BOOK")
        books = await connection.fetch(
            SELECT_ALL_BOOK.format(table_name=get_table_name()), *column)
        if books:
            result: List[Book] = [Book(**i) for i in books]
        else:
            result: List[Book] = list()
        del column, books
        return result
Ejemplo n.º 6
0
async def delete_book(pool: asyncpg.pool.Pool,
                      logger: logging.Logger,
                      book: Book,
                      delete: bool = True) -> Book:
    async with pool.acquire() as connection:
        connection: asyncpg.connection.Connection
        column: Tuple[bool, datetime,
                      int] = (delete, datetime.now(), book.get_id())
        await connection.execute(
            DELETE_BOOK.format(table_name=get_table_name()), *column)
        logger.info(
            f"DELETE book: (id: {book.get_id()}, name: {book.get_name()}, "
            f"author: {book.get_author()}), assessment: {book.get_assessment()})"
        )
        del column
        return book
Ejemplo n.º 7
0
async def update_api(request: Request) -> Response:
    text: str = await request.text()
    data: dict = dict()
    try:
        data = json.loads(text)
    except Exception as e:
        return web.json_response(
            {
                "massage": "Error decode json",
                "error": f"{e}",
                "successful": False
            },
            status=415)

    if not verify_json_post_create(data, ["id", "assessment"]):
        return web.json_response(
            {
                "massage": "Error keys json",
                "error": 'id, assessment',
                "successful": False
            },
            status=422)

    book: Book = Book(**data)

    book_find: List[Book] = await load_book(request,
                                            book,
                                            id_book=book.get_id())

    if book_find:
        await update_book(request.app["pool"], request.app["logger"], book)
        return web.json_response({
            "id": f"{book.get_id()}",
            "successful": True
        },
                                 status=200)
    else:
        return web.json_response(
            {
                "id": f"{book.get_id()}",
                "error": f"Not found Book",
                "successful": False
            },
            status=460)