Working version

This commit is contained in:
Eden Kirin
2023-08-26 14:38:33 +02:00
parent f4882cfb0c
commit 7f1acec1af
16 changed files with 598 additions and 77 deletions

99
main.py
View File

@ -1,18 +1,28 @@
from typing import Any, AsyncGenerator
from uuid import UUID
from litestar import Litestar, get
from litestar.contrib.sqlalchemy.plugins import SQLAlchemyAsyncConfig, SQLAlchemyPlugin
from litestar.exceptions import ClientException
from litestar.contrib.repository import FilterTypes
from litestar.contrib.repository.exceptions import (
RepositoryError as RepositoryException,
)
from litestar.contrib.repository.filters import (
BeforeAfter,
CollectionFilter,
LimitOffset,
NotInCollectionFilter,
NotInSearchFilter,
OnBeforeAfter,
OrderBy,
SearchFilter,
)
from litestar.contrib.sqlalchemy.plugins import SQLAlchemyPlugin
from litestar.openapi import OpenAPIConfig
from litestar.status_codes import HTTP_409_CONFLICT
from sqlalchemy import select
from sqlalchemy.engine import URL
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
from service.orm.machine import MachineORM
sessionmaker = async_sessionmaker(expire_on_commit=False)
from app.controllers import create_router
from app.database import db_config, provide_transaction
from app.lib import exceptions
from app.lib.service import ServiceError
@get("/")
@ -20,56 +30,27 @@ async def hello_world() -> str:
return "Hello, world!"
async def get_machine_list(session: AsyncSession) -> list[MachineORM]:
query = select(MachineORM)
# if done is not None:
# query = query.where(MachineORM.done.is_(done))
result = await session.execute(query)
return result.scalars().all()
def serialize_todo(machine: MachineORM) -> dict[str, Any]:
return {"id": machine.id, "caption": machine.caption}
@get("/machines")
async def get_machines(transaction: AsyncSession) -> list[dict[str, Any]]:
return [serialize_todo(todo) for todo in await get_machine_list(transaction)]
async def provide_transaction(
db_session: AsyncSession,
) -> AsyncGenerator[AsyncSession, None]:
print("AAAAAAAAAAAAAAAAAA")
try:
async with db_session.begin():
yield db_session
except IntegrityError as exc:
raise ClientException(
status_code=HTTP_409_CONFLICT,
detail=str(exc),
) from exc
db_connection_string = URL.create(
drivername="postgresql+asyncpg",
username="televend",
password="televend",
host="localhost",
port=5433,
database="televend",
)
db_config = SQLAlchemyAsyncConfig(
connection_string=db_connection_string.render_as_string(hide_password=False)
)
app = Litestar(
route_handlers=[hello_world, get_machines],
route_handlers=[hello_world, create_router()],
openapi_config=OpenAPIConfig(title="My API", version="1.0.0"),
dependencies={"transaction": provide_transaction},
dependencies={"session": provide_transaction},
plugins=[SQLAlchemyPlugin(db_config)],
exception_handlers={
RepositoryException: exceptions.repository_exception_to_http_response, # type: ignore[dict-item]
ServiceError: exceptions.service_exception_to_http_response, # type: ignore[dict-item]
},
signature_namespace={
"AsyncSession": AsyncSession,
"FilterTypes": FilterTypes,
"BeforeAfter": BeforeAfter,
"CollectionFilter": CollectionFilter,
"LimitOffset": LimitOffset,
"UUID": UUID,
"OrderBy": OrderBy,
"SearchFilter": SearchFilter,
"OnBeforeAfter": OnBeforeAfter,
"NotInSearchFilter": NotInSearchFilter,
"NotInCollectionFilter": NotInCollectionFilter,
},
debug=True,
)