refactor: repository get by id mixin

This commit is contained in:
2025-09-05 09:53:16 +04:00
parent e5be35be35
commit c1d3ac98f0
5 changed files with 40 additions and 49 deletions

View File

@ -1,18 +1,21 @@
from typing import Optional
from sqlalchemy import select
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
from repositories.mixins import RepDeleteMixin, RepCreateMixin, GetByIdMixin
from schemas.base import SortDir
from schemas.deal import UpdateDealSchema, CreateDealSchema
from utils.sorting import apply_sorting
class DealRepository(
BaseRepository, RepDeleteMixin[Deal], RepCreateMixin[Deal, CreateDealSchema]
BaseRepository,
RepDeleteMixin[Deal],
RepCreateMixin[Deal, CreateDealSchema],
GetByIdMixin[Deal],
):
entity_class = Deal
@ -58,14 +61,8 @@ class DealRepository(
result = await self.session.execute(stmt)
return list(result.scalars().all()), total_items
async def get_by_id(self, deal_id: int) -> Optional[Deal]:
stmt = (
select(Deal)
.options(joinedload(Deal.status), joinedload(Deal.board))
.where(Deal.id == deal_id, Deal.is_deleted.is_(False))
)
result = await self.session.execute(stmt)
return result.scalar_one_or_none()
def _process_get_by_id_stmt(self, stmt: Select) -> Select:
return stmt.options(joinedload(Deal.status), joinedload(Deal.board))
async def update(self, deal: Deal, data: UpdateDealSchema) -> Deal:
deal.lexorank = data.lexorank if data.lexorank else deal.lexorank