refactor
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import logging
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
from .create_placements import _build_placement_output
|
||||
from .get_placement import _build_placement_post_output
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
@@ -26,4 +28,32 @@ async def get_placements(self: 'Usecase', input: dto.GetPlacementsInput) -> dto.
|
||||
placements = await self.database.get_project_placements(input.workspace_id, project.id)
|
||||
log.debug('Fetched %s placements for project %s', len(placements), project.id)
|
||||
|
||||
return dto.GetPlacementsOutput(placements=[_build_placement_output(p) for p in placements])
|
||||
placement_ids = [placement.id for placement in placements]
|
||||
placement_posts = await self.database.get_placement_posts_by_placement_ids(
|
||||
input.workspace_id,
|
||||
placement_ids,
|
||||
include_archived=True,
|
||||
)
|
||||
placement_post_ids = [post.id for post in placement_posts]
|
||||
subscriptions_counts = await self.database.count_subscriptions_by_placement_post_batch(placement_post_ids)
|
||||
|
||||
placement_posts_by_placement_id: dict[uuid.UUID, list[dto.PlacementPostOutput]] = {}
|
||||
for placement_post in placement_posts:
|
||||
placement_post_output = _build_placement_post_output(
|
||||
placement_post, subscriptions_counts.get(placement_post.id, 0)
|
||||
)
|
||||
if placement_post_output is None:
|
||||
continue
|
||||
placement_posts_by_placement_id.setdefault(placement_post.placement_id, []).append(placement_post_output)
|
||||
|
||||
placement_outputs = []
|
||||
for placement in placements:
|
||||
placement_output = _build_placement_output(placement)
|
||||
placement_outputs.append(
|
||||
dto.PlacementWithPostsOutput(
|
||||
**placement_output.model_dump(),
|
||||
placement_posts=placement_posts_by_placement_id.get(placement.id, []),
|
||||
)
|
||||
)
|
||||
|
||||
return dto.GetPlacementsOutput(placements=placement_outputs)
|
||||
|
||||
Reference in New Issue
Block a user