42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from db.models.transactions import BalanceTransaction, BalanceTxType
|
|
|
|
|
|
class BalanceTXRepository:
|
|
async def create(
|
|
self,
|
|
session: AsyncSession,
|
|
user_id: int,
|
|
amount: int,
|
|
tx_type: BalanceTxType,
|
|
balance_before: int,
|
|
balance_after: int,
|
|
description: str,
|
|
) -> BalanceTransaction:
|
|
created_at = datetime.now(UTC)
|
|
tx = BalanceTransaction(
|
|
user_id=user_id,
|
|
amount=amount,
|
|
tx_type=tx_type,
|
|
balance_before=balance_before,
|
|
balance_after=balance_after,
|
|
description=description,
|
|
created_at=created_at,
|
|
)
|
|
session.add(tx)
|
|
await session.commit()
|
|
|
|
return tx
|
|
|
|
async def get_transactions_by_id(
|
|
self, session: AsyncSession, user_id: int, limit: int = 50
|
|
) -> list[BalanceTransaction]:
|
|
stmt = select(BalanceTransaction).where(BalanceTransaction.user_id == user_id).limit(limit)
|
|
res = await session.execute(stmt)
|
|
|
|
return list(res.scalars().all())
|