refactor: repository get by id mixin
This commit is contained in:
@ -1,11 +1,9 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, Select
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from models import Board
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import RepDeleteMixin, RepCreateMixin
|
||||
from repositories.mixins import RepDeleteMixin, RepCreateMixin, GetByIdMixin
|
||||
from schemas.board import UpdateBoardSchema, CreateBoardSchema
|
||||
|
||||
|
||||
@ -13,6 +11,7 @@ class BoardRepository(
|
||||
BaseRepository,
|
||||
RepDeleteMixin[Board],
|
||||
RepCreateMixin[Board, CreateBoardSchema],
|
||||
GetByIdMixin[Board],
|
||||
):
|
||||
entity_class = Board
|
||||
|
||||
@ -25,14 +24,8 @@ class BoardRepository(
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_by_id(self, board_id: int) -> Optional[Board]:
|
||||
stmt = (
|
||||
select(Board)
|
||||
.where(Board.id == board_id, Board.is_deleted.is_(False))
|
||||
.options(selectinload(Board.deals))
|
||||
)
|
||||
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(selectinload(Board.deals))
|
||||
|
||||
async def update(self, board: Board, data: UpdateBoardSchema) -> Board:
|
||||
board.lexorank = data.lexorank if data.lexorank else board.lexorank
|
||||
|
||||
Reference in New Issue
Block a user