CRUD finished
This commit is contained in:
@ -1,18 +1,12 @@
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from litestar import Controller, get
|
||||
from litestar import Controller, get, post, put
|
||||
from litestar.contrib.repository.filters import LimitOffset, SearchFilter
|
||||
from litestar.di import Provide
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.domain.city import (
|
||||
City,
|
||||
CityReadDTO,
|
||||
CityWriteDTO,
|
||||
Repository,
|
||||
Service,
|
||||
)
|
||||
from app.domain.city import City, CityReadDTO, CityWriteDTO, Repository, Service
|
||||
from app.lib.responses import ObjectListResponse, ObjectResponse
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -55,9 +49,24 @@ class CityController(Controller):
|
||||
content = await service.list(*filters)
|
||||
return ObjectListResponse(content=content)
|
||||
|
||||
@get(DETAIL_ROUTE)
|
||||
async def get_city(
|
||||
self, service: Service, city_id: UUID
|
||||
@post()
|
||||
async def create_city(
|
||||
self,
|
||||
data: City,
|
||||
service: Service,
|
||||
) -> ObjectResponse[City]:
|
||||
content = await service.create(data)
|
||||
return ObjectResponse(content=content)
|
||||
|
||||
@get(DETAIL_ROUTE)
|
||||
async def get_city(self, service: Service, city_id: UUID) -> ObjectResponse[City]:
|
||||
content = await service.get(city_id)
|
||||
return ObjectResponse(content=content)
|
||||
|
||||
@put(DETAIL_ROUTE)
|
||||
async def update_city(
|
||||
self, data: City, service: Service, city_id: UUID
|
||||
) -> ObjectResponse[City]:
|
||||
data.id = city_id
|
||||
content = await service.update(city_id, data)
|
||||
return ObjectResponse(content=content)
|
||||
|
||||
@ -1,18 +1,12 @@
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from litestar import Controller, get
|
||||
from litestar import Controller, get, post, put
|
||||
from litestar.contrib.repository.filters import LimitOffset, SearchFilter
|
||||
from litestar.di import Provide
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.domain.user import (
|
||||
User,
|
||||
UserReadDTO,
|
||||
UserWriteDTO,
|
||||
Repository,
|
||||
Service,
|
||||
)
|
||||
from app.domain.user import Repository, Service, User, UserReadDTO, UserWriteDTO
|
||||
from app.lib.responses import ObjectListResponse, ObjectResponse
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -55,9 +49,26 @@ class UserController(Controller):
|
||||
content = await service.list(*filters)
|
||||
return ObjectListResponse(content=content)
|
||||
|
||||
@get(DETAIL_ROUTE)
|
||||
async def get_user(
|
||||
self, service: Service, user_id: UUID
|
||||
@post()
|
||||
async def create_user(
|
||||
self,
|
||||
data: User,
|
||||
service: Service,
|
||||
) -> ObjectResponse[User]:
|
||||
print(data)
|
||||
content = await service.create(data)
|
||||
return ObjectResponse(content=content)
|
||||
|
||||
@get(DETAIL_ROUTE)
|
||||
async def get_user(self, service: Service, user_id: UUID) -> ObjectResponse[User]:
|
||||
content = await service.get(user_id)
|
||||
return ObjectResponse(content=content)
|
||||
|
||||
|
||||
@put(DETAIL_ROUTE)
|
||||
async def update_user(
|
||||
self, data: User, service: Service, user_id: UUID
|
||||
) -> ObjectResponse[User]:
|
||||
data.id = user_id
|
||||
content = await service.update(user_id, data)
|
||||
return ObjectResponse(content=content)
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
from datetime import datetime
|
||||
from typing import Annotated
|
||||
|
||||
from litestar.contrib.sqlalchemy.base import UUIDBase
|
||||
from litestar.contrib.sqlalchemy.base import UUIDAuditBase
|
||||
from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTO
|
||||
from litestar.dto import DTOConfig
|
||||
from sqlalchemy.orm import Mapped
|
||||
@ -10,13 +10,13 @@ from app.lib import service
|
||||
from app.lib.filter_repository import FilterRepository
|
||||
|
||||
|
||||
class City(UUIDBase):
|
||||
class City(UUIDAuditBase):
|
||||
__tablename__ = "cities" # type: ignore[assignment]
|
||||
|
||||
name: Mapped[str]
|
||||
postal_code: Mapped[str]
|
||||
created_at: Mapped[datetime]
|
||||
modified_at: Mapped[datetime]
|
||||
updated_at: Mapped[datetime]
|
||||
|
||||
|
||||
class Repository(FilterRepository[City]):
|
||||
@ -27,6 +27,6 @@ class Service(service.Service[City]):
|
||||
repository_type = Repository
|
||||
|
||||
|
||||
write_config = DTOConfig(exclude={"id"})
|
||||
write_config = DTOConfig(exclude={"id", "created_at", "updated_at"})
|
||||
CityWriteDTO = SQLAlchemyDTO[Annotated[City, write_config]]
|
||||
CityReadDTO = SQLAlchemyDTO[City]
|
||||
|
||||
@ -1,22 +1,24 @@
|
||||
from datetime import datetime
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from litestar.contrib.sqlalchemy.base import UUIDBase
|
||||
from litestar.contrib.sqlalchemy.base import UUIDAuditBase
|
||||
from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTO
|
||||
from litestar.dto import DTOConfig
|
||||
from sqlalchemy.orm import Mapped
|
||||
from litestar.dto import DTOConfig, Mark, dto_field
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.domain.city import City
|
||||
from app.lib import service
|
||||
from app.lib.filter_repository import FilterRepository
|
||||
|
||||
|
||||
class User(UUIDBase):
|
||||
class User(UUIDAuditBase):
|
||||
__tablename__ = "users" # type: ignore[assignment]
|
||||
|
||||
first_name: Mapped[str]
|
||||
last_name: Mapped[str]
|
||||
created_at: Mapped[datetime]
|
||||
modified_at: Mapped[datetime]
|
||||
city_id: Mapped[UUID] = mapped_column(ForeignKey("cities.id"))
|
||||
city: Mapped[City] = relationship(lazy="joined", info=dto_field(Mark.READ_ONLY))
|
||||
|
||||
|
||||
class Repository(FilterRepository[User]):
|
||||
@ -27,6 +29,7 @@ class Service(service.Service[User]):
|
||||
repository_type = Repository
|
||||
|
||||
|
||||
write_config = DTOConfig(exclude={"id"})
|
||||
write_config = DTOConfig(exclude={"id", "created_at", "updated_at"})
|
||||
read_config = DTOConfig(exclude={"city"})
|
||||
UserWriteDTO = SQLAlchemyDTO[Annotated[User, write_config]]
|
||||
UserReadDTO = SQLAlchemyDTO[User]
|
||||
UserReadDTO = SQLAlchemyDTO[Annotated[User, read_config]]
|
||||
|
||||
Reference in New Issue
Block a user