fix!: removed .commit() from repository level

This commit is contained in:
2026-08-24 11:07:26 +07:00
parent 8d1b753b99
commit a374501e4d
22 changed files with 177 additions and 180 deletions

View File

@@ -1,12 +1,12 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models import Addon
from db.session import UnitOfWork
class AddonsRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
def __init__(self, uow: UnitOfWork) -> None:
self.session = uow.session
async def get_all(self) -> list[Addon]:
stmt = select(Addon)

View File

@@ -1,13 +1,14 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models.invoice import Invoice
from db.session import UnitOfWork
from schemas.invoices import InvoiceStatus
class InvoiceRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
def __init__(self, uow: UnitOfWork) -> None:
self.uow = uow
self.session = uow.session
async def get_by_id(self, id: int) -> Invoice | None:
stmt = select(Invoice).where(Invoice.id == id)
@@ -32,13 +33,9 @@ class InvoiceRepository:
)
self.session.add(obj)
await self.session.commit()
return obj
async def update_status_by_id(self, invoice_id: int, status: InvoiceStatus) -> Invoice | None:
invoice = await self.get_by_id(invoice_id)
invoice.status = status
await self.session.commit()
return invoice

View File

@@ -1,14 +1,14 @@
from datetime import datetime
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models.link_codes import LinkCode
from db.session import UnitOfWork
from schemas.enums import LinkCodeStatus
async def create_link_code(
session: AsyncSession,
uow: UnitOfWork,
*,
code: str,
user_id: int,
@@ -22,20 +22,17 @@ async def create_link_code(
expires_at=expires_at,
)
session.add(link_code)
await session.commit()
uow.session.add(link_code)
return link_code
async def get_link_code_by_code(session: AsyncSession, code: str) -> LinkCode | None:
async def get_link_code_by_code(uow: UnitOfWork, code: str) -> LinkCode | None:
stmt = select(LinkCode).where(LinkCode.code == code)
r = await session.execute(stmt)
r = await uow.session.execute(stmt)
return r.scalar_one_or_none()
async def use_link_code(session: AsyncSession, code: LinkCode) -> LinkCode:
async def use_link_code(uow: UnitOfWork, code: LinkCode) -> LinkCode:
code.status = LinkCodeStatus.USED
await session.commit()
return code

View File

@@ -1,12 +1,13 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models.orders import Order, OrderAddon, OrderStatus
from db.session import UnitOfWork
class OrderRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
def __init__(self, uow: UnitOfWork) -> None:
self.uow = uow
self.session = uow.session
async def create(
self,
@@ -37,7 +38,6 @@ class OrderRepository:
addon = OrderAddon(order_id=order.id, addon_id=addon_id)
self.session.add(addon)
await self.session.commit()
await self.session.refresh(order, attribute_names=["addons"])
return order

View File

@@ -1,12 +1,12 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models.pricing import PricingConfig
from db.session import UnitOfWork
class PricingRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
def __init__(self, uow: UnitOfWork) -> None:
self.session = uow.session
async def get(self) -> PricingConfig | None:
stmt = select(PricingConfig).where(PricingConfig.id == 1)

View File

@@ -1,19 +1,19 @@
from sqlalchemy import func, or_, select, text
from sqlalchemy.ext.asyncio import AsyncSession
from db.models.service_notifications import ServiceNotification
from db.session import UnitOfWork
from schemas.enums import NotificationStatus
async def get_notification_by_id(session: AsyncSession, n_id: int) -> ServiceNotification | None:
async def get_notification_by_id(uow: UnitOfWork, n_id: int) -> ServiceNotification | None:
stmt = select(ServiceNotification).where(ServiceNotification.id == n_id)
r = await session.execute(stmt)
r = await uow.session.execute(stmt)
return r.scalar_one_or_none()
async def get_pending_notifications(
session: AsyncSession, batch_size: int = 50
uow: UnitOfWork, batch_size: int = 50
) -> list[ServiceNotification]:
stmt = (
select(ServiceNotification)
@@ -33,26 +33,24 @@ async def get_pending_notifications(
.with_for_update(skip_locked=True)
.limit(batch_size)
)
r = await session.execute(stmt)
r = await uow.session.execute(stmt)
return list(r.scalars().all())
async def ack_notification(session: AsyncSession, n_id: int) -> ServiceNotification | None:
notification = await get_notification_by_id(session, n_id)
async def ack_notification(uow: UnitOfWork, n_id: int) -> ServiceNotification | None:
notification = await get_notification_by_id(uow, n_id)
if not notification:
return
notification.sent_at = func.now()
notification.status = NotificationStatus.SENT
await session.commit()
return notification
async def mark_notification_as_dispatched(session: AsyncSession, n_id: int):
notification = await get_notification_by_id(session, n_id)
async def mark_notification_as_dispatched(uow: UnitOfWork, n_id: int):
notification = await get_notification_by_id(uow, n_id)
if not notification:
return
@@ -60,5 +58,4 @@ async def mark_notification_as_dispatched(session: AsyncSession, n_id: int):
notification.status = NotificationStatus.DISPATCHED
notification.attempts += 1
await session.commit()
return notification

View File

@@ -1,13 +1,14 @@
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models import Session
from db.session import UnitOfWork
from schemas.providers import ProvidersType
class SessionsRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
def __init__(self, uow: UnitOfWork) -> None:
self.uow = uow
self.session = uow.session
async def get_session_by_id(self, id: int) -> Session | None:
stmt = select(Session).where(Session.id == id)
@@ -27,12 +28,10 @@ class SessionsRepository:
async def create(self, user_id: int, refresh_token_hash: str, iss: ProvidersType) -> Session:
obj = Session(user_id=user_id, refresh_token_hash=refresh_token_hash, source=iss)
self.session.add(obj)
await self.session.commit()
return obj
async def revoke(self, token_id: int):
session = await self.get_session_by_id(token_id)
session.is_revoked = True
session.revoked_at = func.now()
await self.session.commit()
return session

View File

@@ -1,13 +1,14 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models import User
from db.models.transactions import BalanceTransaction, BalanceTxType
from db.session import UnitOfWork
class UserRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
def __init__(self, uow: UnitOfWork) -> None:
self.uow = uow
self.session = uow.session
async def get_user_by_id(self, id: int) -> User | None:
stmt = select(User).where(User.id == id)
@@ -44,8 +45,6 @@ class UserRepository:
referal_id=referal_id,
)
self.session.add(obj)
await self.session.commit()
return obj
async def increase_balance(
@@ -67,11 +66,8 @@ class UserRepository:
self.session.add(obj)
user.balance += amount
await self.session.commit()
return user
async def update_telegram_id(self, user: User, telegram_id: int) -> User:
user.telegram_id = telegram_id
await self.session.commit()
return user