Service layer provides validation, delegation to repository, and get_disliked_listing_ids for filtering. All 7 unit tests pass.
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
"""Unified decision service -- shared between CLI and HTTP API."""
|
|
from models.decision import ListingDecision
|
|
from repositories.decision_repository import DecisionRepository
|
|
|
|
VALID_DECISIONS = {"liked", "disliked"}
|
|
|
|
|
|
def set_decision(
|
|
repository: DecisionRepository,
|
|
user_id: int,
|
|
listing_id: int,
|
|
listing_type: str,
|
|
decision: str,
|
|
) -> ListingDecision:
|
|
if decision not in VALID_DECISIONS:
|
|
raise ValueError(
|
|
f"Invalid decision '{decision}'. Must be one of: {VALID_DECISIONS}"
|
|
)
|
|
return repository.upsert_decision(
|
|
user_id=user_id,
|
|
listing_id=listing_id,
|
|
listing_type=listing_type,
|
|
decision=decision,
|
|
)
|
|
|
|
|
|
def get_decisions(
|
|
repository: DecisionRepository,
|
|
user_id: int,
|
|
) -> list[ListingDecision]:
|
|
return repository.get_decisions_for_user(user_id)
|
|
|
|
|
|
def clear_decision(
|
|
repository: DecisionRepository,
|
|
user_id: int,
|
|
listing_id: int,
|
|
listing_type: str,
|
|
) -> bool:
|
|
return repository.delete_decision(
|
|
user_id=user_id,
|
|
listing_id=listing_id,
|
|
listing_type=listing_type,
|
|
)
|
|
|
|
|
|
def get_disliked_listing_ids(
|
|
repository: DecisionRepository,
|
|
user_id: int,
|
|
listing_type: str,
|
|
) -> set[int]:
|
|
return repository.get_disliked_listing_ids(
|
|
user_id=user_id, listing_type=listing_type
|
|
)
|