Working version
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Optional, cast
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, Optional, cast, TypeVar, Type, Generic
|
||||
|
||||
from litestar import Controller, delete, get, post, put
|
||||
from litestar.contrib.repository.filters import LimitOffset, SearchFilter, FilterTypes
|
||||
from litestar.di import Provide
|
||||
from litestar.pagination import (
|
||||
AbstractAsyncOffsetPaginator,
|
||||
@ -12,16 +14,19 @@ from litestar.status_codes import HTTP_200_OK
|
||||
from sqlalchemy import ScalarResult, func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.domain.machine import MachineReadDTO, MachineWriteDTO, Repository, Service
|
||||
from app.domain.machine import Machine
|
||||
|
||||
from litestar.contrib.repository.filters import SearchFilter, LimitOffset
|
||||
from app.domain.machine import (
|
||||
Machine,
|
||||
MachineReadDTO,
|
||||
MachineWriteDTO,
|
||||
Repository,
|
||||
Service,
|
||||
)
|
||||
from app.lib.responses import ObjectListResponse, ObjectResponse
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
|
||||
|
||||
DETAIL_ROUTE = "/{machine_id:int}"
|
||||
|
||||
|
||||
@ -59,20 +64,10 @@ class MachineController(Controller):
|
||||
|
||||
@get()
|
||||
async def get_machines(
|
||||
self, service: Service,
|
||||
search: Optional[str] = None,
|
||||
) -> list[Machine]:
|
||||
"""Get a list of authors."""
|
||||
|
||||
print("#" * 100)
|
||||
print(search)
|
||||
print("#" * 100)
|
||||
|
||||
self, service: Service, search: Optional[str] = None
|
||||
) -> ObjectListResponse[Machine]:
|
||||
filters = [
|
||||
LimitOffset(
|
||||
limit=20,
|
||||
offset=0
|
||||
),
|
||||
LimitOffset(limit=20, offset=0),
|
||||
]
|
||||
|
||||
if search:
|
||||
@ -83,22 +78,18 @@ class MachineController(Controller):
|
||||
),
|
||||
)
|
||||
|
||||
return await service.list(*filters)
|
||||
|
||||
# @get()
|
||||
# async def get_machines(
|
||||
# self, service: Service, filters: list[FilterTypes]
|
||||
# ) -> list[Machine]:
|
||||
# """Get a list of authors."""
|
||||
# return await service.list(*filters)
|
||||
content = await service.list(*filters)
|
||||
return ObjectListResponse(content=content)
|
||||
|
||||
# @post()
|
||||
# async def create_author(self, data: Machine, service: Service) -> Machine:
|
||||
# return await service.create(data)
|
||||
#
|
||||
@get(DETAIL_ROUTE)
|
||||
async def get_machine(self, service: Service, machine_id: int) -> Machine:
|
||||
return await service.get(machine_id)
|
||||
async def get_machine(self, service: Service, machine_id: int) -> ObjectResponse[Machine]:
|
||||
content = await service.get(machine_id)
|
||||
return ObjectResponse(content=content)
|
||||
|
||||
#
|
||||
# @put(DETAIL_ROUTE)
|
||||
# async def update_author(
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from litestar.contrib.sqlalchemy.plugins import SQLAlchemyAsyncConfig
|
||||
from litestar.contrib.sqlalchemy.plugins import EngineConfig, SQLAlchemyAsyncConfig
|
||||
from litestar.exceptions import ClientException
|
||||
from litestar.status_codes import HTTP_409_CONFLICT
|
||||
from sqlalchemy import URL
|
||||
@ -32,5 +32,8 @@ db_connection_string = URL.create(
|
||||
database="televend",
|
||||
)
|
||||
db_config = SQLAlchemyAsyncConfig(
|
||||
connection_string=db_connection_string.render_as_string(hide_password=False)
|
||||
connection_string=db_connection_string.render_as_string(hide_password=False),
|
||||
engine_config=EngineConfig(
|
||||
echo=True,
|
||||
),
|
||||
)
|
||||
|
||||
@ -1,9 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from litestar.contrib.repository import FilterTypes
|
||||
from litestar.contrib.sqlalchemy.base import BigIntBase
|
||||
from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTO
|
||||
from litestar.contrib.sqlalchemy.repository import SQLAlchemyAsyncRepository
|
||||
from litestar.contrib.sqlalchemy.repository.types import SelectT
|
||||
from litestar.dto import DTOConfig
|
||||
from sqlalchemy import true
|
||||
from sqlalchemy.orm import Mapped
|
||||
|
||||
from app.lib import service
|
||||
@ -11,12 +16,27 @@ from app.lib import service
|
||||
|
||||
class Machine(BigIntBase):
|
||||
__tablename__ = "machines"
|
||||
|
||||
caption: Mapped[str]
|
||||
enabled: Mapped[bool]
|
||||
alive: Mapped[bool]
|
||||
deleted: Mapped[bool]
|
||||
external_id: Mapped[str]
|
||||
|
||||
|
||||
class Repository(SQLAlchemyAsyncRepository[Machine]):
|
||||
model_type = Machine
|
||||
|
||||
def _apply_filters(
|
||||
self, *filters: FilterTypes, apply_pagination: bool = True, statement: SelectT
|
||||
) -> SelectT:
|
||||
statement = super()._apply_filters(
|
||||
*filters, apply_pagination=apply_pagination, statement=statement
|
||||
)
|
||||
statement = statement.where(Machine.alive == true())
|
||||
return statement
|
||||
|
||||
|
||||
class Service(service.Service[Machine]):
|
||||
repository_type = Repository
|
||||
|
||||
|
||||
26
app/lib/responses.py
Normal file
26
app/lib/responses.py
Normal file
@ -0,0 +1,26 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
@dataclass
|
||||
class ObjectResponse(Generic[T]):
|
||||
content: T
|
||||
|
||||
|
||||
@dataclass
|
||||
class ObjectListResponse(Generic[T]):
|
||||
content: list[T]
|
||||
|
||||
|
||||
@dataclass
|
||||
class PaginationMeta:
|
||||
page: int
|
||||
page_count: int
|
||||
|
||||
|
||||
@dataclass
|
||||
class PaginatedObjectListResponse(Generic[T]):
|
||||
content: list[T]
|
||||
meta: PaginationMeta
|
||||
Reference in New Issue
Block a user