feat: add decision_service with unit tests
Service layer provides validation, delegation to repository, and get_disliked_listing_ids for filtering. All 7 unit tests pass.
This commit is contained in:
parent
91c8c884d2
commit
4877a5fc9f
2 changed files with 136 additions and 0 deletions
54
services/decision_service.py
Normal file
54
services/decision_service.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
"""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
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue