121 lines
4.5 KiB
Python
121 lines
4.5 KiB
Python
|
|
"""Debug CLI — decision commands (like/dislike)."""
|
||
|
|
import click
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
from cli._context import CliContext, get_http_headers, output, error_output, resolve_user_id
|
||
|
|
from database import engine
|
||
|
|
from repositories.decision_repository import DecisionRepository
|
||
|
|
from services import decision_service
|
||
|
|
|
||
|
|
|
||
|
|
@click.group("decisions")
|
||
|
|
def decisions_group() -> None:
|
||
|
|
"""Like/dislike decision commands."""
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
@decisions_group.command("set")
|
||
|
|
@click.argument("listing_id", type=int)
|
||
|
|
@click.argument("decision", type=click.Choice(["liked", "disliked"]))
|
||
|
|
@click.option("--type", "-t", "listing_type", required=True, type=click.Choice(["RENT", "BUY"]))
|
||
|
|
@click.pass_context
|
||
|
|
def set_decision(ctx: click.Context, listing_id: int, decision: str, listing_type: str) -> None:
|
||
|
|
"""Set a like/dislike decision for a listing."""
|
||
|
|
cli_ctx: CliContext = ctx.obj["cli_ctx"]
|
||
|
|
|
||
|
|
try:
|
||
|
|
if cli_ctx.use_http:
|
||
|
|
resp = httpx.put(
|
||
|
|
f"{cli_ctx.api_base_url}/api/decisions/{listing_id}",
|
||
|
|
headers=get_http_headers(cli_ctx.user_email),
|
||
|
|
json={"decision": decision, "listing_type": listing_type},
|
||
|
|
)
|
||
|
|
resp.raise_for_status()
|
||
|
|
data = resp.json()
|
||
|
|
else:
|
||
|
|
user_id = resolve_user_id(cli_ctx.user_email)
|
||
|
|
repo = DecisionRepository(engine)
|
||
|
|
result = decision_service.set_decision(repo, user_id, listing_id, listing_type, decision)
|
||
|
|
data = {
|
||
|
|
"listing_id": result.listing_id,
|
||
|
|
"listing_type": result.listing_type,
|
||
|
|
"decision": result.decision,
|
||
|
|
"created_at": str(result.created_at),
|
||
|
|
"updated_at": str(result.updated_at),
|
||
|
|
}
|
||
|
|
|
||
|
|
output(data, cli_ctx.json_output)
|
||
|
|
except httpx.HTTPStatusError as e:
|
||
|
|
error_output(f"HTTP {e.response.status_code}: {e.response.text}", cli_ctx.json_output)
|
||
|
|
except Exception as e:
|
||
|
|
error_output(str(e), cli_ctx.json_output)
|
||
|
|
|
||
|
|
|
||
|
|
@decisions_group.command("list")
|
||
|
|
@click.pass_context
|
||
|
|
def list_decisions(ctx: click.Context) -> None:
|
||
|
|
"""List all decisions for the user."""
|
||
|
|
cli_ctx: CliContext = ctx.obj["cli_ctx"]
|
||
|
|
|
||
|
|
try:
|
||
|
|
if cli_ctx.use_http:
|
||
|
|
resp = httpx.get(
|
||
|
|
f"{cli_ctx.api_base_url}/api/decisions",
|
||
|
|
headers=get_http_headers(cli_ctx.user_email),
|
||
|
|
)
|
||
|
|
resp.raise_for_status()
|
||
|
|
data = resp.json()
|
||
|
|
else:
|
||
|
|
user_id = resolve_user_id(cli_ctx.user_email)
|
||
|
|
repo = DecisionRepository(engine)
|
||
|
|
decisions = decision_service.get_user_decisions(repo, user_id)
|
||
|
|
data = [
|
||
|
|
{
|
||
|
|
"listing_id": d.listing_id,
|
||
|
|
"listing_type": d.listing_type,
|
||
|
|
"decision": d.decision,
|
||
|
|
"created_at": str(d.created_at),
|
||
|
|
"updated_at": str(d.updated_at),
|
||
|
|
}
|
||
|
|
for d in decisions
|
||
|
|
]
|
||
|
|
|
||
|
|
output(data, cli_ctx.json_output)
|
||
|
|
except httpx.HTTPStatusError as e:
|
||
|
|
error_output(f"HTTP {e.response.status_code}: {e.response.text}", cli_ctx.json_output)
|
||
|
|
except Exception as e:
|
||
|
|
error_output(str(e), cli_ctx.json_output)
|
||
|
|
|
||
|
|
|
||
|
|
@decisions_group.command("remove")
|
||
|
|
@click.argument("listing_id", type=int)
|
||
|
|
@click.option("--type", "-t", "listing_type", required=True, type=click.Choice(["RENT", "BUY"]))
|
||
|
|
@click.pass_context
|
||
|
|
def remove_decision(ctx: click.Context, listing_id: int, listing_type: str) -> None:
|
||
|
|
"""Remove a decision (un-like/un-dislike)."""
|
||
|
|
cli_ctx: CliContext = ctx.obj["cli_ctx"]
|
||
|
|
|
||
|
|
try:
|
||
|
|
if cli_ctx.use_http:
|
||
|
|
resp = httpx.delete(
|
||
|
|
f"{cli_ctx.api_base_url}/api/decisions/{listing_id}",
|
||
|
|
headers=get_http_headers(cli_ctx.user_email),
|
||
|
|
params={"listing_type": listing_type},
|
||
|
|
)
|
||
|
|
resp.raise_for_status()
|
||
|
|
data = resp.json()
|
||
|
|
else:
|
||
|
|
user_id = resolve_user_id(cli_ctx.user_email)
|
||
|
|
repo = DecisionRepository(engine)
|
||
|
|
deleted = decision_service.remove_decision(repo, user_id, listing_id, listing_type)
|
||
|
|
if not deleted:
|
||
|
|
error_output("Decision not found", cli_ctx.json_output)
|
||
|
|
return
|
||
|
|
data = {"success": True}
|
||
|
|
|
||
|
|
output(data, cli_ctx.json_output)
|
||
|
|
except httpx.HTTPStatusError as e:
|
||
|
|
error_output(f"HTTP {e.response.status_code}: {e.response.text}", cli_ctx.json_output)
|
||
|
|
except Exception as e:
|
||
|
|
error_output(str(e), cli_ctx.json_output)
|