refactor: mixins for services
This commit is contained in:
@ -1,25 +1,18 @@
|
||||
from sqlalchemy import Select
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from models import Board
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import (
|
||||
RepDeleteMixin,
|
||||
RepCreateMixin,
|
||||
GetByIdMixin,
|
||||
GetAllMixin,
|
||||
RepUpdateMixin,
|
||||
)
|
||||
from repositories.mixins import *
|
||||
from schemas.board import UpdateBoardSchema, CreateBoardSchema
|
||||
|
||||
|
||||
class BoardRepository(
|
||||
BaseRepository,
|
||||
GetAllMixin[Board],
|
||||
RepGetAllMixin[Board],
|
||||
RepDeleteMixin[Board],
|
||||
RepCreateMixin[Board, CreateBoardSchema],
|
||||
RepUpdateMixin[Board, UpdateBoardSchema],
|
||||
GetByIdMixin[Board],
|
||||
RepGetByIdMixin[Board],
|
||||
):
|
||||
entity_class = Board
|
||||
|
||||
|
||||
@ -1,16 +1,8 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select, Select
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from models import Deal, CardStatusHistory, Board
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import (
|
||||
RepDeleteMixin,
|
||||
RepCreateMixin,
|
||||
GetByIdMixin,
|
||||
RepUpdateMixin,
|
||||
)
|
||||
from repositories.mixins import *
|
||||
from schemas.base import SortDir
|
||||
from schemas.deal import UpdateDealSchema, CreateDealSchema
|
||||
from utils.sorting import apply_sorting
|
||||
@ -21,7 +13,7 @@ class DealRepository(
|
||||
RepDeleteMixin[Deal],
|
||||
RepCreateMixin[Deal, CreateDealSchema],
|
||||
RepUpdateMixin[Deal, UpdateDealSchema],
|
||||
GetByIdMixin[Deal],
|
||||
RepGetByIdMixin[Deal],
|
||||
):
|
||||
entity_class = Deal
|
||||
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -1,25 +1,18 @@
|
||||
from sqlalchemy import Select
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from models.project import Project
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import (
|
||||
RepDeleteMixin,
|
||||
RepCreateMixin,
|
||||
GetByIdMixin,
|
||||
GetAllMixin,
|
||||
RepUpdateMixin,
|
||||
)
|
||||
from repositories.mixins import *
|
||||
from schemas.project import CreateProjectSchema, UpdateProjectSchema
|
||||
|
||||
|
||||
class ProjectRepository(
|
||||
BaseRepository,
|
||||
GetAllMixin[Project],
|
||||
RepGetAllMixin[Project],
|
||||
RepDeleteMixin[Project],
|
||||
RepCreateMixin[Project, CreateProjectSchema],
|
||||
RepUpdateMixin[Project, UpdateProjectSchema],
|
||||
GetByIdMixin[Project],
|
||||
RepGetByIdMixin[Project],
|
||||
):
|
||||
entity_class = Project
|
||||
|
||||
|
||||
@ -1,24 +1,18 @@
|
||||
from sqlalchemy import select, func, Select
|
||||
from sqlalchemy import func
|
||||
|
||||
from models import Status, Deal
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import (
|
||||
RepDeleteMixin,
|
||||
RepCreateMixin,
|
||||
GetByIdMixin,
|
||||
GetAllMixin,
|
||||
RepUpdateMixin,
|
||||
)
|
||||
from repositories.mixins import *
|
||||
from schemas.status import UpdateStatusSchema, CreateStatusSchema
|
||||
|
||||
|
||||
class StatusRepository(
|
||||
BaseRepository,
|
||||
GetAllMixin[Status],
|
||||
RepGetAllMixin[Status],
|
||||
RepDeleteMixin[Status],
|
||||
RepCreateMixin[Status, CreateStatusSchema],
|
||||
RepUpdateMixin[Status, UpdateStatusSchema],
|
||||
GetByIdMixin[Status],
|
||||
RepGetByIdMixin[Status],
|
||||
):
|
||||
entity_class = Status
|
||||
|
||||
|
||||
Reference in New Issue
Block a user