mirror of
https://github.com/koloideal/DutyLog.git
synced 2026-06-10 18:35:29 +03:00
update
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user