refactor: repository delete mixin
This commit is contained in:
25
repositories/mixins.py
Normal file
25
repositories/mixins.py
Normal file
@ -0,0 +1,25 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
EntityType = TypeVar("EntityType")
|
||||
|
||||
|
||||
class RepBaseMixin(Generic[EntityType]):
|
||||
session: AsyncSession
|
||||
|
||||
|
||||
class RepDeleteMixin(RepBaseMixin[EntityType]):
|
||||
async def delete(self, obj: EntityType, is_soft: bool) -> None:
|
||||
if not is_soft:
|
||||
await self.session.delete(obj)
|
||||
await self.session.commit()
|
||||
return
|
||||
|
||||
if not hasattr(obj, "is_deleted"):
|
||||
raise AttributeError(
|
||||
f"{obj.__class__.__name__} does not support soft delete (missing `is_deleted` field)"
|
||||
)
|
||||
obj.is_deleted = True
|
||||
self.session.add(obj)
|
||||
await self.session.commit()
|
||||
Reference in New Issue
Block a user