From 6f5a2f422e09f5065bdabcf41af7401687bb3a9d Mon Sep 17 00:00:00 2001 From: Jacob Windsor Date: Wed, 19 Feb 2025 15:09:45 +0100 Subject: [PATCH] Make the house list work --- backend/app/dtos/houses_list_response.py | 12 ++++++++++++ backend/app/repositories/house_repository.py | 4 ++-- backend/app/routers/houses.py | 12 ++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 backend/app/dtos/houses_list_response.py diff --git a/backend/app/dtos/houses_list_response.py b/backend/app/dtos/houses_list_response.py new file mode 100644 index 0000000..fbf3274 --- /dev/null +++ b/backend/app/dtos/houses_list_response.py @@ -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] diff --git a/backend/app/repositories/house_repository.py b/backend/app/repositories/house_repository.py index ce97af7..466a627 100644 --- a/backend/app/repositories/house_repository.py +++ b/backend/app/repositories/house_repository.py @@ -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() diff --git a/backend/app/routers/houses.py b/backend/app/routers/houses.py index e0ecffa..c02c5db 100644 --- a/backend/app/routers/houses.py +++ b/backend/app/routers/houses.py @@ -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.") \ No newline at end of file +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 + ) \ No newline at end of file