from sqlalchemy.ext.asyncio.session import AsyncSession from typing import Annotated from fastapi import Depends from ..providers.db_provider import get_session from ..models.house import House from sqlmodel import select from uuid import UUID 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) result = await self.session.execute(statement) return result.scalars().all() async def get_by_id(self, house_id: UUID): statement = select(House).where(House.id == house_id) result = await self.session.execute(statement) return result.scalar_one_or_none() async def save(self, house: House) -> None: """ Save a house to the database. If a house with that ID already exists, do an upsert. """ existing_house = await self.get_by_id(house.id) if not existing_house: existing_house = house for key, value in house.model_dump(exclude_unset=True).items(): setattr(house, key, value) try: self.session.add(house) await self.session.commit() await self.session.refresh(house) except Exception as e: await self.session.rollback() raise e