uow and di basic implementation
This commit is contained in:
@@ -1,23 +1,22 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Generic, TypeVar
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import insert, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from api.model.base import Base
|
||||
import api.models as models
|
||||
|
||||
ModelType = TypeVar("ModelType", bound=Base)
|
||||
ModelType = TypeVar("ModelType", bound=models.Base)
|
||||
|
||||
|
||||
class AbstractRepository(ABC):
|
||||
@abstractmethod
|
||||
async def add_one(self, data: dict):
|
||||
raise NotImplementedError
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
async def find_all(self):
|
||||
raise NotImplementedError
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class SQLAlchemyRepository(AbstractRepository, Generic[ModelType]):
|
||||
@@ -26,12 +25,12 @@ class SQLAlchemyRepository(AbstractRepository, Generic[ModelType]):
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.session = session
|
||||
|
||||
async def add_one(self, data: dict) -> UUID:
|
||||
stmt = insert(self.model).values(**data).returning(self.model.id)
|
||||
async def add_one(self, data: dict) -> ModelType:
|
||||
stmt = insert(self.model).values(**data)
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one()
|
||||
|
||||
async def find_all(self):
|
||||
async def find_all(self) -> list[ModelType]:
|
||||
stmt = select(self.model)
|
||||
res = await self.session.execute(stmt)
|
||||
res = [row[0].to_read_model() for row in res.all()]
|
||||
|
Reference in New Issue
Block a user