refactor: mixins for services
This commit is contained in:
@ -1,18 +1,14 @@
|
||||
from typing import Generic, TypeVar, Type, Optional
|
||||
from typing import Type, Optional
|
||||
|
||||
from sqlalchemy import select, Select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
EntityType = TypeVar("EntityType")
|
||||
CreateType = TypeVar("CreateType")
|
||||
UpdateType = TypeVar("UpdateType")
|
||||
|
||||
|
||||
class RepBaseMixin(Generic[EntityType]):
|
||||
class RepBaseMixin[EntityType]:
|
||||
session: AsyncSession
|
||||
|
||||
|
||||
class RepDeleteMixin(RepBaseMixin[EntityType]):
|
||||
class RepDeleteMixin[EntityType](RepBaseMixin[EntityType]):
|
||||
async def delete(self, obj: EntityType, is_soft: bool) -> None:
|
||||
if not is_soft:
|
||||
await self.session.delete(obj)
|
||||
@ -28,7 +24,7 @@ class RepDeleteMixin(RepBaseMixin[EntityType]):
|
||||
await self.session.commit()
|
||||
|
||||
|
||||
class RepCreateMixin(RepBaseMixin[EntityType], Generic[EntityType, CreateType]):
|
||||
class RepCreateMixin[EntityType, CreateType](RepBaseMixin[EntityType]):
|
||||
entity_class: Type[EntityType]
|
||||
|
||||
async def create(self, data: CreateType) -> int:
|
||||
@ -39,7 +35,7 @@ class RepCreateMixin(RepBaseMixin[EntityType], Generic[EntityType, CreateType]):
|
||||
return obj.id
|
||||
|
||||
|
||||
class RepUpdateMixin(RepBaseMixin[EntityType], Generic[EntityType, UpdateType]):
|
||||
class RepUpdateMixin[EntityType, UpdateType](RepBaseMixin[EntityType]):
|
||||
async def _apply_update_data_to_model(
|
||||
self,
|
||||
model: EntityType,
|
||||
@ -61,8 +57,11 @@ class RepUpdateMixin(RepBaseMixin[EntityType], Generic[EntityType, UpdateType]):
|
||||
await self.session.refresh(model)
|
||||
return model
|
||||
|
||||
async def update(self, entity: EntityType, data: UpdateType) -> EntityType:
|
||||
pass
|
||||
|
||||
class GetByIdMixin(RepBaseMixin[EntityType]):
|
||||
|
||||
class RepGetByIdMixin[EntityType](RepBaseMixin[EntityType]):
|
||||
entity_class: Type[EntityType]
|
||||
|
||||
def _process_get_by_id_stmt(self, stmt: Select) -> Select:
|
||||
@ -78,7 +77,7 @@ class GetByIdMixin(RepBaseMixin[EntityType]):
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
class GetAllMixin(RepBaseMixin[EntityType]):
|
||||
class RepGetAllMixin[EntityType](RepBaseMixin[EntityType]):
|
||||
entity_class: Type[EntityType]
|
||||
|
||||
def _process_get_all_stmt_with_args(self, stmt: Select, *args) -> Select:
|
||||
|
||||
Reference in New Issue
Block a user