mirror of
https://github.com/koloideal/DutyLog.git
synced 2026-06-10 10:25:29 +03:00
42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
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_user_id(self, user_id: int) -> list[HoursTransaction]:
|
|
result = await self.session.execute(
|
|
select(HoursTransaction)
|
|
.where(HoursTransaction.user_id == user_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()
|