Make the house list work
This commit is contained in:
parent
cb8b1469cf
commit
6f5a2f422e
12
backend/app/dtos/houses_list_response.py
Normal file
12
backend/app/dtos/houses_list_response.py
Normal file
@ -0,0 +1,12 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class HouseResponse(BaseModel):
|
||||
id: str
|
||||
description: str
|
||||
address: str
|
||||
city: str
|
||||
country: str
|
||||
price: float
|
||||
|
||||
class HousesListResponse(BaseModel):
|
||||
houses: list[HouseResponse]
|
||||
@ -11,8 +11,8 @@ class HouseRepository:
|
||||
def __init__(self, session: Annotated[AsyncSession, Depends(get_session)]) -> None:
|
||||
self.session = session
|
||||
|
||||
async def get_all(self) -> list[House]:
|
||||
statement = select(House)
|
||||
async def get_all(self, limit: int = 100, offset: int = 0) -> list[House]:
|
||||
statement = select(House).offset(offset).limit(limit)
|
||||
result = await self.session.execute(statement)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ from ..repositories.house_repository import HouseRepository
|
||||
from ..models.house import House
|
||||
from ..dtos.house_create_request import HouseCreateRequest
|
||||
from ..dtos.house_create_response import HouseCreateResponse
|
||||
from ..dtos.houses_list_response import HousesListResponse, HouseResponse
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -30,5 +31,12 @@ async def create_house(body: HouseCreateRequest, auth: Annotated[AuthContext, De
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def get_all_houses():
|
||||
raise NotImplementedError("This endpoint is not implemented yet.")
|
||||
async def get_all_houses(house_repository: Annotated[HouseRepository, Depends()], limit: int = 100, offset: int = 0) -> HousesListResponse:
|
||||
all_houses = await house_repository.get_all(offset=offset, limit=limit)
|
||||
|
||||
house_responses = ([HouseResponse(id=str(house.id), description=house.description, address=house.address, city=house.city, country=house.country, price=house.price) for house in all_houses])
|
||||
print(house_responses)
|
||||
|
||||
return HousesListResponse(
|
||||
houses=house_responses
|
||||
)
|
||||
Loading…
x
Reference in New Issue
Block a user