service_man/api/uow/uow_base.py

48 lines
936 B
Python
Raw Normal View History

2024-03-06 02:28:59 +03:00
from abc import ABC, abstractmethod
2024-03-04 13:15:28 +03:00
2024-03-06 02:28:59 +03:00
from api.repository.user import UserRepository
2024-03-04 13:15:28 +03:00
2024-03-06 02:28:59 +03:00
class IUnitOfWork(ABC):
users: type[UserRepository]
2024-03-04 13:15:28 +03:00
2024-03-06 02:28:59 +03:00
@abstractmethod
def __init__(self):
...
2024-03-04 13:15:28 +03:00
2024-03-06 02:28:59 +03:00
@abstractmethod
async def __aenter__(self):
...
@abstractmethod
async def __aexit__(self):
...
@abstractmethod
async def commit(self):
...
@abstractmethod
async def rollback(self):
...
class UnitOfWork:
def __init__(self, session_factory):
self.session_factory = session_factory
async def __aenter__(self):
self.session = self.session_factory()
self.users = UserRepository(self.session)
async def __aexit__(self, *args):
await self.session.rollback()
await self.session.close()
async def commit(self):
await self.session.commit()
async def rollback(self):
await self.session.rollback()