from sqlalchemy import select, delete from sqlalchemy.ext.asyncio import AsyncSession from dutylog.infrastructure.database.models.hours_transaction import HoursTransaction class HoursTransactionsDAO: def __init__(self, session: AsyncSession): self.session = session async def get_by_id(self, transaction_id: int) -> HoursTransaction | None: result = await self.session.execute( select(HoursTransaction).where(HoursTransaction.id == transaction_id) ) return result.scalar_one_or_none() async def get_all(self) -> list[HoursTransaction]: result = await self.session.execute( select(HoursTransaction).order_by(HoursTransaction.created_at.desc()) ) return list(result.scalars().all()) async def get_by_resident_id(self, resident_id: int) -> list[HoursTransaction]: result = await self.session.execute( select(HoursTransaction) .where(HoursTransaction.resident_id == resident_id) .order_by(HoursTransaction.created_at.desc()) ) return list(result.scalars().all()) async def create(self, transaction: HoursTransaction) -> HoursTransaction: self.session.add(transaction) await self.session.commit() await self.session.refresh(transaction) return transaction async def delete(self, transaction_id: int) -> None: await self.session.execute( delete(HoursTransaction).where(HoursTransaction.id == transaction_id) ) await self.session.commit()