96 行
2.9 KiB
Python
96 行
2.9 KiB
Python
"""Wardrobe route handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.services.wardrobe_service import WardrobeService
|
|
from core.audit_service import AuditService
|
|
from core.identity import RequestContext
|
|
from core.security import require_authenticated, require_permission
|
|
from database.db_helper import Database, load_database_settings
|
|
from database.repositories.material_repository import MaterialRepository
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
from database.repositories.wardrobe_repository import WardrobeRepository
|
|
|
|
|
|
def list_wardrobe_items(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
query: dict[str, Any] | None = None,
|
|
service: WardrobeService | None = None,
|
|
) -> dict:
|
|
wardrobe_service = service or _create_wardrobe_service()
|
|
params = query or {}
|
|
|
|
def action() -> list[dict[str, Any]]:
|
|
require_authenticated(ctx)
|
|
return [
|
|
item.to_dict()
|
|
for item in wardrobe_service.list_items(profile_id, _optional_str(params.get("wardrobe_category")))
|
|
]
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def import_wardrobe_material(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
payload: dict[str, Any],
|
|
service: WardrobeService | None = None,
|
|
) -> dict:
|
|
wardrobe_service = service or _create_wardrobe_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return wardrobe_service.import_material(profile_id, payload, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def delete_wardrobe_item(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
wardrobe_item_id: str,
|
|
service: WardrobeService | None = None,
|
|
) -> dict:
|
|
wardrobe_service = service or _create_wardrobe_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return wardrobe_service.delete_item(profile_id, wardrobe_item_id, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def match_wardrobe(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: WardrobeService | None = None,
|
|
) -> dict:
|
|
wardrobe_service = service or _create_wardrobe_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
return wardrobe_service.match(profile_id, payload or {}, ctx)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def _create_wardrobe_service() -> WardrobeService:
|
|
database = Database(load_database_settings())
|
|
return WardrobeService(
|
|
wardrobe_repository=WardrobeRepository(database),
|
|
profile_repository=ProfileRepository(database),
|
|
material_repository=MaterialRepository(database),
|
|
audit_service=AuditService(database),
|
|
)
|
|
|
|
|
|
def _optional_str(value: Any) -> str | None:
|
|
if value is None:
|
|
return None
|
|
text = str(value).strip()
|
|
return text or None
|