This commit is contained in:
2026-02-27 17:24:37 +03:00
parent d86276f56d
commit e9098f7bc1
13 changed files with 184 additions and 17 deletions
@@ -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()
@@ -1,21 +1,38 @@
from sqlalchemy import select
from sqlalchemy import select, update, delete
from sqlalchemy.ext.asyncio import AsyncSession
from src.dutylog.infrastructure.database.models.user import User
from dutylog.infrastructure.database.models.user import User
class UsersDAO:
def __init__(self, session: AsyncSession):
self.session = session
async def get_user(self, user_id: int) -> User | None:
async def get_by_id(self, user_id: int) -> User | None:
result = await self.session.execute(
select(User).where(User.id == user_id)
)
return result.scalar_one_or_none()
async def create_user(self, user_id: int, username: str | None) -> User:
user = User(id=user_id, username=username)
async def get_all(self) -> list[User]:
result = await self.session.execute(select(User))
return list(result.scalars().all())
async def create(self, user: User) -> User:
self.session.add(user)
await self.session.commit()
await self.session.refresh(user)
return user
async def update(self, user_id: int, **kwargs) -> User | None:
await self.session.execute(
update(User).where(User.id == user_id).values(**kwargs)
)
await self.session.commit()
return await self.get_by_id(user_id)
async def delete(self, user_id: int) -> None:
await self.session.execute(
delete(User).where(User.id == user_id)
)
await self.session.commit()